to-spreadsheet 1.1.0 → 1.1.2
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 +8 -4
- package/lib/generate-excel.js +25 -11
- package/lib/index.d.ts +30 -9
- package/lib/index.js +9 -4
- package/lib/util.d.ts +13 -1
- package/lib/util.js +19 -1
- package/lib/xl/worksheets/sheet.xml.js +28 -8
- package/package.json +1 -1
- package/src/index.ts +27 -9
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ npm package to create spreadsheet in node environment
|
|
|
6
6
|
[](https://bundlephobia.com/result?p=to-spreadsheet)
|
|
7
7
|
[](https://bundlephobia.com/result?p=to-spreadsheet)
|
|
8
8
|
|
|
9
|
+
[](https://github.com/maifeeulasad/to-spreadsheet/stargazers)
|
|
10
|
+
[](https://github.com/maifeeulasad/to-spreadsheet/watchers)
|
|
9
11
|
|
|
10
12
|
# NPM
|
|
11
13
|
```
|
|
@@ -14,8 +16,10 @@ npm i to-spreadsheet
|
|
|
14
16
|
|
|
15
17
|
|
|
16
18
|
# Usage
|
|
19
|
+
[](https://codesandbox.io/s/to-spreadsheet-example-hdmrvc?file=/src/App.tsx)
|
|
20
|
+
|
|
17
21
|
```ts
|
|
18
|
-
import { generateExcel , EnvironmentType } from 'to-spreadsheet/lib/index';
|
|
22
|
+
import { generateExcel , EnvironmentType, skipCell } from 'to-spreadsheet/lib/index';
|
|
19
23
|
|
|
20
24
|
const sampleData = [
|
|
21
25
|
{
|
|
@@ -27,10 +31,10 @@ const sampleData = [
|
|
|
27
31
|
[1, 2, 3],
|
|
28
32
|
]
|
|
29
33
|
},
|
|
30
|
-
{ title: 'Maifee2', content: [[1], [1, 2]] },
|
|
31
|
-
{ title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
|
|
34
|
+
{ title: 'Maifee2', content: [[1], [1, skipCell(3), 2]] },
|
|
35
|
+
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
|
|
32
36
|
]
|
|
33
37
|
|
|
34
38
|
generateExcel(sampleData); // <-- by default generate XLSX for node
|
|
35
39
|
generateExcel(sampleData, EnvironmentType.BROWSER); // <-- for browser
|
|
36
|
-
```
|
|
40
|
+
```
|
package/lib/generate-excel.js
CHANGED
|
@@ -12,6 +12,7 @@ const theme1_xml_1 = require("./xl/theme/theme1.xml");
|
|
|
12
12
|
const workbook_xml_1 = require("./xl/workbook.xml");
|
|
13
13
|
const sheet_xml_1 = require("./xl/worksheets/sheet.xml");
|
|
14
14
|
const index_1 = require("./index");
|
|
15
|
+
const util_1 = require("./util");
|
|
15
16
|
const generateTree = (workbook) => {
|
|
16
17
|
return Object.assign({ "[Content_Types].xml": (0, content_types_xml_1.generateContentTypesXml)(workbook), "_rels/.rels": (0, _rels_1.generateRels)(), "docProps/app.xml": (0, app_xml_1.generateAppXml)(workbook), "docProps/core.xml": (0, core_xml_1.generateCoreXml)({}), "xl/_rels/workbook.xml.rels": (0, workbook_xml_rels_1.generateWorkBookXmlRels)(workbook), "xl/sharedStrings.xml": (0, sharedStrings_xml_1.generateSharedStrings)(workbook), "xl/styles.xml": (0, styles_xml_1.generateStyleXml)(), "xl/theme/theme1.xml": (0, theme1_xml_1.generateTheme1)(), "xl/workbook.xml": (0, workbook_xml_1.generateWorkBookXml)(workbook) }, workbook.sheets.reduce((acc, sheet, idx) => (Object.assign(Object.assign({}, acc), { [`xl/worksheets/sheet${idx + 1}.xml`]: (0, sheet_xml_1.generateSheetXml)(sheet) })), {}));
|
|
17
18
|
};
|
|
@@ -21,19 +22,32 @@ var EnvironmentType;
|
|
|
21
22
|
EnvironmentType[EnvironmentType["BROWSER"] = 1] = "BROWSER";
|
|
22
23
|
})(EnvironmentType || (EnvironmentType = {}));
|
|
23
24
|
exports.EnvironmentType = EnvironmentType;
|
|
24
|
-
const generateExcel = (dump, environmentType = EnvironmentType.
|
|
25
|
+
const generateExcel = (dump, environmentType = EnvironmentType.NODE) => {
|
|
25
26
|
const strings = [];
|
|
26
27
|
const sheets = dump.map(({ title, content }) => {
|
|
27
28
|
const rows = content.map(row => {
|
|
28
|
-
const cells = row.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
value = strings.
|
|
29
|
+
const cells = row.flatMap(content => {
|
|
30
|
+
if (typeof content === 'number') {
|
|
31
|
+
return { type: index_1.ICellType.number, value: content };
|
|
32
|
+
}
|
|
33
|
+
else if (typeof content === 'string') {
|
|
34
|
+
const type = index_1.ICellType.string;
|
|
35
|
+
let value = strings.indexOf(content);
|
|
36
|
+
if (value === -1) {
|
|
37
|
+
strings.push(content);
|
|
38
|
+
value = strings.length - 1;
|
|
39
|
+
}
|
|
40
|
+
return { type: index_1.ICellType.string, value };
|
|
41
|
+
}
|
|
42
|
+
else if (content instanceof util_1.SkipCell) {
|
|
43
|
+
return new Array(content.getSkipCell()).fill({ type: index_1.ICellType.skip, value: undefined });
|
|
44
|
+
}
|
|
45
|
+
else if (content instanceof util_1.Equation) {
|
|
46
|
+
return { type: index_1.ICellType.equation, value: content };
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return { type: index_1.ICellType.skip };
|
|
35
50
|
}
|
|
36
|
-
return { type, value };
|
|
37
51
|
});
|
|
38
52
|
return { cells };
|
|
39
53
|
});
|
|
@@ -68,14 +82,14 @@ const generateExcelWorkbookNode = (workbook) => {
|
|
|
68
82
|
output.on("end", () => {
|
|
69
83
|
console.debug("Data has been drained");
|
|
70
84
|
});
|
|
71
|
-
archive.on("warning",
|
|
85
|
+
archive.on("warning", (err) => {
|
|
72
86
|
if (err.code === "ENOENT") {
|
|
73
87
|
}
|
|
74
88
|
else {
|
|
75
89
|
reject(err);
|
|
76
90
|
}
|
|
77
91
|
});
|
|
78
|
-
archive.on("error",
|
|
92
|
+
archive.on("error", (err) => {
|
|
79
93
|
reject(err);
|
|
80
94
|
});
|
|
81
95
|
archive.pipe(output);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
import { generateExcel, EnvironmentType } from "./generate-excel";
|
|
2
|
+
import { SkipCell, skipCell, Equation, writeEquation } from "./util";
|
|
2
3
|
declare enum ICellType {
|
|
3
4
|
string = "s",
|
|
4
|
-
number = "n"
|
|
5
|
+
number = "n",
|
|
6
|
+
skip = "skip",
|
|
7
|
+
equation = "equation"
|
|
5
8
|
}
|
|
6
|
-
interface
|
|
7
|
-
type: ICellType;
|
|
8
|
-
value:
|
|
9
|
+
interface ICellString {
|
|
10
|
+
type: ICellType.string;
|
|
11
|
+
value: number;
|
|
9
12
|
}
|
|
13
|
+
interface ICellNumber {
|
|
14
|
+
type: ICellType.number;
|
|
15
|
+
value: number;
|
|
16
|
+
}
|
|
17
|
+
interface ICellSkip {
|
|
18
|
+
type: ICellType.skip;
|
|
19
|
+
}
|
|
20
|
+
interface ICellEquation {
|
|
21
|
+
type: ICellType.equation;
|
|
22
|
+
value: Equation;
|
|
23
|
+
}
|
|
24
|
+
declare type ICell = ICellString | ICellNumber | ICellSkip | ICellEquation;
|
|
10
25
|
interface IRows {
|
|
11
26
|
cells: ICell[];
|
|
12
27
|
}
|
|
@@ -21,11 +36,17 @@ interface IWorkbook {
|
|
|
21
36
|
}
|
|
22
37
|
interface IPage {
|
|
23
38
|
title: string;
|
|
24
|
-
content: (string | number)[][];
|
|
39
|
+
content: (string | number | undefined | SkipCell | Equation)[][];
|
|
25
40
|
}
|
|
26
41
|
export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage };
|
|
27
|
-
declare const sampleData: {
|
|
42
|
+
declare const sampleData: ({
|
|
43
|
+
title: string;
|
|
44
|
+
content: (string[] | (number | Equation)[])[];
|
|
45
|
+
} | {
|
|
46
|
+
title: string;
|
|
47
|
+
content: (number | SkipCell)[][];
|
|
48
|
+
} | {
|
|
28
49
|
title: string;
|
|
29
|
-
content: (string
|
|
30
|
-
}[];
|
|
31
|
-
export { generateExcel, sampleData, EnvironmentType };
|
|
50
|
+
content: (string | undefined)[][];
|
|
51
|
+
})[];
|
|
52
|
+
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation };
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.ICellType = void 0;
|
|
3
|
+
exports.writeEquation = exports.skipCell = exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.ICellType = void 0;
|
|
4
4
|
const generate_excel_1 = require("./generate-excel");
|
|
5
5
|
Object.defineProperty(exports, "generateExcel", { enumerable: true, get: function () { return generate_excel_1.generateExcel; } });
|
|
6
6
|
Object.defineProperty(exports, "EnvironmentType", { enumerable: true, get: function () { return generate_excel_1.EnvironmentType; } });
|
|
7
|
+
const util_1 = require("./util");
|
|
8
|
+
Object.defineProperty(exports, "skipCell", { enumerable: true, get: function () { return util_1.skipCell; } });
|
|
9
|
+
Object.defineProperty(exports, "writeEquation", { enumerable: true, get: function () { return util_1.writeEquation; } });
|
|
7
10
|
var ICellType;
|
|
8
11
|
(function (ICellType) {
|
|
9
12
|
ICellType["string"] = "s";
|
|
10
13
|
ICellType["number"] = "n";
|
|
14
|
+
ICellType["skip"] = "skip";
|
|
15
|
+
ICellType["equation"] = "equation";
|
|
11
16
|
})(ICellType || (ICellType = {}));
|
|
12
17
|
exports.ICellType = ICellType;
|
|
13
18
|
const sampleData = [
|
|
@@ -17,10 +22,10 @@ const sampleData = [
|
|
|
17
22
|
['woof', 'smack'],
|
|
18
23
|
[1],
|
|
19
24
|
[1, 2],
|
|
20
|
-
[1, 2, 3],
|
|
25
|
+
[1, 2, 3, (0, util_1.writeEquation)('SUM(A5,C5)')],
|
|
21
26
|
]
|
|
22
27
|
},
|
|
23
|
-
{ title: 'Maifee2', content: [[1], [1, 2]] },
|
|
24
|
-
{ title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
|
|
28
|
+
{ title: 'Maifee2', content: [[1], [1, (0, util_1.skipCell)(3), 2]] },
|
|
29
|
+
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
|
|
25
30
|
];
|
|
26
31
|
exports.sampleData = sampleData;
|
package/lib/util.d.ts
CHANGED
|
@@ -4,4 +4,16 @@ declare const indexToVbRelationIndex: (index: number) => number;
|
|
|
4
4
|
declare const indexToRowIndex: (index: number) => string;
|
|
5
5
|
declare const rowColumnToVbPosition: (row: number, col: number) => string;
|
|
6
6
|
declare const calculateExtant: (rows: IRows[]) => string;
|
|
7
|
-
|
|
7
|
+
declare class SkipCell {
|
|
8
|
+
private skipCell;
|
|
9
|
+
getSkipCell: () => number;
|
|
10
|
+
constructor(skipCell: number);
|
|
11
|
+
}
|
|
12
|
+
declare const skipCell: (skipCell: number) => SkipCell;
|
|
13
|
+
declare class Equation {
|
|
14
|
+
private equation;
|
|
15
|
+
getEquation: () => string;
|
|
16
|
+
constructor(equation: string);
|
|
17
|
+
}
|
|
18
|
+
declare const writeEquation: (equation: string) => Equation;
|
|
19
|
+
export { indexToVbIndex, indexToVbRelationIndex, indexToRowIndex, rowColumnToVbPosition, calculateExtant, SkipCell, skipCell, Equation, writeEquation };
|
package/lib/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
|
|
3
|
+
exports.writeEquation = exports.Equation = exports.skipCell = exports.SkipCell = exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
|
|
4
4
|
const indexToVbIndex = (index) => index + 1;
|
|
5
5
|
exports.indexToVbIndex = indexToVbIndex;
|
|
6
6
|
const indexToVbRelationIndex = (index) => indexToVbIndex(index) + 2;
|
|
@@ -20,3 +20,21 @@ const rowColumnToVbPosition = (row, col) => indexToRowIndex(indexToVbIndex(row))
|
|
|
20
20
|
exports.rowColumnToVbPosition = rowColumnToVbPosition;
|
|
21
21
|
const calculateExtant = (rows) => rowColumnToVbPosition(Math.max(...rows.map(row => row.cells.length)) - 1, rows.length - 1);
|
|
22
22
|
exports.calculateExtant = calculateExtant;
|
|
23
|
+
class SkipCell {
|
|
24
|
+
constructor(skipCell) {
|
|
25
|
+
this.getSkipCell = () => this.skipCell;
|
|
26
|
+
this.skipCell = skipCell;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.SkipCell = SkipCell;
|
|
30
|
+
const skipCell = (skipCell) => new SkipCell(skipCell);
|
|
31
|
+
exports.skipCell = skipCell;
|
|
32
|
+
class Equation {
|
|
33
|
+
constructor(equation) {
|
|
34
|
+
this.getEquation = () => this.equation;
|
|
35
|
+
this.equation = equation;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.Equation = Equation;
|
|
39
|
+
const writeEquation = (equation) => new Equation(equation);
|
|
40
|
+
exports.writeEquation = writeEquation;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateSheetXml = void 0;
|
|
4
|
+
const __1 = require("../..");
|
|
4
5
|
const util_1 = require("../../util");
|
|
5
6
|
const generateSheetXml = (sheet) => {
|
|
6
7
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
@@ -19,14 +20,33 @@ const generateSheetXml = (sheet) => {
|
|
|
19
20
|
<col max="1025" min="1" style="0" width="11.52"/>
|
|
20
21
|
</cols>
|
|
21
22
|
<sheetData>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
${sheet.rows.map((row, rowIndex) => {
|
|
24
|
+
let rowContent = '';
|
|
25
|
+
rowContent += `<row r="${(0, util_1.indexToVbIndex)(rowIndex)}">\n`;
|
|
26
|
+
row.cells.forEach((cell, cellIndex) => {
|
|
27
|
+
const cellType = cell.type;
|
|
28
|
+
if (cellType !== __1.ICellType.skip) {
|
|
29
|
+
const cellPosition = (0, util_1.rowColumnToVbPosition)(cellIndex, rowIndex);
|
|
30
|
+
const cellValue = cell.value || '';
|
|
31
|
+
if (cell.type === __1.ICellType.equation) {
|
|
32
|
+
rowContent += `
|
|
33
|
+
<c r="${cellPosition}" t="n">
|
|
34
|
+
<f aca="false">${cell.value.getEquation()}</f>
|
|
35
|
+
</c>\n`;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
rowContent += `
|
|
39
|
+
<c r="${cellPosition}" t="${cellType}">
|
|
40
|
+
<v>${cellValue}</v>
|
|
41
|
+
</c>\n`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
rowContent += '</row>\n';
|
|
46
|
+
return rowContent;
|
|
47
|
+
}).join('')}
|
|
48
|
+
</sheetData>
|
|
49
|
+
|
|
30
50
|
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
|
|
31
51
|
</worksheet>
|
|
32
52
|
`;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { generateExcel, EnvironmentType } from "./generate-excel";
|
|
2
|
+
import { SkipCell, skipCell, Equation, writeEquation } from "./util";
|
|
2
3
|
|
|
3
4
|
enum ICellType {
|
|
4
5
|
string = "s",
|
|
5
|
-
number = "n"
|
|
6
|
+
number = "n",
|
|
7
|
+
skip = "skip",
|
|
8
|
+
equation = "equation",
|
|
6
9
|
}
|
|
7
10
|
|
|
8
|
-
interface
|
|
9
|
-
type: ICellType;
|
|
10
|
-
value:
|
|
11
|
+
interface ICellString {
|
|
12
|
+
type: ICellType.string;
|
|
13
|
+
value: number;
|
|
14
|
+
}
|
|
15
|
+
interface ICellNumber {
|
|
16
|
+
type: ICellType.number;
|
|
17
|
+
value: number;
|
|
18
|
+
}
|
|
19
|
+
interface ICellSkip {
|
|
20
|
+
type: ICellType.skip;
|
|
21
|
+
}
|
|
22
|
+
interface ICellEquation {
|
|
23
|
+
type: ICellType.equation;
|
|
24
|
+
value: Equation;
|
|
11
25
|
}
|
|
12
26
|
|
|
27
|
+
type ICell = ICellString | ICellNumber | ICellSkip | ICellEquation;
|
|
28
|
+
|
|
13
29
|
interface IRows {
|
|
14
30
|
cells: ICell[];
|
|
15
31
|
}
|
|
@@ -27,7 +43,7 @@ interface IWorkbook {
|
|
|
27
43
|
|
|
28
44
|
interface IPage {
|
|
29
45
|
title: string
|
|
30
|
-
content: (string | number)[][]
|
|
46
|
+
content: (string | number | undefined | SkipCell | Equation)[][]
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage }
|
|
@@ -40,11 +56,13 @@ const sampleData = [
|
|
|
40
56
|
['woof', 'smack'],
|
|
41
57
|
[1],
|
|
42
58
|
[1, 2],
|
|
43
|
-
[1, 2, 3],
|
|
59
|
+
[1, 2, 3, writeEquation('SUM(A5,C5)')],
|
|
44
60
|
]
|
|
45
61
|
},
|
|
46
|
-
{ title: 'Maifee2', content: [[1], [1, 2]] },
|
|
47
|
-
{ title: 'Maifee3', content: [['meaw', "meaw"], ["woof", 'woof']] }
|
|
62
|
+
{ title: 'Maifee2', content: [[1], [1, skipCell(3), 2]] },
|
|
63
|
+
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
|
|
48
64
|
]
|
|
49
65
|
|
|
50
|
-
|
|
66
|
+
// generateExcel(sampleData) // <- check before releasing with `yarn test:compile`
|
|
67
|
+
|
|
68
|
+
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation };
|