Comparing version 1.0.4 to 1.0.5
@@ -0,1 +1,5 @@ | ||
# v1.0.5 | ||
- Function value support is added | ||
# v1.0.4 | ||
@@ -2,0 +6,0 @@ |
{ | ||
"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! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10184
214