json-as-xlsx 2.2.0 → 2.2.4

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 CHANGED
@@ -50,19 +50,61 @@ xlsx(data, settings) // Will download the excel file
50
50
 
51
51
  ### TypeScript
52
52
 
53
+ Here is an example of a server setup using TS, thanks to @elyse0 for the contribution
54
+
53
55
  ```ts
54
- const jsonSheets: IJsonSheet[] = [{
55
- sheet: "Friends",
56
- columns: [
57
- { label: "Name", value: "name" },
58
- { label: "Username", value: "username" }
59
- ],
60
- content: [
61
- { name: "Andreas", username: "andr34s" }
62
- ]
63
- }]
64
-
65
- xlsx(jsonSheets)
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
+ })
66
108
  ```
67
109
 
68
110
  ## Examples
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_1=require("xlsx");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_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_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});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=jsonSheet.content.map(function(contentItem){return getJsonSheetRow(contentItem,jsonSheet.columns)});var worksheet=xlsx_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_1.write)(workbook,writeOptions):(0,xlsx_1.writeFile)(workbook,filename,writeOptions)}function xlsx(jsonSheets,settings){if(settings===void 0){settings={}}if(jsonSheets.length===0){return}var workbook=xlsx_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_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;
1
+ "use strict";exports.__esModule=true;exports.getWorksheetColumnWidths=exports.getJsonSheetRow=exports.getContentProperty=void 0;var xlsx_1=require("xlsx");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_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_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_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_1.write)(workbook,writeOptions):(0,xlsx_1.writeFile)(workbook,filename,writeOptions)}function xlsx(jsonSheets,settings){if(settings===void 0){settings={}}if(jsonSheets.length===0){return}var workbook=xlsx_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_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.2.0",
3
+ "version": "2.2.4",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "description": "Create excel from json",
@@ -21,7 +21,7 @@
21
21
  "devDependencies": {
22
22
  "@types/jest": "^27.0.2",
23
23
  "@types/node": "^16.10.2",
24
- "@vue/cli-service": "^4.5.13",
24
+ "@vue/cli-service": "^3.12.1",
25
25
  "express": "^4.17.1",
26
26
  "jest": "^27.3.1",
27
27
  "ts-jest": "^27.0.7",
@@ -35,7 +35,8 @@
35
35
  "ts-standard": "^10.0.0"
36
36
  },
37
37
  "files": [
38
- "index.js"
38
+ "index.js",
39
+ "types/index.d.ts"
39
40
  ],
40
41
  "repository": {
41
42
  "type": "git",
@@ -0,0 +1,34 @@
1
+ import { WritingOptions } from 'xlsx'
2
+
3
+ export interface IColumn {
4
+ label: string
5
+ value: string | ((value: IContent) => string | number | boolean | Date | IContent)
6
+ }
7
+
8
+ export interface IContent {
9
+ [key: string]: string | number | boolean | Date | IContent
10
+ }
11
+
12
+ export interface IJsonSheet {
13
+ sheet?: string
14
+ columns: IColumn[]
15
+ content: IContent[]
16
+ }
17
+
18
+ export interface ISettings {
19
+ extraLength?: number
20
+ fileName?: string
21
+ writeOptions?: WritingOptions
22
+ }
23
+
24
+ export interface IJsonSheetRow {
25
+ [key: string]: string | number | boolean | Date | IContent
26
+ }
27
+
28
+ export interface IWorksheetColumnWidth {
29
+ width: number
30
+ }
31
+
32
+ export function xlsx (jsonSheets: IJsonSheet[], settings?: ISettings): Buffer | undefined
33
+
34
+ export default xlsx