Socket
Socket
Sign inDemoInstall

json2sheet

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json2sheet - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

4

CHANGELOG.md

@@ -0,1 +1,5 @@

# v1.0.5
- Function value support is added
# v1.0.4

@@ -2,0 +6,0 @@

2

package.json
{
"name": "json2sheet",
"version": "1.0.4",
"version": "1.0.5",
"description": "Utility package to convert json objects to excel sheets easily (based on exceljs)",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/Rustam-Kirgizbaev/json2sheet",

@@ -9,2 +9,6 @@ # json2sheet

- [Usage](#usage)
- [File Buffer](#file-buffer)
- [Excel File](#excel-file)
- [Function Values](#function-values)
- [Style](#style)
- [Contributing](#contributing)

@@ -30,12 +34,34 @@

1. Create Excel `file buffer` from json object:
### File Buffer
Create Excel `file buffer` from json object:
```javascript
import { jsonToBuffer } from "json2sheet";
import { jsonToBuffer, Column } from "json2sheet";
async function createExcelBuffer() {
const data: any[] = [
{
name: "John Doe",
age: 24,
user: {
name: "johndoe",
},
},
{
name: "Doe John",
age: 42,
user: {
name: "doejohn",
},
},
];
const columns: Column[] = [
{
label: "User's name", // column name
value: "name", // property name
/*
* value is a property name or a function which takes single object as parameter and returns desired value
*/
value: "name",
width: 40, // column width

@@ -63,19 +89,7 @@ style: {

},
];
const data: any[] = [
{
name: "John Doe",
age: 24,
user: {
name: "johndoe",
},
label: "Profile link",
value: (person) => `https://example.com/${person.user.name}`,
width: 20,
},
{
name: "Doe John",
age: 42,
user: {
name: "doejohn",
},
},
];

@@ -104,6 +118,8 @@

2. Create `Excel file` itself:
### Excel File
Create `Excel file` itself:
```javascript
import { jsonToFile } from "json2sheet";
import { jsonToFile, Column } from "json2sheet";

@@ -164,2 +180,34 @@ async function createExcelBuffer() {

### Function Values
Sometimes we need to modify the value from JSON a little bit as in the above example:
```javascript
{
label: "Profile link",
value: (person) => `https://example.com/${person.user.name}`,
width: 20,
}
```
Most of the time, it is a common need so package supports function values. With functions you can transform value according to your need without modifying JSON file or object.
However, function must take only one parameter which represents single object from your data array. If you have existing function you can also use that:
```javascript
function generateLink(person) {
return `https://example.com/${person.user.name}`
}
...
{
label: "Profile link",
value: generateLink,
width: 20,
}
```
### Style
If you want to give more style to your columns [check these files](https://github.com/Rustam-Kirgizbaev/json2sheet/tree/main/src/interfaces)!

@@ -166,0 +214,0 @@ Currently package only supports `font` and `alignment` in column style!

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc