@js-ak/excel-toolbox 1.8.3 → 1.9.0
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/build/cjs/lib/template/template-fs.js +2 -1
- package/build/cjs/lib/template/template-memory.js +2 -1
- package/build/cjs/lib/template/utils/column-letter-to-index.js +15 -0
- package/build/cjs/lib/template/utils/index.js +1 -1
- package/build/cjs/lib/template/utils/prepare-row-to-cells.js +2 -2
- package/build/cjs/lib/template/utils/validate-worksheet-xml.js +19 -9
- package/build/cjs/lib/utils/index.js +2 -0
- package/build/cjs/lib/utils/trim-and-join-multiline.js +32 -0
- package/build/cjs/lib/workbook-builder/default/border.js +18 -0
- package/build/cjs/lib/workbook-builder/default/cell-xf.js +15 -0
- package/build/cjs/lib/workbook-builder/default/fill.js +18 -0
- package/build/cjs/lib/workbook-builder/default/font.js +19 -0
- package/build/cjs/lib/workbook-builder/default/index.js +21 -0
- package/build/cjs/lib/workbook-builder/default/sheet-name.js +10 -0
- package/build/cjs/lib/workbook-builder/index.js +18 -0
- package/build/cjs/lib/workbook-builder/merge-cells/add.js +72 -0
- package/build/cjs/lib/workbook-builder/merge-cells/helpers/index.js +18 -0
- package/build/cjs/lib/workbook-builder/merge-cells/helpers/ranges-equal.js +17 -0
- package/build/cjs/lib/workbook-builder/merge-cells/helpers/ranges-intersect.js +17 -0
- package/build/cjs/lib/workbook-builder/merge-cells/index.js +18 -0
- package/build/cjs/lib/workbook-builder/merge-cells/remove.js +60 -0
- package/build/cjs/lib/workbook-builder/shared-string-ref/add.js +24 -0
- package/build/cjs/lib/workbook-builder/shared-string-ref/index.js +19 -0
- package/build/cjs/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.js +66 -0
- package/build/cjs/lib/workbook-builder/shared-string-ref/remove.js +66 -0
- package/build/cjs/lib/workbook-builder/style-ref/add-or-get.js +94 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/add-num-fmt.js +25 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/border-to-xml.js +49 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/fill-to-xml.js +51 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/font-to-xml.js +112 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/index.js +21 -0
- package/build/cjs/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.js +30 -0
- package/build/cjs/lib/workbook-builder/style-ref/index.js +19 -0
- package/build/cjs/lib/workbook-builder/style-ref/remove-all-from-sheet.js +26 -0
- package/build/cjs/lib/workbook-builder/style-ref/remove.js +52 -0
- package/build/cjs/lib/workbook-builder/types/app-xml-options.js +2 -0
- package/build/cjs/lib/workbook-builder/types/border-style.js +2 -0
- package/build/cjs/lib/workbook-builder/types/cell-data.js +2 -0
- package/build/cjs/lib/workbook-builder/types/cell-style.js +2 -0
- package/build/cjs/lib/workbook-builder/types/cell-type.js +2 -0
- package/build/cjs/lib/workbook-builder/types/cell-value.js +2 -0
- package/build/cjs/lib/workbook-builder/types/cell-xf.js +2 -0
- package/build/cjs/lib/workbook-builder/types/index.js +27 -0
- package/build/cjs/lib/workbook-builder/types/merge-cell.js +2 -0
- package/build/cjs/lib/workbook-builder/types/row-data.js +2 -0
- package/build/cjs/lib/workbook-builder/types/sheet-data.js +2 -0
- package/build/cjs/lib/workbook-builder/types/xml-node.js +2 -0
- package/build/cjs/lib/workbook-builder/utils/build-app-xml.js +51 -0
- package/build/cjs/lib/workbook-builder/utils/build-cell-children.js +59 -0
- package/build/cjs/lib/workbook-builder/utils/build-content-types-xml.js +42 -0
- package/build/cjs/lib/workbook-builder/utils/build-core-xml.js +27 -0
- package/build/cjs/lib/workbook-builder/utils/build-rels-xml.js +19 -0
- package/build/cjs/lib/workbook-builder/utils/build-shared-strings-xml.js +39 -0
- package/build/cjs/lib/workbook-builder/utils/build-styles-xml.js +178 -0
- package/build/cjs/lib/workbook-builder/utils/build-theme-xml.js +609 -0
- package/build/cjs/lib/workbook-builder/utils/build-workbook-rels-xml.js +58 -0
- package/build/cjs/lib/workbook-builder/utils/build-workbook-xml.js +26 -0
- package/build/cjs/lib/workbook-builder/utils/build-worksheet-xml.js +66 -0
- package/build/cjs/lib/workbook-builder/utils/build-xml.js +72 -0
- package/build/cjs/lib/workbook-builder/utils/constants.js +55 -0
- package/build/cjs/lib/workbook-builder/utils/date-to-excel-serial.js +16 -0
- package/build/cjs/lib/workbook-builder/utils/index.js +34 -0
- package/build/cjs/lib/workbook-builder/utils/initialize-files.js +40 -0
- package/build/cjs/lib/workbook-builder/utils/sheet.js +144 -0
- package/build/cjs/lib/workbook-builder/utils/write-shared-strings-xml.js +49 -0
- package/build/cjs/lib/workbook-builder/utils/write-styles-xml.js +196 -0
- package/build/cjs/lib/workbook-builder/utils/write-worksheet-xml.js +209 -0
- package/build/cjs/lib/workbook-builder/utils/write-xml.js +37 -0
- package/build/cjs/lib/workbook-builder/workbook-builder.js +414 -0
- package/build/esm/lib/template/template-fs.js +2 -1
- package/build/esm/lib/template/template-memory.js +2 -1
- package/build/esm/lib/template/utils/column-letter-to-index.js +12 -0
- package/build/esm/lib/template/utils/index.js +1 -1
- package/build/esm/lib/template/utils/prepare-row-to-cells.js +1 -1
- package/build/esm/lib/template/utils/validate-worksheet-xml.js +19 -9
- package/build/esm/lib/utils/index.js +2 -0
- package/build/esm/lib/utils/trim-and-join-multiline.js +29 -0
- package/build/esm/lib/workbook-builder/default/border.js +14 -0
- package/build/esm/lib/workbook-builder/default/cell-xf.js +11 -0
- package/build/esm/lib/workbook-builder/default/fill.js +14 -0
- package/build/esm/lib/workbook-builder/default/font.js +15 -0
- package/build/esm/lib/workbook-builder/default/index.js +5 -0
- package/build/esm/lib/workbook-builder/default/sheet-name.js +6 -0
- package/build/esm/lib/workbook-builder/index.js +2 -0
- package/build/esm/lib/workbook-builder/merge-cells/add.js +36 -0
- package/build/esm/lib/workbook-builder/merge-cells/helpers/index.js +2 -0
- package/build/esm/lib/workbook-builder/merge-cells/helpers/ranges-equal.js +14 -0
- package/build/esm/lib/workbook-builder/merge-cells/helpers/ranges-intersect.js +14 -0
- package/build/esm/lib/workbook-builder/merge-cells/index.js +2 -0
- package/build/esm/lib/workbook-builder/merge-cells/remove.js +24 -0
- package/build/esm/lib/workbook-builder/shared-string-ref/add.js +21 -0
- package/build/esm/lib/workbook-builder/shared-string-ref/index.js +3 -0
- package/build/esm/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.js +63 -0
- package/build/esm/lib/workbook-builder/shared-string-ref/remove.js +63 -0
- package/build/esm/lib/workbook-builder/style-ref/add-or-get.js +58 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/add-num-fmt.js +21 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/border-to-xml.js +45 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/fill-to-xml.js +47 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/font-to-xml.js +75 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/index.js +5 -0
- package/build/esm/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.js +26 -0
- package/build/esm/lib/workbook-builder/style-ref/index.js +3 -0
- package/build/esm/lib/workbook-builder/style-ref/remove-all-from-sheet.js +23 -0
- package/build/esm/lib/workbook-builder/style-ref/remove.js +49 -0
- package/build/esm/lib/workbook-builder/types/app-xml-options.js +1 -0
- package/build/esm/lib/workbook-builder/types/border-style.js +1 -0
- package/build/esm/lib/workbook-builder/types/cell-data.js +1 -0
- package/build/esm/lib/workbook-builder/types/cell-style.js +1 -0
- package/build/esm/lib/workbook-builder/types/cell-type.js +1 -0
- package/build/esm/lib/workbook-builder/types/cell-value.js +1 -0
- package/build/esm/lib/workbook-builder/types/cell-xf.js +1 -0
- package/build/esm/lib/workbook-builder/types/index.js +11 -0
- package/build/esm/lib/workbook-builder/types/merge-cell.js +1 -0
- package/build/esm/lib/workbook-builder/types/row-data.js +1 -0
- package/build/esm/lib/workbook-builder/types/sheet-data.js +1 -0
- package/build/esm/lib/workbook-builder/types/xml-node.js +1 -0
- package/build/esm/lib/workbook-builder/utils/build-app-xml.js +48 -0
- package/build/esm/lib/workbook-builder/utils/build-cell-children.js +56 -0
- package/build/esm/lib/workbook-builder/utils/build-content-types-xml.js +39 -0
- package/build/esm/lib/workbook-builder/utils/build-core-xml.js +24 -0
- package/build/esm/lib/workbook-builder/utils/build-rels-xml.js +16 -0
- package/build/esm/lib/workbook-builder/utils/build-shared-strings-xml.js +36 -0
- package/build/esm/lib/workbook-builder/utils/build-styles-xml.js +142 -0
- package/build/esm/lib/workbook-builder/utils/build-theme-xml.js +606 -0
- package/build/esm/lib/workbook-builder/utils/build-workbook-rels-xml.js +55 -0
- package/build/esm/lib/workbook-builder/utils/build-workbook-xml.js +23 -0
- package/build/esm/lib/workbook-builder/utils/build-worksheet-xml.js +63 -0
- package/build/esm/lib/workbook-builder/utils/build-xml.js +69 -0
- package/build/esm/lib/workbook-builder/utils/constants.js +52 -0
- package/build/esm/lib/workbook-builder/utils/date-to-excel-serial.js +13 -0
- package/build/esm/lib/workbook-builder/utils/index.js +18 -0
- package/build/esm/lib/workbook-builder/utils/initialize-files.js +36 -0
- package/build/esm/lib/workbook-builder/utils/sheet.js +141 -0
- package/build/esm/lib/workbook-builder/utils/write-shared-strings-xml.js +43 -0
- package/build/esm/lib/workbook-builder/utils/write-styles-xml.js +157 -0
- package/build/esm/lib/workbook-builder/utils/write-worksheet-xml.js +203 -0
- package/build/esm/lib/workbook-builder/utils/write-xml.js +34 -0
- package/build/esm/lib/workbook-builder/workbook-builder.js +374 -0
- package/build/types/lib/template/utils/column-letter-to-index.d.ts +1 -0
- package/build/types/lib/template/utils/index.d.ts +1 -1
- package/build/types/lib/utils/index.d.ts +2 -0
- package/build/types/lib/utils/trim-and-join-multiline.d.ts +23 -0
- package/build/types/lib/workbook-builder/default/border.d.ts +7 -0
- package/build/types/lib/workbook-builder/default/cell-xf.d.ts +7 -0
- package/build/types/lib/workbook-builder/default/fill.d.ts +7 -0
- package/build/types/lib/workbook-builder/default/font.d.ts +21 -0
- package/build/types/lib/workbook-builder/default/index.d.ts +5 -0
- package/build/types/lib/workbook-builder/default/sheet-name.d.ts +6 -0
- package/build/types/lib/workbook-builder/index.d.ts +2 -0
- package/build/types/lib/workbook-builder/merge-cells/add.d.ts +15 -0
- package/build/types/lib/workbook-builder/merge-cells/helpers/index.d.ts +2 -0
- package/build/types/lib/workbook-builder/merge-cells/helpers/ranges-equal.d.ts +10 -0
- package/build/types/lib/workbook-builder/merge-cells/helpers/ranges-intersect.d.ts +10 -0
- package/build/types/lib/workbook-builder/merge-cells/index.d.ts +2 -0
- package/build/types/lib/workbook-builder/merge-cells/remove.d.ts +15 -0
- package/build/types/lib/workbook-builder/shared-string-ref/add.d.ts +13 -0
- package/build/types/lib/workbook-builder/shared-string-ref/index.d.ts +3 -0
- package/build/types/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.d.ts +10 -0
- package/build/types/lib/workbook-builder/shared-string-ref/remove.d.ts +13 -0
- package/build/types/lib/workbook-builder/style-ref/add-or-get.d.ts +16 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/add-num-fmt.d.ts +17 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/border-to-xml.d.ts +16 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/fill-to-xml.d.ts +17 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/font-to-xml.d.ts +18 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/index.d.ts +5 -0
- package/build/types/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.d.ts +15 -0
- package/build/types/lib/workbook-builder/style-ref/index.d.ts +3 -0
- package/build/types/lib/workbook-builder/style-ref/remove-all-from-sheet.d.ts +4 -0
- package/build/types/lib/workbook-builder/style-ref/remove.d.ts +18 -0
- package/build/types/lib/workbook-builder/types/app-xml-options.d.ts +9 -0
- package/build/types/lib/workbook-builder/types/border-style.d.ts +5 -0
- package/build/types/lib/workbook-builder/types/cell-data.d.ts +10 -0
- package/build/types/lib/workbook-builder/types/cell-style.d.ts +32 -0
- package/build/types/lib/workbook-builder/types/cell-type.d.ts +11 -0
- package/build/types/lib/workbook-builder/types/cell-value.d.ts +2 -0
- package/build/types/lib/workbook-builder/types/cell-xf.d.ts +13 -0
- package/build/types/lib/workbook-builder/types/index.d.ts +11 -0
- package/build/types/lib/workbook-builder/types/merge-cell.d.ts +6 -0
- package/build/types/lib/workbook-builder/types/row-data.d.ts +5 -0
- package/build/types/lib/workbook-builder/types/sheet-data.d.ts +13 -0
- package/build/types/lib/workbook-builder/types/xml-node.d.ts +11 -0
- package/build/types/lib/workbook-builder/utils/build-app-xml.d.ts +2 -0
- package/build/types/lib/workbook-builder/utils/build-cell-children.d.ts +9 -0
- package/build/types/lib/workbook-builder/utils/build-content-types-xml.d.ts +1 -0
- package/build/types/lib/workbook-builder/utils/build-core-xml.d.ts +1 -0
- package/build/types/lib/workbook-builder/utils/build-rels-xml.d.ts +1 -0
- package/build/types/lib/workbook-builder/utils/build-shared-strings-xml.d.ts +10 -0
- package/build/types/lib/workbook-builder/utils/build-styles-xml.d.ts +23 -0
- package/build/types/lib/workbook-builder/utils/build-theme-xml.d.ts +1 -0
- package/build/types/lib/workbook-builder/utils/build-workbook-rels-xml.d.ts +9 -0
- package/build/types/lib/workbook-builder/utils/build-workbook-xml.d.ts +3 -0
- package/build/types/lib/workbook-builder/utils/build-worksheet-xml.d.ts +2 -0
- package/build/types/lib/workbook-builder/utils/build-xml.d.ts +50 -0
- package/build/types/lib/workbook-builder/utils/constants.d.ts +47 -0
- package/build/types/lib/workbook-builder/utils/date-to-excel-serial.d.ts +9 -0
- package/build/types/lib/workbook-builder/utils/index.d.ts +18 -0
- package/build/types/lib/workbook-builder/utils/initialize-files.d.ts +13 -0
- package/build/types/lib/workbook-builder/utils/sheet.d.ts +21 -0
- package/build/types/lib/workbook-builder/utils/write-shared-strings-xml.d.ts +11 -0
- package/build/types/lib/workbook-builder/utils/write-styles-xml.d.ts +24 -0
- package/build/types/lib/workbook-builder/utils/write-worksheet-xml.d.ts +14 -0
- package/build/types/lib/workbook-builder/utils/write-xml.d.ts +3 -0
- package/build/types/lib/workbook-builder/workbook-builder.d.ts +110 -0
- package/package.json +1 -1
- /package/build/cjs/lib/{template/utils → utils}/escape-xml.js +0 -0
- /package/build/esm/lib/{template/utils → utils}/escape-xml.js +0 -0
- /package/build/types/lib/{template/utils → utils}/escape-xml.d.ts +0 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildWorkbookRels = buildWorkbookRels;
|
4
|
+
const constants_js_1 = require("./constants.js");
|
5
|
+
const build_xml_js_1 = require("./build-xml.js");
|
6
|
+
/**
|
7
|
+
* Builds the `_rels/workbook.xml.rels` content for the given number of sheets.
|
8
|
+
*
|
9
|
+
* Relationships are created for each worksheet and then for styles, theme, and shared strings.
|
10
|
+
*
|
11
|
+
* @param sheetsCount - Number of worksheets in the workbook
|
12
|
+
* @returns XML string for the workbook relationships part
|
13
|
+
*/
|
14
|
+
function buildWorkbookRels(sheetsCount) {
|
15
|
+
// Create relationships for each worksheet
|
16
|
+
const sheetRels = Array.from({ length: sheetsCount }, (_, i) => ({
|
17
|
+
attrs: {
|
18
|
+
Id: `rId${i + 1}`,
|
19
|
+
Target: `worksheets/sheet${i + 1}.xml`,
|
20
|
+
Type: constants_js_1.RELATIONSHIP_TYPES.WORKSHEET,
|
21
|
+
},
|
22
|
+
tag: "Relationship",
|
23
|
+
}));
|
24
|
+
// Ids for styles, theme, and sharedStrings follow after the worksheets
|
25
|
+
const stylesRel = {
|
26
|
+
attrs: {
|
27
|
+
Id: `rId${sheetsCount + 1}`,
|
28
|
+
Target: "styles.xml",
|
29
|
+
Type: constants_js_1.RELATIONSHIP_TYPES.STYLES,
|
30
|
+
},
|
31
|
+
tag: "Relationship",
|
32
|
+
};
|
33
|
+
const themeRel = {
|
34
|
+
attrs: {
|
35
|
+
Id: `rId${sheetsCount + 2}`,
|
36
|
+
Target: "theme/theme1.xml",
|
37
|
+
Type: constants_js_1.RELATIONSHIP_TYPES.THEME,
|
38
|
+
},
|
39
|
+
tag: "Relationship",
|
40
|
+
};
|
41
|
+
const sharedStringsRel = {
|
42
|
+
attrs: {
|
43
|
+
Id: `rId${sheetsCount + 3}`,
|
44
|
+
Target: "sharedStrings.xml",
|
45
|
+
Type: constants_js_1.RELATIONSHIP_TYPES.SHARED_STRINGS,
|
46
|
+
},
|
47
|
+
tag: "Relationship",
|
48
|
+
};
|
49
|
+
const allRels = [...sheetRels, stylesRel, themeRel, sharedStringsRel];
|
50
|
+
return [
|
51
|
+
constants_js_1.XML_DECLARATION,
|
52
|
+
(0, build_xml_js_1.buildXml)({
|
53
|
+
attrs: { xmlns: constants_js_1.XML_NAMESPACES.PACKAGE_RELATIONSHIPS },
|
54
|
+
children: allRels,
|
55
|
+
tag: "Relationships",
|
56
|
+
}),
|
57
|
+
].join("\n");
|
58
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildWorkbookXml = buildWorkbookXml;
|
4
|
+
const constants_js_1 = require("./constants.js");
|
5
|
+
const build_xml_js_1 = require("./build-xml.js");
|
6
|
+
function buildWorkbookXml(sheets) {
|
7
|
+
return [
|
8
|
+
constants_js_1.XML_DECLARATION,
|
9
|
+
(0, build_xml_js_1.buildXml)({
|
10
|
+
attrs: {
|
11
|
+
xmlns: constants_js_1.XML_NAMESPACES.SPREADSHEET_ML,
|
12
|
+
"xmlns:r": constants_js_1.XML_NAMESPACES.OFFICE_DOCUMENT,
|
13
|
+
},
|
14
|
+
children: [
|
15
|
+
{
|
16
|
+
children: sheets.map((sheet, i) => ({
|
17
|
+
attrs: { name: sheet.name, "r:id": `rId${i + 1}`, sheetId: (i + 1).toString() },
|
18
|
+
tag: "sheet",
|
19
|
+
})),
|
20
|
+
tag: "sheets",
|
21
|
+
},
|
22
|
+
],
|
23
|
+
tag: "workbook",
|
24
|
+
}),
|
25
|
+
].join("\n");
|
26
|
+
}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildWorksheetXml = buildWorksheetXml;
|
4
|
+
const constants_js_1 = require("./constants.js");
|
5
|
+
const build_cell_children_js_1 = require("./build-cell-children.js");
|
6
|
+
const build_xml_js_1 = require("./build-xml.js");
|
7
|
+
function buildWorksheetXml(rows = new Map(), merges = []) {
|
8
|
+
const typeSetSkipping = new Set(["n"]);
|
9
|
+
const children = [
|
10
|
+
{
|
11
|
+
attrs: { ref: "A1:A1" },
|
12
|
+
tag: "dimension",
|
13
|
+
},
|
14
|
+
{
|
15
|
+
children: [{ attrs: { workbookViewId: "0" }, tag: "sheetView" }],
|
16
|
+
tag: "sheetViews",
|
17
|
+
},
|
18
|
+
{
|
19
|
+
attrs: { defaultRowHeight: "15" },
|
20
|
+
tag: "sheetFormatPr",
|
21
|
+
},
|
22
|
+
{
|
23
|
+
children: Array.from(rows.entries()).map(([rowNumber, row]) => ({
|
24
|
+
attrs: { r: rowNumber.toString() },
|
25
|
+
children: Array.from(row.cells.entries()).map(([colNumber, cell]) => {
|
26
|
+
const cellRef = `${colNumber}${rowNumber}`;
|
27
|
+
const attrT = (cell.type && typeSetSkipping.has(cell.type))
|
28
|
+
? undefined
|
29
|
+
: cell.type;
|
30
|
+
return {
|
31
|
+
attrs: {
|
32
|
+
r: cellRef,
|
33
|
+
s: cell.style?.index,
|
34
|
+
t: attrT,
|
35
|
+
},
|
36
|
+
children: (0, build_cell_children_js_1.buildCellChildren)(cell),
|
37
|
+
tag: "c",
|
38
|
+
};
|
39
|
+
}),
|
40
|
+
tag: "row",
|
41
|
+
})),
|
42
|
+
tag: "sheetData",
|
43
|
+
},
|
44
|
+
];
|
45
|
+
if (merges.length > 0) {
|
46
|
+
children.push({
|
47
|
+
attrs: { count: merges.length.toString() },
|
48
|
+
children: merges.map((ref) => ({
|
49
|
+
attrs: { ref },
|
50
|
+
tag: "mergeCell",
|
51
|
+
})),
|
52
|
+
tag: "mergeCells",
|
53
|
+
});
|
54
|
+
}
|
55
|
+
return [
|
56
|
+
constants_js_1.XML_DECLARATION,
|
57
|
+
(0, build_xml_js_1.buildXml)({
|
58
|
+
attrs: {
|
59
|
+
xmlns: constants_js_1.XML_NAMESPACES.SPREADSHEET_ML,
|
60
|
+
"xmlns:r": constants_js_1.XML_NAMESPACES.OFFICE_DOCUMENT,
|
61
|
+
},
|
62
|
+
children,
|
63
|
+
tag: "worksheet",
|
64
|
+
}),
|
65
|
+
].join("\n");
|
66
|
+
}
|
@@ -0,0 +1,72 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.buildXml = buildXml;
|
4
|
+
/**
|
5
|
+
* Builds XML string from an XmlNode structure.
|
6
|
+
*
|
7
|
+
* @param node - The XML node to convert to string
|
8
|
+
* @param level - The indentation level for formatting (default: 0)
|
9
|
+
*
|
10
|
+
* @returns The formatted XML string
|
11
|
+
*/
|
12
|
+
function buildXml(node, level = 0) {
|
13
|
+
const { attrs = {}, children = [], tag } = node;
|
14
|
+
const attrStr = Object.entries(attrs)
|
15
|
+
.filter(attr => ((attr[1] !== undefined) && (attr[1] !== null)))
|
16
|
+
.map(([k, v]) => ` ${k}="${v}"`)
|
17
|
+
.join("");
|
18
|
+
const gap = " ".repeat(level);
|
19
|
+
// No children → self-closing tag
|
20
|
+
if (!children.length) {
|
21
|
+
return `${gap}<${tag}${attrStr}/>`;
|
22
|
+
}
|
23
|
+
// Single text child → inline formatting
|
24
|
+
if (children.length === 1 && typeof children[0] === "string" && !children[0].includes("<")) {
|
25
|
+
return `${gap}<${tag}${attrStr}>${children[0].trimEnd()}</${tag}>`;
|
26
|
+
}
|
27
|
+
// Has children → recursive rendering
|
28
|
+
const inner = children
|
29
|
+
.map(c => typeof c === "string" ? `${" ".repeat(level + 1)}${c.trimEnd()}` : buildXml(c, level + 1))
|
30
|
+
.join("\n");
|
31
|
+
return `${gap}<${tag}${attrStr}>\n${inner}\n${gap}</${tag}>`;
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Cell XML Structure Documentation
|
35
|
+
*
|
36
|
+
* Reference: https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
|
37
|
+
*
|
38
|
+
* Parent Elements:
|
39
|
+
* - row
|
40
|
+
*
|
41
|
+
* Child Elements:
|
42
|
+
* - extLst (Future Feature Data Storage Area)
|
43
|
+
* - f (Formula)
|
44
|
+
* - is (Rich Text Inline)
|
45
|
+
* - v (Cell Value)
|
46
|
+
*
|
47
|
+
* Attributes:
|
48
|
+
* - cm (Cell Metadata Index): The zero-based index of the cell metadata record associated with this cell.
|
49
|
+
* Metadata information is found in the Metadata Part. Cell metadata is extra information stored at the
|
50
|
+
* cell level, and is attached to the cell (travels through moves, copy / paste, clear, etc).
|
51
|
+
* Cell metadata is not accessible via formula reference.
|
52
|
+
* The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
|
53
|
+
*
|
54
|
+
* - ph (Show Phonetic): A Boolean value indicating if the spreadsheet application should show phonetic
|
55
|
+
* information. Phonetic information is displayed in the same cell across the top of the cell and serves
|
56
|
+
* as a 'hint' which indicates how the text should be pronounced. This should only be used for East Asian languages.
|
57
|
+
* The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
|
58
|
+
*
|
59
|
+
* - r (Reference): An A1 style reference to the location of this cell
|
60
|
+
* The possible values for this attribute are defined by the ST_CellRef simple type.
|
61
|
+
*
|
62
|
+
* - s (Style Index): The index of this cell's style. Style records are stored in the Styles Part.
|
63
|
+
* The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
|
64
|
+
*
|
65
|
+
* - t (Cell Data Type): An enumeration representing the cell's data type.
|
66
|
+
* The possible values for this attribute are defined by the ST_CellType simple type.
|
67
|
+
*
|
68
|
+
* - vm (Value Metadata Index): The zero-based index of the value metadata record associated with this cell's value.
|
69
|
+
* Metadata records are stored in the Metadata Part. Value metadata is extra information stored at the cell level,
|
70
|
+
* but associated with the value rather than the cell itself. Value metadata is accessible via formula reference.
|
71
|
+
* The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
|
72
|
+
*/
|
@@ -0,0 +1,55 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.XML_NAMESPACES = exports.XML_DECLARATION = exports.RELATIONSHIP_TYPES = exports.FILE_PATHS = exports.CONTENT_TYPES = void 0;
|
4
|
+
// Content Types
|
5
|
+
exports.CONTENT_TYPES = {
|
6
|
+
APP: "application/vnd.openxmlformats-officedocument.extended-properties+xml",
|
7
|
+
CORE: "application/vnd.openxmlformats-package.core-properties+xml",
|
8
|
+
RELATIONSHIPS: "application/vnd.openxmlformats-package.relationships+xml",
|
9
|
+
SHARED_STRINGS: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
|
10
|
+
STYLES: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
|
11
|
+
THEME: "application/vnd.openxmlformats-officedocument.theme+xml",
|
12
|
+
WORKBOOK: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
|
13
|
+
WORKSHEET: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
|
14
|
+
XML: "application/xml",
|
15
|
+
};
|
16
|
+
// Default file paths
|
17
|
+
exports.FILE_PATHS = {
|
18
|
+
APP: "docProps/app.xml",
|
19
|
+
CONTENT_TYPES: "[Content_Types].xml",
|
20
|
+
CORE: "docProps/core.xml",
|
21
|
+
RELS: "_rels/.rels",
|
22
|
+
SHARED_STRINGS: "xl/sharedStrings.xml",
|
23
|
+
STYLES: "xl/styles.xml",
|
24
|
+
THEME: "xl/theme/theme1.xml",
|
25
|
+
WORKBOOK: "xl/workbook.xml",
|
26
|
+
WORKBOOK_RELS: "xl/_rels/workbook.xml.rels",
|
27
|
+
WORKSHEET: "xl/worksheets/sheet1.xml",
|
28
|
+
};
|
29
|
+
// Relationship Types
|
30
|
+
exports.RELATIONSHIP_TYPES = {
|
31
|
+
APP: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
|
32
|
+
CORE: "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
|
33
|
+
OFFICE_DOCUMENT: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
34
|
+
SHARED_STRINGS: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
|
35
|
+
STYLES: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
36
|
+
THEME: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
|
37
|
+
WORKSHEET: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
|
38
|
+
};
|
39
|
+
// XML Declarations
|
40
|
+
exports.XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
|
41
|
+
// XML Namespaces
|
42
|
+
exports.XML_NAMESPACES = {
|
43
|
+
CONTENT_TYPES: "http://schemas.openxmlformats.org/package/2006/content-types",
|
44
|
+
CORE_PROPERTIES: "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
|
45
|
+
DC: "http://purl.org/dc/elements/1.1/",
|
46
|
+
DCMITYPE: "http://purl.org/dc/dcmitype/",
|
47
|
+
DCTERMS: "http://purl.org/dc/terms/",
|
48
|
+
DOC_PROPS_VTYPES: "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
|
49
|
+
DRAWINGML: "http://schemas.openxmlformats.org/drawingml/2006/main",
|
50
|
+
EXTENDED_PROPERTIES: "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties",
|
51
|
+
OFFICE_DOCUMENT: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
52
|
+
PACKAGE_RELATIONSHIPS: "http://schemas.openxmlformats.org/package/2006/relationships",
|
53
|
+
SPREADSHEET_ML: "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
|
54
|
+
XSI: "http://www.w3.org/2001/XMLSchema-instance",
|
55
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.dateToExcelSerial = dateToExcelSerial;
|
4
|
+
/**
|
5
|
+
* Converts a JavaScript Date object to Excel serial number format.
|
6
|
+
* Excel stores dates as serial numbers where 1 represents January 1, 1900.
|
7
|
+
*
|
8
|
+
* @param date - The JavaScript Date object to convert
|
9
|
+
*
|
10
|
+
* @returns The Excel serial number as a floating-point number
|
11
|
+
*/
|
12
|
+
function dateToExcelSerial(date) {
|
13
|
+
const msPerDay = 24 * 60 * 60 * 1000;
|
14
|
+
const excelEpoch = Date.UTC(1899, 11, 30); // Excel "day 0"
|
15
|
+
return (date.getTime() - excelEpoch) / msPerDay;
|
16
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./build-app-xml.js"), exports);
|
18
|
+
__exportStar(require("./build-cell-children.js"), exports);
|
19
|
+
__exportStar(require("./build-content-types-xml.js"), exports);
|
20
|
+
__exportStar(require("./build-shared-strings-xml.js"), exports);
|
21
|
+
__exportStar(require("./build-styles-xml.js"), exports);
|
22
|
+
__exportStar(require("./build-theme-xml.js"), exports);
|
23
|
+
__exportStar(require("./build-workbook-rels-xml.js"), exports);
|
24
|
+
__exportStar(require("./build-workbook-xml.js"), exports);
|
25
|
+
__exportStar(require("./build-worksheet-xml.js"), exports);
|
26
|
+
__exportStar(require("./build-xml.js"), exports);
|
27
|
+
__exportStar(require("./constants.js"), exports);
|
28
|
+
__exportStar(require("./date-to-excel-serial.js"), exports);
|
29
|
+
__exportStar(require("./initialize-files.js"), exports);
|
30
|
+
__exportStar(require("./sheet.js"), exports);
|
31
|
+
__exportStar(require("./write-shared-strings-xml.js"), exports);
|
32
|
+
__exportStar(require("./write-styles-xml.js"), exports);
|
33
|
+
__exportStar(require("./write-worksheet-xml.js"), exports);
|
34
|
+
__exportStar(require("./write-xml.js"), exports);
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.initializeFiles = void 0;
|
4
|
+
const build_app_xml_js_1 = require("./build-app-xml.js");
|
5
|
+
const build_content_types_xml_js_1 = require("./build-content-types-xml.js");
|
6
|
+
const build_core_xml_js_1 = require("./build-core-xml.js");
|
7
|
+
const build_rels_xml_js_1 = require("./build-rels-xml.js");
|
8
|
+
const build_shared_strings_xml_js_1 = require("./build-shared-strings-xml.js");
|
9
|
+
const build_styles_xml_js_1 = require("./build-styles-xml.js");
|
10
|
+
const build_theme_xml_js_1 = require("./build-theme-xml.js");
|
11
|
+
const build_workbook_rels_xml_js_1 = require("./build-workbook-rels-xml.js");
|
12
|
+
const build_workbook_xml_js_1 = require("./build-workbook-xml.js");
|
13
|
+
const build_worksheet_xml_js_1 = require("./build-worksheet-xml.js");
|
14
|
+
const constants_js_1 = require("./constants.js");
|
15
|
+
const initializeFiles = (sheetName) => {
|
16
|
+
const sheetsCount = 1;
|
17
|
+
const contentTypesXml = (0, build_content_types_xml_js_1.buildContentTypesXml)(sheetsCount);
|
18
|
+
const relsXml = (0, build_rels_xml_js_1.buildRelsXml)();
|
19
|
+
const appXml = (0, build_app_xml_js_1.buildAppXml)({ sheetNames: [sheetName] });
|
20
|
+
const coreXml = (0, build_core_xml_js_1.buildCoreXml)();
|
21
|
+
const workbookRelsXml = (0, build_workbook_rels_xml_js_1.buildWorkbookRels)(sheetsCount);
|
22
|
+
const stylesXml = (0, build_styles_xml_js_1.buildStylesXml)();
|
23
|
+
const sharedStringsXml = (0, build_shared_strings_xml_js_1.buildSharedStringsXml)();
|
24
|
+
const themeXml = (0, build_theme_xml_js_1.buildThemeXml)();
|
25
|
+
const workbookXml = (0, build_workbook_xml_js_1.buildWorkbookXml)([{ name: sheetName }]);
|
26
|
+
const worksheetXml = (0, build_worksheet_xml_js_1.buildWorksheetXml)();
|
27
|
+
return {
|
28
|
+
[constants_js_1.FILE_PATHS.CONTENT_TYPES]: contentTypesXml,
|
29
|
+
[constants_js_1.FILE_PATHS.RELS]: relsXml,
|
30
|
+
[constants_js_1.FILE_PATHS.APP]: appXml,
|
31
|
+
[constants_js_1.FILE_PATHS.CORE]: coreXml,
|
32
|
+
[constants_js_1.FILE_PATHS.WORKBOOK_RELS]: workbookRelsXml,
|
33
|
+
[constants_js_1.FILE_PATHS.STYLES]: stylesXml,
|
34
|
+
[constants_js_1.FILE_PATHS.SHARED_STRINGS]: sharedStringsXml,
|
35
|
+
[constants_js_1.FILE_PATHS.THEME]: themeXml,
|
36
|
+
[constants_js_1.FILE_PATHS.WORKBOOK]: workbookXml,
|
37
|
+
[constants_js_1.FILE_PATHS.WORKSHEET]: worksheetXml,
|
38
|
+
};
|
39
|
+
};
|
40
|
+
exports.initializeFiles = initializeFiles;
|
@@ -0,0 +1,144 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createSheet = createSheet;
|
4
|
+
const index_js_1 = require("../../template/utils/index.js");
|
5
|
+
const date_to_excel_serial_js_1 = require("./date-to-excel-serial.js");
|
6
|
+
/** Maximum number of columns supported by Excel (XFD). */
|
7
|
+
const MAX_COLUMNS = 16384;
|
8
|
+
/** Maximum number of rows supported by Excel (1,048,576). */
|
9
|
+
const MAX_ROWS = 1_048_576;
|
10
|
+
/**
|
11
|
+
* Factory for creating a new sheet with bound helpers.
|
12
|
+
*
|
13
|
+
* @param name - Sheet name
|
14
|
+
* @param fn.addMerge - Function to add a merge range (bound to workbook)
|
15
|
+
* @param fn.removeMerge - Function to remove a merge range (bound to workbook)
|
16
|
+
* @param fn.addOrGetStyle - Function to add or get a style index
|
17
|
+
* @param fn.addSharedString - Function to add a shared string and return its index
|
18
|
+
* @returns SheetData instance with helpers
|
19
|
+
*/
|
20
|
+
function createSheet(name, fn) {
|
21
|
+
const { addMerge, addOrGetStyle, addSharedString, removeMerge, } = fn;
|
22
|
+
const rows = new Map();
|
23
|
+
return {
|
24
|
+
name,
|
25
|
+
rows,
|
26
|
+
addMerge(mergeCell) {
|
27
|
+
return addMerge({ ...mergeCell, sheetName: name });
|
28
|
+
},
|
29
|
+
removeMerge(mergeCell) {
|
30
|
+
return removeMerge({ ...mergeCell, sheetName: name });
|
31
|
+
},
|
32
|
+
setCell(rowIndex, column, cell) {
|
33
|
+
if (rowIndex <= 0) {
|
34
|
+
throw new Error("Invalid rowIndex");
|
35
|
+
}
|
36
|
+
if (!Number.isInteger(rowIndex) || rowIndex <= 0) {
|
37
|
+
throw new Error("Invalid rowIndex: must be a positive integer");
|
38
|
+
}
|
39
|
+
if (rowIndex > MAX_ROWS) {
|
40
|
+
throw new Error(`Invalid rowIndex: exceeds Excel max rows (${MAX_ROWS})`);
|
41
|
+
}
|
42
|
+
if (!rows.has(rowIndex)) {
|
43
|
+
rows.set(rowIndex, { cells: new Map() });
|
44
|
+
}
|
45
|
+
const letterColumn = typeof column === "number"
|
46
|
+
? (0, index_js_1.columnIndexToLetter)(column)
|
47
|
+
: column;
|
48
|
+
if (!isValidColumn(letterColumn)) {
|
49
|
+
throw new Error(`Invalid column string: "${letterColumn}"`);
|
50
|
+
}
|
51
|
+
// if is Date
|
52
|
+
if (cell.value instanceof Date) {
|
53
|
+
cell.value = (0, date_to_excel_serial_js_1.dateToExcelSerial)(cell.value);
|
54
|
+
}
|
55
|
+
if (cell.isFormula) {
|
56
|
+
cell.type = undefined;
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
if (cell.type === "str") {
|
60
|
+
throw new Error(`Cell type: "${cell.type}" valid only for formula cells`);
|
61
|
+
}
|
62
|
+
// If type is not provided — detect automatically
|
63
|
+
cell.type = detectCellType(cell.value, cell.type);
|
64
|
+
}
|
65
|
+
// Handle shared string
|
66
|
+
if (cell.type === "s") {
|
67
|
+
const idx = addSharedString(String(cell.value ?? ""), name);
|
68
|
+
cell = { ...cell, value: idx };
|
69
|
+
}
|
70
|
+
if (cell.style) {
|
71
|
+
const styleIndex = addOrGetStyle(cell.style, name);
|
72
|
+
cell.style.index = styleIndex;
|
73
|
+
}
|
74
|
+
rows.get(rowIndex)?.cells.set(letterColumn, cell);
|
75
|
+
},
|
76
|
+
getCell(rowIndex, column) {
|
77
|
+
if (typeof column === "number") {
|
78
|
+
if (column < 0 || column > MAX_COLUMNS) {
|
79
|
+
throw new Error("Invalid column number");
|
80
|
+
}
|
81
|
+
return rows.get(rowIndex)?.cells.get((0, index_js_1.columnIndexToLetter)(column));
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
if (!isValidColumn(column)) {
|
85
|
+
throw new Error(`Invalid column string: "${column}"`);
|
86
|
+
}
|
87
|
+
return rows.get(rowIndex)?.cells.get(column);
|
88
|
+
}
|
89
|
+
},
|
90
|
+
removeCell(rowIndex, column) {
|
91
|
+
if (rowIndex <= 0) {
|
92
|
+
throw new Error("Invalid rowIndex");
|
93
|
+
}
|
94
|
+
if (!Number.isInteger(rowIndex) || rowIndex <= 0) {
|
95
|
+
throw new Error("Invalid rowIndex: must be a positive integer");
|
96
|
+
}
|
97
|
+
if (rowIndex > MAX_ROWS) {
|
98
|
+
throw new Error(`Invalid rowIndex: exceeds Excel max rows (${MAX_ROWS})`);
|
99
|
+
}
|
100
|
+
const letterColumn = typeof column === "number"
|
101
|
+
? (0, index_js_1.columnIndexToLetter)(column)
|
102
|
+
: column;
|
103
|
+
if (!isValidColumn(letterColumn)) {
|
104
|
+
throw new Error(`Invalid column string: "${letterColumn}"`);
|
105
|
+
}
|
106
|
+
return rows.get(rowIndex)?.cells.delete(letterColumn) ?? false;
|
107
|
+
},
|
108
|
+
};
|
109
|
+
}
|
110
|
+
/** Validates an Excel column string (A-Z, AA, ..., XFD). */
|
111
|
+
function isValidColumn(column) {
|
112
|
+
if (!/^[A-Z]+$/.test(column))
|
113
|
+
return false;
|
114
|
+
const idx = (0, index_js_1.columnLetterToIndex)(column);
|
115
|
+
return idx > 0 && idx <= MAX_COLUMNS;
|
116
|
+
}
|
117
|
+
/**
|
118
|
+
* Detects the appropriate cell type based on the value when not explicitly specified.
|
119
|
+
*
|
120
|
+
* @param value - Cell value
|
121
|
+
* @param explicitType - Explicitly provided type, if any
|
122
|
+
* @returns CellType inferred or the explicit type
|
123
|
+
*/
|
124
|
+
function detectCellType(value, explicitType) {
|
125
|
+
if (explicitType) {
|
126
|
+
return explicitType;
|
127
|
+
}
|
128
|
+
if (value === null || value === undefined) {
|
129
|
+
// For empty cells we default to numeric type with empty value
|
130
|
+
return "n";
|
131
|
+
}
|
132
|
+
if (typeof value === "number") {
|
133
|
+
return "n";
|
134
|
+
}
|
135
|
+
if (typeof value === "boolean") {
|
136
|
+
return "b";
|
137
|
+
}
|
138
|
+
if (typeof value === "string") {
|
139
|
+
// Default to inlineStr for plain strings
|
140
|
+
return "inlineStr";
|
141
|
+
}
|
142
|
+
// Fallback to inlineStr
|
143
|
+
return "inlineStr";
|
144
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.writeSharedStringsXml = writeSharedStringsXml;
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
8
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
10
|
+
const index_js_1 = require("../../utils/index.js");
|
11
|
+
const constants_js_1 = require("./constants.js");
|
12
|
+
/**
|
13
|
+
* Writes the `sharedStrings.xml` content to a file at the given destination.
|
14
|
+
*
|
15
|
+
* Uses a file write stream with backpressure control to avoid buffering large
|
16
|
+
* content in memory.
|
17
|
+
*
|
18
|
+
* @param destination - Absolute or relative file path to write
|
19
|
+
* @param strings - Array of unique strings used in the workbook
|
20
|
+
* @returns Promise that resolves when the write stream finishes
|
21
|
+
*/
|
22
|
+
async function writeSharedStringsXml(destination, strings = []) {
|
23
|
+
// Ensure destination folder exists
|
24
|
+
await promises_1.default.mkdir(node_path_1.default.dirname(destination), { recursive: true });
|
25
|
+
const stream = node_fs_1.default.createWriteStream(destination, { encoding: "utf-8" });
|
26
|
+
try {
|
27
|
+
// Document header
|
28
|
+
stream.write(constants_js_1.XML_DECLARATION + "\n");
|
29
|
+
stream.write(`<sst xmlns="${constants_js_1.XML_NAMESPACES.SPREADSHEET_ML}" count="${strings.length}" uniqueCount="${strings.length}">\n`);
|
30
|
+
// Main string items
|
31
|
+
for (const s of strings) {
|
32
|
+
const preserve = /^\s|\s$/.test(s) ? " xml:space=\"preserve\"" : "";
|
33
|
+
const siXml = `<si><t${preserve}>${(0, index_js_1.escapeXml)(s)}</t></si>\n`;
|
34
|
+
// Write with backpressure control
|
35
|
+
if (!stream.write(siXml)) {
|
36
|
+
await new Promise(resolve => stream.once("drain", () => resolve()));
|
37
|
+
}
|
38
|
+
}
|
39
|
+
// Closing tag
|
40
|
+
stream.write("</sst>");
|
41
|
+
}
|
42
|
+
finally {
|
43
|
+
stream.end();
|
44
|
+
}
|
45
|
+
return new Promise((resolve, reject) => {
|
46
|
+
stream.on("error", reject);
|
47
|
+
stream.on("finish", resolve);
|
48
|
+
});
|
49
|
+
}
|