json-as-xlsx 2.2.5 → 2.3.5
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 +9 -70
- package/index.js +1 -1
- package/package.json +7 -9
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# json-as-xlsx
|
|
2
2
|
|
|
3
|
-
This is a tool that helps to build an excel from a json and it depends only on `xlsx`
|
|
3
|
+
This is a tool that helps to build an excel from a json and it depends only on `xlsx` library
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
You can see a live example of it working on any of this sites (there are many just in case):
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
* [xlsx.pages.dev](https://xlsx.pages.dev)
|
|
8
|
+
* [xlsx.marroquin.dev](https://xlsx.marroquin.dev)
|
|
9
|
+
* [xlsx.luismarroquin.com](https://xlsx.luismarroquin.com)
|
|
8
10
|
|
|
9
11
|
## Usage
|
|
10
12
|
|
|
11
|
-
Just import and use it
|
|
12
|
-
|
|
13
13
|
```js
|
|
14
14
|
let xlsx = require('json-as-xlsx')
|
|
15
15
|
|
|
@@ -40,7 +40,7 @@ let data = [
|
|
|
40
40
|
]
|
|
41
41
|
|
|
42
42
|
let settings = {
|
|
43
|
-
fileName: 'MySpreadsheet', // Name of the spreadsheet
|
|
43
|
+
fileName: 'MySpreadsheet', // Name of the resulting spreadsheet
|
|
44
44
|
extraLength: 3, // A bigger number means that columns will be wider
|
|
45
45
|
writeOptions: {} // Style options from https://github.com/SheetJS/sheetjs#writing-options
|
|
46
46
|
}
|
|
@@ -48,70 +48,9 @@ let settings = {
|
|
|
48
48
|
xlsx(data, settings) // Will download the excel file
|
|
49
49
|
```
|
|
50
50
|
|
|
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
51
|
## Examples
|
|
111
52
|
|
|
112
|
-
This are
|
|
113
|
-
|
|
114
|
-
`require('./index.js')` and `require('../index.js')` to `require('json-as-xlsx')`
|
|
53
|
+
This are files used for development, please change imports from `./index.js` to `json-as-xlsx`
|
|
115
54
|
|
|
116
|
-
*
|
|
117
|
-
*
|
|
55
|
+
* [VueJS with JavaScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/src/App.vue)
|
|
56
|
+
* [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");var getContentProperty=function(content,property){var accessContentProperties=function(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;var getJsonSheetRow=function(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;var getWorksheetColumnWidths=function(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;var getWorksheet=function(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};var writeWorkbook=function(workbook,settings){var _a,_b;if(settings===void 0){settings={}}var filename="".concat((_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)};var xlsx=function(jsonSheets,settings,workbookCallback){if(settings===void 0){settings={}}if(workbookCallback===void 0){workbookCallback=function(){}}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 ".concat(actualIndex+1);xlsx_mini_min_1.utils.book_append_sheet(workbook,worksheet,worksheetName)});workbookCallback(workbook);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,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as-xlsx",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.5",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
7
|
-
"description": "Create excel from json",
|
|
7
|
+
"description": "Create excel xlsx file from json",
|
|
8
8
|
"author": "LuisEnMarroquin <luis@marroquin.dev>",
|
|
9
|
-
"homepage": "https://
|
|
9
|
+
"homepage": "https://xlsx.marroquin.dev",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"start": "tsc --watch index.ts",
|
|
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,21 +20,19 @@
|
|
|
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
|
-
"@types/node": "^
|
|
25
|
+
"@types/node": "^17.0.0",
|
|
25
26
|
"@vue/cli-service": "^3.12.1",
|
|
26
27
|
"express": "^4.17.1",
|
|
27
28
|
"jest": "^27.3.1",
|
|
28
29
|
"ts-jest": "^27.0.7",
|
|
30
|
+
"ts-standard": "^11.0.0",
|
|
29
31
|
"typescript": "^4.4.3",
|
|
30
32
|
"uglify-js": "^3.14.2",
|
|
31
33
|
"vue": "^2.6.14",
|
|
32
34
|
"vue-template-compiler": "^2.6.14"
|
|
33
35
|
},
|
|
34
|
-
"optionalDependencies": {
|
|
35
|
-
"nodemon": "^2.0.12",
|
|
36
|
-
"ts-standard": "^10.0.0"
|
|
37
|
-
},
|
|
38
36
|
"files": [
|
|
39
37
|
"index.js",
|
|
40
38
|
"types/index.d.ts"
|