json-as-xlsx 2.2.5 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -70
- package/index.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,14 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
This is a tool that helps to build an excel from a json and it depends only on `xlsx`
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
You can see a live example of how it works on this site: [luisenmarroquin.github.io/json-as-xlsx](https://luisenmarroquin.github.io/json-as-xlsx)
|
|
5
|
+
You can see a live example of it working on this site: [luisenmarroquin.github.io/json-as-xlsx](https://luisenmarroquin.github.io/json-as-xlsx)
|
|
8
6
|
|
|
9
7
|
## Usage
|
|
10
8
|
|
|
11
|
-
Just import and use it
|
|
12
|
-
|
|
13
9
|
```js
|
|
14
10
|
let xlsx = require('json-as-xlsx')
|
|
15
11
|
|
|
@@ -40,7 +36,7 @@ let data = [
|
|
|
40
36
|
]
|
|
41
37
|
|
|
42
38
|
let settings = {
|
|
43
|
-
fileName: 'MySpreadsheet', // Name of the spreadsheet
|
|
39
|
+
fileName: 'MySpreadsheet', // Name of the resulting spreadsheet
|
|
44
40
|
extraLength: 3, // A bigger number means that columns will be wider
|
|
45
41
|
writeOptions: {} // Style options from https://github.com/SheetJS/sheetjs#writing-options
|
|
46
42
|
}
|
|
@@ -48,70 +44,9 @@ let settings = {
|
|
|
48
44
|
xlsx(data, settings) // Will download the excel file
|
|
49
45
|
```
|
|
50
46
|
|
|
51
|
-
### TypeScript
|
|
52
|
-
|
|
53
|
-
Here is an example of a server setup using TS, thanks to @elyse0 for the contribution
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
import xlsx, { IJsonSheet, ISettings } from 'json-as-xlsx'
|
|
57
|
-
import express from 'express'
|
|
58
|
-
|
|
59
|
-
const app = express()
|
|
60
|
-
app.use(express.json())
|
|
61
|
-
app.use(express.urlencoded({ extended: true }))
|
|
62
|
-
|
|
63
|
-
const data: IJsonSheet[] = [
|
|
64
|
-
{
|
|
65
|
-
sheet: 'Adults',
|
|
66
|
-
columns: [
|
|
67
|
-
{ label: 'User', value: 'user' },
|
|
68
|
-
{ label: 'Age', value: 'age' }
|
|
69
|
-
],
|
|
70
|
-
content: [
|
|
71
|
-
{ user: 'Andrea', age: 20, more: { phone: '11111111' } },
|
|
72
|
-
{ user: 'Luis', age: 21, more: { phone: '12345678' } }
|
|
73
|
-
]
|
|
74
|
-
}, {
|
|
75
|
-
sheet: 'Children',
|
|
76
|
-
columns: [
|
|
77
|
-
{ label: 'User', value: 'user' },
|
|
78
|
-
{ label: 'Age', value: 'age' }
|
|
79
|
-
],
|
|
80
|
-
content: [
|
|
81
|
-
{ user: 'Manuel', age: 16, more: { phone: '99999999' } },
|
|
82
|
-
{ user: 'Ana', age: 17, more: { phone: '87654321' } }
|
|
83
|
-
]
|
|
84
|
-
}
|
|
85
|
-
]
|
|
86
|
-
|
|
87
|
-
const settings: ISettings = {
|
|
88
|
-
writeOptions: {
|
|
89
|
-
type: 'buffer',
|
|
90
|
-
bookType: 'xlsx'
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
app.get('/', (_, res) => {
|
|
95
|
-
const buffer = xlsx(data, settings)
|
|
96
|
-
res.writeHead(200, {
|
|
97
|
-
'Content-Type': 'application/octet-stream',
|
|
98
|
-
'Content-disposition': 'attachment; filename=MySheet.xlsx'
|
|
99
|
-
})
|
|
100
|
-
res.end(buffer)
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
const port = process.env.PORT ?? 3000
|
|
104
|
-
|
|
105
|
-
app.listen(port, () => {
|
|
106
|
-
console.log(`Your app is listening on port ${port}`)
|
|
107
|
-
})
|
|
108
|
-
```
|
|
109
|
-
|
|
110
47
|
## Examples
|
|
111
48
|
|
|
112
|
-
This are
|
|
113
|
-
|
|
114
|
-
`require('./index.js')` and `require('../index.js')` to `require('json-as-xlsx')`
|
|
49
|
+
This are files used for development, please change imports from `./index.js` to `json-as-xlsx`
|
|
115
50
|
|
|
116
|
-
*
|
|
117
|
-
*
|
|
51
|
+
* [VueJS with JavaScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/src/App.vue)
|
|
52
|
+
* [Express with TypeScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/server.ts)
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var
|
|
1
|
+
"use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_mini_min_1=require("xlsx/dist/xlsx.mini.min");function getContentProperty(content,property){function accessContentProperties(content,properties){var value=content[properties[0]];if(properties.length===1){return value!==null&&value!==void 0?value:""}if(value===undefined||typeof value==="string"||typeof value==="boolean"||typeof value==="number"||value instanceof Date){return""}return accessContentProperties(value,properties.slice(1))}var properties=property.split(".");return accessContentProperties(content,properties)}exports.getContentProperty=getContentProperty;function getJsonSheetRow(content,columns){var jsonSheetRow={};columns.forEach(function(column){if(typeof column.value==="function"){jsonSheetRow[column.label]=column.value(content)}else{jsonSheetRow[column.label]=getContentProperty(content,column.value)}});return jsonSheetRow}exports.getJsonSheetRow=getJsonSheetRow;function getWorksheetColumnWidths(worksheet,extraLength){var _a;if(extraLength===void 0){extraLength=1}var columnRange=xlsx_mini_min_1.utils.decode_range((_a=worksheet["!ref"])!==null&&_a!==void 0?_a:"");var columnLetters=[];for(var C=columnRange.s.c;C<=columnRange.e.c;C++){var address=xlsx_mini_min_1.utils.encode_col(C);columnLetters.push(address)}return columnLetters.map(function(column){var columnCells=Object.keys(worksheet).filter(function(cell){return cell.charAt(0)===column||cell.slice(0,2)===column});var maxWidthCell=columnCells.reduce(function(previousCell,currentCell){return worksheet[previousCell].v.length>worksheet[currentCell].v.length?previousCell:currentCell});return{width:worksheet[maxWidthCell].v.length+extraLength}})}exports.getWorksheetColumnWidths=getWorksheetColumnWidths;function getWorksheet(jsonSheet,settings){var jsonSheetRows;if(jsonSheet.content.length>0){jsonSheetRows=jsonSheet.content.map(function(contentItem){return getJsonSheetRow(contentItem,jsonSheet.columns)})}else{jsonSheetRows=jsonSheet.columns.map(function(column){var _a;return _a={},_a[column.label]="",_a})}var worksheet=xlsx_mini_min_1.utils.json_to_sheet(jsonSheetRows);worksheet["!cols"]=getWorksheetColumnWidths(worksheet,settings.extraLength);return worksheet}function writeWorkbook(workbook,settings){var _a,_b;if(settings===void 0){settings={}}var filename=((_a=settings.fileName)!==null&&_a!==void 0?_a:"Spreadsheet")+".xlsx";var writeOptions=(_b=settings.writeOptions)!==null&&_b!==void 0?_b:{};return writeOptions.type==="buffer"?(0,xlsx_mini_min_1.write)(workbook,writeOptions):(0,xlsx_mini_min_1.writeFile)(workbook,filename,writeOptions)}function xlsx(jsonSheets,settings){if(settings===void 0){settings={}}if(jsonSheets.length===0){return}var workbook=xlsx_mini_min_1.utils.book_new();jsonSheets.forEach(function(actualSheet,actualIndex){var _a;var worksheet=getWorksheet(actualSheet,settings);var worksheetName=(_a=actualSheet.sheet)!==null&&_a!==void 0?_a:"Sheet "+(actualIndex+1);xlsx_mini_min_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});return writeWorkbook(workbook,settings)}exports["default"]=xlsx;module.exports=xlsx;module.exports.getContentProperty=getContentProperty;module.exports.getJsonSheetRow=getJsonSheetRow;module.exports.getWorksheetColumnWidths=getWorksheetColumnWidths;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as-xlsx",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"build": "tsc index.ts && uglifyjs index.js --output index.js",
|
|
13
13
|
"start-client": "vue-cli-service serve",
|
|
14
14
|
"build-client": "vue-cli-service build",
|
|
15
|
-
"start-server": "nodemon server.
|
|
15
|
+
"start-server": "npx nodemon --exec npx ts-node --skip-project server.ts",
|
|
16
16
|
"lint": "ts-standard --fix",
|
|
17
17
|
"test": "jest"
|
|
18
18
|
},
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"xlsx": "^0.17.2"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"@types/express": "^4.17.13",
|
|
23
24
|
"@types/jest": "^27.0.2",
|
|
24
25
|
"@types/node": "^16.10.2",
|
|
25
26
|
"@vue/cli-service": "^3.12.1",
|
|
@@ -32,7 +33,6 @@
|
|
|
32
33
|
"vue-template-compiler": "^2.6.14"
|
|
33
34
|
},
|
|
34
35
|
"optionalDependencies": {
|
|
35
|
-
"nodemon": "^2.0.12",
|
|
36
36
|
"ts-standard": "^10.0.0"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|