json-as-xlsx 2.3.8 → 2.3.9
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 +3 -3
- package/package.json +10 -9
- package/{index.js → src/index.js} +0 -0
- package/src/index.ts +110 -0
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ xlsx(data, settings) // Will download the excel file
|
|
|
50
50
|
|
|
51
51
|
## Examples
|
|
52
52
|
|
|
53
|
-
This are files used for development, please change imports from
|
|
53
|
+
This are files used for development, please change imports from `../../src/index.js` to `json-as-xlsx`
|
|
54
54
|
|
|
55
|
-
* [VueJS with JavaScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/
|
|
56
|
-
* [Express with TypeScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/server.ts)
|
|
55
|
+
* [VueJS with JavaScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/examples/vue-app/App.vue)
|
|
56
|
+
* [Express with TypeScript](https://github.com/LuisEnMarroquin/json-as-xlsx/blob/main/examples/express/server.ts)
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as-xlsx",
|
|
3
|
-
"version": "2.3.
|
|
4
|
-
"main": "index.
|
|
3
|
+
"version": "2.3.9",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
7
7
|
"description": "Create excel xlsx file from json",
|
|
8
8
|
"author": "LuisEnMarroquin <luis@marroquin.dev>",
|
|
9
9
|
"homepage": "https://xlsx.marroquin.dev",
|
|
10
10
|
"scripts": {
|
|
11
|
-
"start": "tsc --watch index.ts",
|
|
12
|
-
"build": "tsc index.ts && uglifyjs index.js --output index.js",
|
|
11
|
+
"start": "tsc --watch src/index.ts",
|
|
12
|
+
"build": "tsc src/index.ts && uglifyjs src/index.js --output src/index.js",
|
|
13
13
|
"start-client": "vue-cli-service serve",
|
|
14
14
|
"build-client": "vue-cli-service build",
|
|
15
|
-
"start-server": "npx nodemon --exec npx ts-node --skip-project server.ts",
|
|
15
|
+
"start-server": "npx nodemon --exec npx ts-node --skip-project examples/express/server.ts",
|
|
16
16
|
"lint": "ts-standard --fix",
|
|
17
17
|
"test": "jest"
|
|
18
18
|
},
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@types/express": "^4.17.13",
|
|
24
24
|
"@types/jest": "^27.0.2",
|
|
25
25
|
"@types/node": "^17.0.0",
|
|
26
|
-
"@vue/cli-service": "^
|
|
26
|
+
"@vue/cli-service": "^4.5.15",
|
|
27
27
|
"express": "^4.17.1",
|
|
28
28
|
"jest": "^27.3.1",
|
|
29
29
|
"ts-jest": "^27.0.7",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"vue-template-compiler": "^2.6.14"
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
37
|
-
"index.js",
|
|
37
|
+
"src/index.js",
|
|
38
38
|
"types/index.d.ts"
|
|
39
39
|
],
|
|
40
40
|
"repository": {
|
|
@@ -50,7 +50,6 @@
|
|
|
50
50
|
"excel",
|
|
51
51
|
"jsonc",
|
|
52
52
|
"json-xlsx",
|
|
53
|
-
"microsoft",
|
|
54
53
|
"xlsx-json",
|
|
55
54
|
"create-xlsx",
|
|
56
55
|
"create-excel",
|
|
@@ -58,8 +57,10 @@
|
|
|
58
57
|
"json-to-xlsx",
|
|
59
58
|
"json-as-excel",
|
|
60
59
|
"json-to-excel",
|
|
61
|
-
"
|
|
60
|
+
"xlsx-as-json",
|
|
62
61
|
"xlsx-from-json",
|
|
62
|
+
"microsoft",
|
|
63
|
+
"microsoft-xlsx",
|
|
63
64
|
"microsoft-excel"
|
|
64
65
|
],
|
|
65
66
|
"browserslist": [
|
|
File without changes
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { utils, WorkBook, WorkSheet, write, writeFile } from 'xlsx/dist/xlsx.mini.min'
|
|
2
|
+
import { IColumn, IContent, IJsonSheet, IJsonSheetRow, ISettings, IWorksheetColumnWidth } from '../types/index'
|
|
3
|
+
|
|
4
|
+
const getContentProperty = (content: IContent, property: string): string | number | boolean | Date | IContent => {
|
|
5
|
+
const accessContentProperties = (content: IContent, properties: string[]): string | number | boolean | Date | IContent => {
|
|
6
|
+
const value = content[properties[0]]
|
|
7
|
+
|
|
8
|
+
if (properties.length === 1) {
|
|
9
|
+
return value ?? ''
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (value === undefined || typeof value === 'string' || typeof value === 'boolean' ||
|
|
13
|
+
typeof value === 'number' || value instanceof Date) {
|
|
14
|
+
return ''
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return accessContentProperties(value, properties.slice(1))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const properties = property.split('.')
|
|
21
|
+
return accessContentProperties(content, properties)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const getJsonSheetRow = (content: IContent, columns: IColumn[]): IJsonSheetRow => {
|
|
25
|
+
const jsonSheetRow: IJsonSheetRow = {}
|
|
26
|
+
columns.forEach((column) => {
|
|
27
|
+
if (typeof column.value === 'function') {
|
|
28
|
+
jsonSheetRow[column.label] = column.value(content)
|
|
29
|
+
} else {
|
|
30
|
+
jsonSheetRow[column.label] = getContentProperty(content, column.value)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
return jsonSheetRow
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const getWorksheetColumnWidths = (worksheet: WorkSheet, extraLength: number = 1): IWorksheetColumnWidth[] => {
|
|
37
|
+
const columnRange = utils.decode_range(worksheet['!ref'] ?? '')
|
|
38
|
+
|
|
39
|
+
// Column letters present in the workbook, e.g. A, B, C
|
|
40
|
+
const columnLetters: string[] = []
|
|
41
|
+
for (let C = columnRange.s.c; C <= columnRange.e.c; C++) {
|
|
42
|
+
const address = utils.encode_col(C)
|
|
43
|
+
columnLetters.push(address)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return columnLetters.map((column) => {
|
|
47
|
+
// Cells that belong to this column
|
|
48
|
+
const columnCells: string[] = Object.keys(worksheet).filter((cell) => {
|
|
49
|
+
return cell.charAt(0) === column || cell.slice(0, 2) === column
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const maxWidthCell = columnCells.reduce((previousCell, currentCell) => {
|
|
53
|
+
return worksheet[previousCell].v.length > worksheet[currentCell].v.length
|
|
54
|
+
? previousCell
|
|
55
|
+
: currentCell
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return { width: worksheet[maxWidthCell].v.length as number + extraLength }
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const getWorksheet = (jsonSheet: IJsonSheet, settings: ISettings): WorkSheet => {
|
|
63
|
+
let jsonSheetRows: IJsonSheetRow[]
|
|
64
|
+
|
|
65
|
+
if (jsonSheet.content.length > 0) {
|
|
66
|
+
jsonSheetRows = jsonSheet.content.map((contentItem) => {
|
|
67
|
+
return getJsonSheetRow(contentItem, jsonSheet.columns)
|
|
68
|
+
})
|
|
69
|
+
} else {
|
|
70
|
+
// If there's no content, show only column labels
|
|
71
|
+
jsonSheetRows = jsonSheet.columns.map((column) => ({ [column.label]: '' }))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const worksheet = utils.json_to_sheet(jsonSheetRows)
|
|
75
|
+
worksheet['!cols'] = getWorksheetColumnWidths(worksheet, settings.extraLength)
|
|
76
|
+
|
|
77
|
+
return worksheet
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const writeWorkbook = (workbook: WorkBook, settings: ISettings = {}): Buffer | undefined => {
|
|
81
|
+
const filename = `${settings.fileName ?? 'Spreadsheet'}.xlsx`
|
|
82
|
+
const writeOptions = settings.writeOptions ?? {}
|
|
83
|
+
|
|
84
|
+
return writeOptions.type === 'buffer'
|
|
85
|
+
? write(workbook, writeOptions)
|
|
86
|
+
: writeFile(workbook, filename, writeOptions)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type IWorkbookCallback = (workbook: WorkBook) => void
|
|
90
|
+
|
|
91
|
+
const xlsx = (jsonSheets: IJsonSheet[], settings: ISettings = {}, workbookCallback: IWorkbookCallback = () => { }): Buffer | undefined => {
|
|
92
|
+
if (jsonSheets.length === 0) return
|
|
93
|
+
|
|
94
|
+
const workbook = utils.book_new() // Creating a workbook, this is the name given to an Excel file
|
|
95
|
+
jsonSheets.forEach((actualSheet, actualIndex) => {
|
|
96
|
+
const worksheet = getWorksheet(actualSheet, settings)
|
|
97
|
+
const worksheetName = actualSheet.sheet ?? `Sheet ${actualIndex + 1}`
|
|
98
|
+
utils.book_append_sheet(workbook, worksheet, worksheetName) // Add Worksheet to Workbook
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
workbookCallback(workbook)
|
|
102
|
+
return writeWorkbook(workbook, settings)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default xlsx
|
|
106
|
+
export { getContentProperty, getJsonSheetRow, getWorksheetColumnWidths }
|
|
107
|
+
module.exports = xlsx
|
|
108
|
+
module.exports.getContentProperty = getContentProperty
|
|
109
|
+
module.exports.getJsonSheetRow = getJsonSheetRow
|
|
110
|
+
module.exports.getWorksheetColumnWidths = getWorksheetColumnWidths
|