json-as-xlsx 2.3.9 → 2.3.10

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +0 -110
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "json-as-xlsx",
3
- "version": "2.3.9",
4
- "main": "src/index.ts",
3
+ "version": "2.3.10",
4
+ "main": "src/index.js",
5
5
  "license": "MIT",
6
6
  "types": "types/index.d.ts",
7
7
  "description": "Create excel xlsx file from json",
package/src/index.ts DELETED
@@ -1,110 +0,0 @@
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