@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.
Files changed (207) hide show
  1. package/build/cjs/lib/template/template-fs.js +2 -1
  2. package/build/cjs/lib/template/template-memory.js +2 -1
  3. package/build/cjs/lib/template/utils/column-letter-to-index.js +15 -0
  4. package/build/cjs/lib/template/utils/index.js +1 -1
  5. package/build/cjs/lib/template/utils/prepare-row-to-cells.js +2 -2
  6. package/build/cjs/lib/template/utils/validate-worksheet-xml.js +19 -9
  7. package/build/cjs/lib/utils/index.js +2 -0
  8. package/build/cjs/lib/utils/trim-and-join-multiline.js +32 -0
  9. package/build/cjs/lib/workbook-builder/default/border.js +18 -0
  10. package/build/cjs/lib/workbook-builder/default/cell-xf.js +15 -0
  11. package/build/cjs/lib/workbook-builder/default/fill.js +18 -0
  12. package/build/cjs/lib/workbook-builder/default/font.js +19 -0
  13. package/build/cjs/lib/workbook-builder/default/index.js +21 -0
  14. package/build/cjs/lib/workbook-builder/default/sheet-name.js +10 -0
  15. package/build/cjs/lib/workbook-builder/index.js +18 -0
  16. package/build/cjs/lib/workbook-builder/merge-cells/add.js +72 -0
  17. package/build/cjs/lib/workbook-builder/merge-cells/helpers/index.js +18 -0
  18. package/build/cjs/lib/workbook-builder/merge-cells/helpers/ranges-equal.js +17 -0
  19. package/build/cjs/lib/workbook-builder/merge-cells/helpers/ranges-intersect.js +17 -0
  20. package/build/cjs/lib/workbook-builder/merge-cells/index.js +18 -0
  21. package/build/cjs/lib/workbook-builder/merge-cells/remove.js +60 -0
  22. package/build/cjs/lib/workbook-builder/shared-string-ref/add.js +24 -0
  23. package/build/cjs/lib/workbook-builder/shared-string-ref/index.js +19 -0
  24. package/build/cjs/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.js +66 -0
  25. package/build/cjs/lib/workbook-builder/shared-string-ref/remove.js +66 -0
  26. package/build/cjs/lib/workbook-builder/style-ref/add-or-get.js +94 -0
  27. package/build/cjs/lib/workbook-builder/style-ref/helpers/add-num-fmt.js +25 -0
  28. package/build/cjs/lib/workbook-builder/style-ref/helpers/border-to-xml.js +49 -0
  29. package/build/cjs/lib/workbook-builder/style-ref/helpers/fill-to-xml.js +51 -0
  30. package/build/cjs/lib/workbook-builder/style-ref/helpers/font-to-xml.js +112 -0
  31. package/build/cjs/lib/workbook-builder/style-ref/helpers/index.js +21 -0
  32. package/build/cjs/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.js +30 -0
  33. package/build/cjs/lib/workbook-builder/style-ref/index.js +19 -0
  34. package/build/cjs/lib/workbook-builder/style-ref/remove-all-from-sheet.js +26 -0
  35. package/build/cjs/lib/workbook-builder/style-ref/remove.js +52 -0
  36. package/build/cjs/lib/workbook-builder/types/app-xml-options.js +2 -0
  37. package/build/cjs/lib/workbook-builder/types/border-style.js +2 -0
  38. package/build/cjs/lib/workbook-builder/types/cell-data.js +2 -0
  39. package/build/cjs/lib/workbook-builder/types/cell-style.js +2 -0
  40. package/build/cjs/lib/workbook-builder/types/cell-type.js +2 -0
  41. package/build/cjs/lib/workbook-builder/types/cell-value.js +2 -0
  42. package/build/cjs/lib/workbook-builder/types/cell-xf.js +2 -0
  43. package/build/cjs/lib/workbook-builder/types/index.js +27 -0
  44. package/build/cjs/lib/workbook-builder/types/merge-cell.js +2 -0
  45. package/build/cjs/lib/workbook-builder/types/row-data.js +2 -0
  46. package/build/cjs/lib/workbook-builder/types/sheet-data.js +2 -0
  47. package/build/cjs/lib/workbook-builder/types/xml-node.js +2 -0
  48. package/build/cjs/lib/workbook-builder/utils/build-app-xml.js +51 -0
  49. package/build/cjs/lib/workbook-builder/utils/build-cell-children.js +59 -0
  50. package/build/cjs/lib/workbook-builder/utils/build-content-types-xml.js +42 -0
  51. package/build/cjs/lib/workbook-builder/utils/build-core-xml.js +27 -0
  52. package/build/cjs/lib/workbook-builder/utils/build-rels-xml.js +19 -0
  53. package/build/cjs/lib/workbook-builder/utils/build-shared-strings-xml.js +39 -0
  54. package/build/cjs/lib/workbook-builder/utils/build-styles-xml.js +178 -0
  55. package/build/cjs/lib/workbook-builder/utils/build-theme-xml.js +609 -0
  56. package/build/cjs/lib/workbook-builder/utils/build-workbook-rels-xml.js +58 -0
  57. package/build/cjs/lib/workbook-builder/utils/build-workbook-xml.js +26 -0
  58. package/build/cjs/lib/workbook-builder/utils/build-worksheet-xml.js +66 -0
  59. package/build/cjs/lib/workbook-builder/utils/build-xml.js +72 -0
  60. package/build/cjs/lib/workbook-builder/utils/constants.js +55 -0
  61. package/build/cjs/lib/workbook-builder/utils/date-to-excel-serial.js +16 -0
  62. package/build/cjs/lib/workbook-builder/utils/index.js +34 -0
  63. package/build/cjs/lib/workbook-builder/utils/initialize-files.js +40 -0
  64. package/build/cjs/lib/workbook-builder/utils/sheet.js +144 -0
  65. package/build/cjs/lib/workbook-builder/utils/write-shared-strings-xml.js +49 -0
  66. package/build/cjs/lib/workbook-builder/utils/write-styles-xml.js +196 -0
  67. package/build/cjs/lib/workbook-builder/utils/write-worksheet-xml.js +209 -0
  68. package/build/cjs/lib/workbook-builder/utils/write-xml.js +37 -0
  69. package/build/cjs/lib/workbook-builder/workbook-builder.js +414 -0
  70. package/build/esm/lib/template/template-fs.js +2 -1
  71. package/build/esm/lib/template/template-memory.js +2 -1
  72. package/build/esm/lib/template/utils/column-letter-to-index.js +12 -0
  73. package/build/esm/lib/template/utils/index.js +1 -1
  74. package/build/esm/lib/template/utils/prepare-row-to-cells.js +1 -1
  75. package/build/esm/lib/template/utils/validate-worksheet-xml.js +19 -9
  76. package/build/esm/lib/utils/index.js +2 -0
  77. package/build/esm/lib/utils/trim-and-join-multiline.js +29 -0
  78. package/build/esm/lib/workbook-builder/default/border.js +14 -0
  79. package/build/esm/lib/workbook-builder/default/cell-xf.js +11 -0
  80. package/build/esm/lib/workbook-builder/default/fill.js +14 -0
  81. package/build/esm/lib/workbook-builder/default/font.js +15 -0
  82. package/build/esm/lib/workbook-builder/default/index.js +5 -0
  83. package/build/esm/lib/workbook-builder/default/sheet-name.js +6 -0
  84. package/build/esm/lib/workbook-builder/index.js +2 -0
  85. package/build/esm/lib/workbook-builder/merge-cells/add.js +36 -0
  86. package/build/esm/lib/workbook-builder/merge-cells/helpers/index.js +2 -0
  87. package/build/esm/lib/workbook-builder/merge-cells/helpers/ranges-equal.js +14 -0
  88. package/build/esm/lib/workbook-builder/merge-cells/helpers/ranges-intersect.js +14 -0
  89. package/build/esm/lib/workbook-builder/merge-cells/index.js +2 -0
  90. package/build/esm/lib/workbook-builder/merge-cells/remove.js +24 -0
  91. package/build/esm/lib/workbook-builder/shared-string-ref/add.js +21 -0
  92. package/build/esm/lib/workbook-builder/shared-string-ref/index.js +3 -0
  93. package/build/esm/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.js +63 -0
  94. package/build/esm/lib/workbook-builder/shared-string-ref/remove.js +63 -0
  95. package/build/esm/lib/workbook-builder/style-ref/add-or-get.js +58 -0
  96. package/build/esm/lib/workbook-builder/style-ref/helpers/add-num-fmt.js +21 -0
  97. package/build/esm/lib/workbook-builder/style-ref/helpers/border-to-xml.js +45 -0
  98. package/build/esm/lib/workbook-builder/style-ref/helpers/fill-to-xml.js +47 -0
  99. package/build/esm/lib/workbook-builder/style-ref/helpers/font-to-xml.js +75 -0
  100. package/build/esm/lib/workbook-builder/style-ref/helpers/index.js +5 -0
  101. package/build/esm/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.js +26 -0
  102. package/build/esm/lib/workbook-builder/style-ref/index.js +3 -0
  103. package/build/esm/lib/workbook-builder/style-ref/remove-all-from-sheet.js +23 -0
  104. package/build/esm/lib/workbook-builder/style-ref/remove.js +49 -0
  105. package/build/esm/lib/workbook-builder/types/app-xml-options.js +1 -0
  106. package/build/esm/lib/workbook-builder/types/border-style.js +1 -0
  107. package/build/esm/lib/workbook-builder/types/cell-data.js +1 -0
  108. package/build/esm/lib/workbook-builder/types/cell-style.js +1 -0
  109. package/build/esm/lib/workbook-builder/types/cell-type.js +1 -0
  110. package/build/esm/lib/workbook-builder/types/cell-value.js +1 -0
  111. package/build/esm/lib/workbook-builder/types/cell-xf.js +1 -0
  112. package/build/esm/lib/workbook-builder/types/index.js +11 -0
  113. package/build/esm/lib/workbook-builder/types/merge-cell.js +1 -0
  114. package/build/esm/lib/workbook-builder/types/row-data.js +1 -0
  115. package/build/esm/lib/workbook-builder/types/sheet-data.js +1 -0
  116. package/build/esm/lib/workbook-builder/types/xml-node.js +1 -0
  117. package/build/esm/lib/workbook-builder/utils/build-app-xml.js +48 -0
  118. package/build/esm/lib/workbook-builder/utils/build-cell-children.js +56 -0
  119. package/build/esm/lib/workbook-builder/utils/build-content-types-xml.js +39 -0
  120. package/build/esm/lib/workbook-builder/utils/build-core-xml.js +24 -0
  121. package/build/esm/lib/workbook-builder/utils/build-rels-xml.js +16 -0
  122. package/build/esm/lib/workbook-builder/utils/build-shared-strings-xml.js +36 -0
  123. package/build/esm/lib/workbook-builder/utils/build-styles-xml.js +142 -0
  124. package/build/esm/lib/workbook-builder/utils/build-theme-xml.js +606 -0
  125. package/build/esm/lib/workbook-builder/utils/build-workbook-rels-xml.js +55 -0
  126. package/build/esm/lib/workbook-builder/utils/build-workbook-xml.js +23 -0
  127. package/build/esm/lib/workbook-builder/utils/build-worksheet-xml.js +63 -0
  128. package/build/esm/lib/workbook-builder/utils/build-xml.js +69 -0
  129. package/build/esm/lib/workbook-builder/utils/constants.js +52 -0
  130. package/build/esm/lib/workbook-builder/utils/date-to-excel-serial.js +13 -0
  131. package/build/esm/lib/workbook-builder/utils/index.js +18 -0
  132. package/build/esm/lib/workbook-builder/utils/initialize-files.js +36 -0
  133. package/build/esm/lib/workbook-builder/utils/sheet.js +141 -0
  134. package/build/esm/lib/workbook-builder/utils/write-shared-strings-xml.js +43 -0
  135. package/build/esm/lib/workbook-builder/utils/write-styles-xml.js +157 -0
  136. package/build/esm/lib/workbook-builder/utils/write-worksheet-xml.js +203 -0
  137. package/build/esm/lib/workbook-builder/utils/write-xml.js +34 -0
  138. package/build/esm/lib/workbook-builder/workbook-builder.js +374 -0
  139. package/build/types/lib/template/utils/column-letter-to-index.d.ts +1 -0
  140. package/build/types/lib/template/utils/index.d.ts +1 -1
  141. package/build/types/lib/utils/index.d.ts +2 -0
  142. package/build/types/lib/utils/trim-and-join-multiline.d.ts +23 -0
  143. package/build/types/lib/workbook-builder/default/border.d.ts +7 -0
  144. package/build/types/lib/workbook-builder/default/cell-xf.d.ts +7 -0
  145. package/build/types/lib/workbook-builder/default/fill.d.ts +7 -0
  146. package/build/types/lib/workbook-builder/default/font.d.ts +21 -0
  147. package/build/types/lib/workbook-builder/default/index.d.ts +5 -0
  148. package/build/types/lib/workbook-builder/default/sheet-name.d.ts +6 -0
  149. package/build/types/lib/workbook-builder/index.d.ts +2 -0
  150. package/build/types/lib/workbook-builder/merge-cells/add.d.ts +15 -0
  151. package/build/types/lib/workbook-builder/merge-cells/helpers/index.d.ts +2 -0
  152. package/build/types/lib/workbook-builder/merge-cells/helpers/ranges-equal.d.ts +10 -0
  153. package/build/types/lib/workbook-builder/merge-cells/helpers/ranges-intersect.d.ts +10 -0
  154. package/build/types/lib/workbook-builder/merge-cells/index.d.ts +2 -0
  155. package/build/types/lib/workbook-builder/merge-cells/remove.d.ts +15 -0
  156. package/build/types/lib/workbook-builder/shared-string-ref/add.d.ts +13 -0
  157. package/build/types/lib/workbook-builder/shared-string-ref/index.d.ts +3 -0
  158. package/build/types/lib/workbook-builder/shared-string-ref/remove-all-from-sheet.d.ts +10 -0
  159. package/build/types/lib/workbook-builder/shared-string-ref/remove.d.ts +13 -0
  160. package/build/types/lib/workbook-builder/style-ref/add-or-get.d.ts +16 -0
  161. package/build/types/lib/workbook-builder/style-ref/helpers/add-num-fmt.d.ts +17 -0
  162. package/build/types/lib/workbook-builder/style-ref/helpers/border-to-xml.d.ts +16 -0
  163. package/build/types/lib/workbook-builder/style-ref/helpers/fill-to-xml.d.ts +17 -0
  164. package/build/types/lib/workbook-builder/style-ref/helpers/font-to-xml.d.ts +18 -0
  165. package/build/types/lib/workbook-builder/style-ref/helpers/index.d.ts +5 -0
  166. package/build/types/lib/workbook-builder/style-ref/helpers/reindex-style-map-after-removal.d.ts +15 -0
  167. package/build/types/lib/workbook-builder/style-ref/index.d.ts +3 -0
  168. package/build/types/lib/workbook-builder/style-ref/remove-all-from-sheet.d.ts +4 -0
  169. package/build/types/lib/workbook-builder/style-ref/remove.d.ts +18 -0
  170. package/build/types/lib/workbook-builder/types/app-xml-options.d.ts +9 -0
  171. package/build/types/lib/workbook-builder/types/border-style.d.ts +5 -0
  172. package/build/types/lib/workbook-builder/types/cell-data.d.ts +10 -0
  173. package/build/types/lib/workbook-builder/types/cell-style.d.ts +32 -0
  174. package/build/types/lib/workbook-builder/types/cell-type.d.ts +11 -0
  175. package/build/types/lib/workbook-builder/types/cell-value.d.ts +2 -0
  176. package/build/types/lib/workbook-builder/types/cell-xf.d.ts +13 -0
  177. package/build/types/lib/workbook-builder/types/index.d.ts +11 -0
  178. package/build/types/lib/workbook-builder/types/merge-cell.d.ts +6 -0
  179. package/build/types/lib/workbook-builder/types/row-data.d.ts +5 -0
  180. package/build/types/lib/workbook-builder/types/sheet-data.d.ts +13 -0
  181. package/build/types/lib/workbook-builder/types/xml-node.d.ts +11 -0
  182. package/build/types/lib/workbook-builder/utils/build-app-xml.d.ts +2 -0
  183. package/build/types/lib/workbook-builder/utils/build-cell-children.d.ts +9 -0
  184. package/build/types/lib/workbook-builder/utils/build-content-types-xml.d.ts +1 -0
  185. package/build/types/lib/workbook-builder/utils/build-core-xml.d.ts +1 -0
  186. package/build/types/lib/workbook-builder/utils/build-rels-xml.d.ts +1 -0
  187. package/build/types/lib/workbook-builder/utils/build-shared-strings-xml.d.ts +10 -0
  188. package/build/types/lib/workbook-builder/utils/build-styles-xml.d.ts +23 -0
  189. package/build/types/lib/workbook-builder/utils/build-theme-xml.d.ts +1 -0
  190. package/build/types/lib/workbook-builder/utils/build-workbook-rels-xml.d.ts +9 -0
  191. package/build/types/lib/workbook-builder/utils/build-workbook-xml.d.ts +3 -0
  192. package/build/types/lib/workbook-builder/utils/build-worksheet-xml.d.ts +2 -0
  193. package/build/types/lib/workbook-builder/utils/build-xml.d.ts +50 -0
  194. package/build/types/lib/workbook-builder/utils/constants.d.ts +47 -0
  195. package/build/types/lib/workbook-builder/utils/date-to-excel-serial.d.ts +9 -0
  196. package/build/types/lib/workbook-builder/utils/index.d.ts +18 -0
  197. package/build/types/lib/workbook-builder/utils/initialize-files.d.ts +13 -0
  198. package/build/types/lib/workbook-builder/utils/sheet.d.ts +21 -0
  199. package/build/types/lib/workbook-builder/utils/write-shared-strings-xml.d.ts +11 -0
  200. package/build/types/lib/workbook-builder/utils/write-styles-xml.d.ts +24 -0
  201. package/build/types/lib/workbook-builder/utils/write-worksheet-xml.d.ts +14 -0
  202. package/build/types/lib/workbook-builder/utils/write-xml.d.ts +3 -0
  203. package/build/types/lib/workbook-builder/workbook-builder.d.ts +110 -0
  204. package/package.json +1 -1
  205. /package/build/cjs/lib/{template/utils → utils}/escape-xml.js +0 -0
  206. /package/build/esm/lib/{template/utils → utils}/escape-xml.js +0 -0
  207. /package/build/types/lib/{template/utils → utils}/escape-xml.d.ts +0 -0
@@ -0,0 +1,48 @@
1
+ import { XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
2
+ import { buildXml } from "./build-xml.js";
3
+ export function buildAppXml({ appVersion = "16.0300", application = "Microsoft Excel", company = "", hyperlinksChanged = false, linksUpToDate = false, sharedDoc = false, sheetNames = ["Sheet1"], } = {}) {
4
+ const app = {
5
+ attrs: {
6
+ xmlns: XML_NAMESPACES.EXTENDED_PROPERTIES,
7
+ "xmlns:vt": XML_NAMESPACES.DOC_PROPS_VTYPES,
8
+ },
9
+ children: [
10
+ { children: [application], tag: "Application" },
11
+ { children: ["0"], tag: "DocSecurity" },
12
+ { children: ["false"], tag: "ScaleCrop" },
13
+ {
14
+ children: [
15
+ {
16
+ attrs: { baseType: "variant", size: "2" },
17
+ children: [
18
+ { children: [{ children: ["Worksheets"], tag: "vt:lpstr" }], tag: "vt:variant" },
19
+ { children: [{ children: [String(sheetNames.length)], tag: "vt:i4" }], tag: "vt:variant" },
20
+ ],
21
+ tag: "vt:vector",
22
+ },
23
+ ],
24
+ tag: "HeadingPairs",
25
+ },
26
+ {
27
+ children: [
28
+ {
29
+ attrs: { baseType: "lpstr", size: String(sheetNames.length) },
30
+ children: sheetNames.map((name) => ({ children: [name], tag: "vt:lpstr" })),
31
+ tag: "vt:vector",
32
+ },
33
+ ],
34
+ tag: "TitlesOfParts",
35
+ },
36
+ { children: [company], tag: "Company" },
37
+ { children: [String(linksUpToDate)], tag: "LinksUpToDate" },
38
+ { children: [String(sharedDoc)], tag: "SharedDoc" },
39
+ { children: [String(hyperlinksChanged)], tag: "HyperlinksChanged" },
40
+ { children: [appVersion], tag: "AppVersion" },
41
+ ],
42
+ tag: "Properties",
43
+ };
44
+ return [
45
+ XML_DECLARATION,
46
+ buildXml(app),
47
+ ].join("\n");
48
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Builds XML children nodes for a cell based on its type and value.
3
+ *
4
+ * @param cell - The cell data containing value and type information
5
+ *
6
+ * @returns Array of XML nodes representing the cell's content structure
7
+ */
8
+ export function buildCellChildren(cell) {
9
+ if (cell.value === undefined) {
10
+ return [];
11
+ }
12
+ if (cell.isFormula) {
13
+ return [
14
+ {
15
+ children: [String(cell.value)],
16
+ tag: "f",
17
+ },
18
+ ];
19
+ }
20
+ switch (cell.type) {
21
+ case "b": {
22
+ return [
23
+ {
24
+ children: [cell.value ? "1" : "0"],
25
+ tag: "v",
26
+ },
27
+ ];
28
+ }
29
+ case "inlineStr": {
30
+ // For inlineStr, wrap value in <is><t>value</t></is>
31
+ return [
32
+ {
33
+ children: [
34
+ {
35
+ children: [String(cell.value)],
36
+ tag: "t",
37
+ },
38
+ ],
39
+ tag: "is",
40
+ },
41
+ ];
42
+ }
43
+ case "s":
44
+ case "n":
45
+ case "str":
46
+ case "e":
47
+ default: {
48
+ return [
49
+ {
50
+ children: [String(cell.value)],
51
+ tag: "v",
52
+ },
53
+ ];
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,39 @@
1
+ import { CONTENT_TYPES, XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
2
+ import { buildXml } from "./build-xml.js";
3
+ export function buildContentTypesXml(sheetsCount) {
4
+ const defaults = [
5
+ { ContentType: CONTENT_TYPES.RELATIONSHIPS, Extension: "rels" },
6
+ { ContentType: CONTENT_TYPES.XML, Extension: "xml" },
7
+ ].map(({ ContentType, Extension }) => ({
8
+ attrs: { ContentType, Extension },
9
+ tag: "Default",
10
+ }));
11
+ const overrides = [
12
+ { ContentType: CONTENT_TYPES.THEME, PartName: "/xl/theme/theme1.xml" },
13
+ { ContentType: CONTENT_TYPES.WORKBOOK, PartName: "/xl/workbook.xml" },
14
+ { ContentType: CONTENT_TYPES.STYLES, PartName: "/xl/styles.xml" },
15
+ { ContentType: CONTENT_TYPES.CORE, PartName: "/docProps/core.xml" },
16
+ { ContentType: CONTENT_TYPES.APP, PartName: "/docProps/app.xml" },
17
+ { ContentType: CONTENT_TYPES.SHARED_STRINGS, PartName: "/xl/sharedStrings.xml" },
18
+ ].map(({ ContentType, PartName }) => ({
19
+ attrs: { ContentType, PartName },
20
+ tag: "Override",
21
+ }));
22
+ for (let i = 1; i <= sheetsCount; i++) {
23
+ overrides.push({
24
+ attrs: {
25
+ ContentType: CONTENT_TYPES.WORKSHEET,
26
+ PartName: `/xl/worksheets/sheet${i}.xml`,
27
+ },
28
+ tag: "Override",
29
+ });
30
+ }
31
+ return [
32
+ XML_DECLARATION,
33
+ buildXml({
34
+ attrs: { xmlns: XML_NAMESPACES.CONTENT_TYPES },
35
+ children: [...defaults, ...overrides],
36
+ tag: "Types",
37
+ }),
38
+ ].join("\n");
39
+ }
@@ -0,0 +1,24 @@
1
+ import { XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
2
+ import { buildXml } from "./build-xml.js";
3
+ export function buildCoreXml(creator = "Excel Generator", lastModifiedBy = "Excel Generator", created = "2025-01-01T00:00:00Z", modified = "2025-01-01T00:00:00Z") {
4
+ const coreProps = {
5
+ attrs: {
6
+ "xmlns:cp": XML_NAMESPACES.CORE_PROPERTIES,
7
+ "xmlns:dc": XML_NAMESPACES.DC,
8
+ "xmlns:dcmitype": XML_NAMESPACES.DCMITYPE,
9
+ "xmlns:dcterms": XML_NAMESPACES.DCTERMS,
10
+ "xmlns:xsi": XML_NAMESPACES.XSI,
11
+ },
12
+ children: [
13
+ { children: [creator], tag: "dc:creator" },
14
+ { children: [lastModifiedBy], tag: "cp:lastModifiedBy" },
15
+ { attrs: { "xsi:type": "dcterms:W3CDTF" }, children: [created], tag: "dcterms:created" },
16
+ { attrs: { "xsi:type": "dcterms:W3CDTF" }, children: [modified], tag: "dcterms:modified" },
17
+ ],
18
+ tag: "cp:coreProperties",
19
+ };
20
+ return [
21
+ XML_DECLARATION,
22
+ buildXml(coreProps),
23
+ ].join("\n");
24
+ }
@@ -0,0 +1,16 @@
1
+ import { RELATIONSHIP_TYPES, XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
2
+ import { buildXml } from "./build-xml.js";
3
+ export function buildRelsXml() {
4
+ return [
5
+ XML_DECLARATION,
6
+ buildXml({
7
+ attrs: { xmlns: XML_NAMESPACES.PACKAGE_RELATIONSHIPS },
8
+ children: [
9
+ { attrs: { Id: "rId1", Target: "xl/workbook.xml", Type: RELATIONSHIP_TYPES.OFFICE_DOCUMENT }, tag: "Relationship" },
10
+ { attrs: { Id: "rId2", Target: "docProps/core.xml", Type: RELATIONSHIP_TYPES.CORE }, tag: "Relationship" },
11
+ { attrs: { Id: "rId3", Target: "docProps/app.xml", Type: RELATIONSHIP_TYPES.APP }, tag: "Relationship" },
12
+ ],
13
+ tag: "Relationships",
14
+ }),
15
+ ].join("\n");
16
+ }
@@ -0,0 +1,36 @@
1
+ import { escapeXml } from "../../utils/index.js";
2
+ import { XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
3
+ import { buildXml } from "./build-xml.js";
4
+ /**
5
+ * Builds the `sharedStrings.xml` content from the provided strings.
6
+ *
7
+ * Note: According to the Excel specification, leading/trailing spaces in text
8
+ * nodes must set the attribute `xml:space="preserve"` on the corresponding <t>.
9
+ *
10
+ * @param strings - Array of unique strings used in the workbook
11
+ * @returns XML string for the shared strings part
12
+ */
13
+ export function buildSharedStringsXml(strings = []) {
14
+ return [
15
+ XML_DECLARATION,
16
+ buildXml({
17
+ attrs: {
18
+ count: String(strings.length),
19
+ uniqueCount: String(strings.length),
20
+ xmlns: XML_NAMESPACES.SPREADSHEET_ML,
21
+ },
22
+ children: strings.map((s) => ({
23
+ children: [
24
+ {
25
+ // Excel spec: leading/trailing spaces require xml:space="preserve"
26
+ attrs: /^\s|\s$/.test(s) ? { "xml:space": "preserve" } : undefined,
27
+ children: [escapeXml(s)],
28
+ tag: "t",
29
+ },
30
+ ],
31
+ tag: "si",
32
+ })),
33
+ tag: "sst",
34
+ }),
35
+ ].join("\n");
36
+ }
@@ -0,0 +1,142 @@
1
+ import * as Default from "../default/index.js";
2
+ import { XML_DECLARATION, XML_NAMESPACES } from "./constants.js";
3
+ import { buildXml } from "./build-xml.js";
4
+ /**
5
+ * Builds the `styles.xml` part for the workbook based on provided style collections.
6
+ * Falls back to default font/fill/border collections when none are provided.
7
+ *
8
+ * @param data - Style collections used to construct the stylesheet
9
+ * @param data.borders - Array of border XmlNodes
10
+ * @param data.cellXfs - Array of cell format records
11
+ * @param data.fills - Array of fill XmlNodes
12
+ * @param data.fonts - Array of font XmlNodes
13
+ * @param data.numFmts - Array of custom number formats (formatCode and id)
14
+ * @returns XML string for the styles part
15
+ */
16
+ export function buildStylesXml(data) {
17
+ const { borders = [], cellXfs = [], fills = [], fonts = [], numFmts = [], } = data || {};
18
+ const children = [];
19
+ if (numFmts.length) {
20
+ children.push({
21
+ attrs: { count: String(numFmts.length) },
22
+ children: numFmts.map(nf => ({
23
+ attrs: {
24
+ formatCode: nf.formatCode,
25
+ numFmtId: String(nf.id),
26
+ },
27
+ tag: "numFmt",
28
+ })),
29
+ tag: "numFmts",
30
+ });
31
+ }
32
+ if (fonts.length) {
33
+ children.push({
34
+ attrs: { count: String(fonts.length) },
35
+ children: fonts,
36
+ tag: "fonts",
37
+ });
38
+ }
39
+ else {
40
+ children.push({
41
+ attrs: { count: "1" },
42
+ children: [Default.font()],
43
+ tag: "fonts",
44
+ });
45
+ }
46
+ if (fills.length) {
47
+ children.push({
48
+ attrs: { count: String(fills.length) },
49
+ children: fills,
50
+ tag: "fills",
51
+ });
52
+ }
53
+ else {
54
+ children.push({
55
+ attrs: { count: "1" },
56
+ children: [Default.fill()],
57
+ tag: "fills",
58
+ });
59
+ }
60
+ if (borders.length) {
61
+ children.push({
62
+ attrs: { count: String(borders.length) },
63
+ children: borders,
64
+ tag: "borders",
65
+ });
66
+ }
67
+ else {
68
+ children.push({
69
+ attrs: { count: "1" },
70
+ children: [Default.border()],
71
+ tag: "borders",
72
+ });
73
+ }
74
+ children.push({
75
+ attrs: { count: "1" },
76
+ children: [{
77
+ attrs: { borderId: "0", fillId: "0", fontId: "0", numFmtId: "0" },
78
+ tag: "xf",
79
+ }],
80
+ tag: "cellStyleXfs",
81
+ });
82
+ if (cellXfs.length) {
83
+ children.push({
84
+ attrs: { count: String(cellXfs.length) },
85
+ children: cellXfs.map((xf, i) => {
86
+ const isBaseXf = i === 0;
87
+ const hasAlignment = !!xf.alignment;
88
+ const xfChildren = [];
89
+ if (hasAlignment) {
90
+ xfChildren.push({
91
+ attrs: {
92
+ ...(xf.alignment?.horizontal ? { horizontal: xf.alignment.horizontal } : {}),
93
+ ...(xf.alignment?.vertical ? { vertical: xf.alignment.vertical } : {}),
94
+ ...(xf.alignment?.wrapText ? { wrapText: "1" } : {}),
95
+ ...(xf.alignment?.indent !== undefined ? { indent: String(xf.alignment.indent) } : {}),
96
+ },
97
+ tag: "alignment",
98
+ });
99
+ }
100
+ return {
101
+ attrs: {
102
+ ...(isBaseXf
103
+ ? {}
104
+ : {
105
+ applyBorder: "1",
106
+ applyFill: "1",
107
+ applyFont: "1",
108
+ applyNumberFormat: xf.numFmtId ? "1" : "0",
109
+ }),
110
+ ...(hasAlignment ? { applyAlignment: "1" } : {}),
111
+ borderId: String(xf.borderId),
112
+ fillId: String(xf.fillId),
113
+ fontId: String(xf.fontId),
114
+ numFmtId: String(xf.numFmtId ?? 0),
115
+ xfId: "0",
116
+ },
117
+ children: xfChildren,
118
+ tag: "xf",
119
+ };
120
+ }),
121
+ tag: "cellXfs",
122
+ });
123
+ }
124
+ else {
125
+ // Base style without fill
126
+ children.push({
127
+ attrs: { count: "1" },
128
+ children: [
129
+ { attrs: { borderId: "0", fillId: "0", fontId: "0", numFmtId: "0", xfId: "0" }, tag: "xf" },
130
+ ],
131
+ tag: "cellXfs",
132
+ });
133
+ }
134
+ return [
135
+ XML_DECLARATION,
136
+ buildXml({
137
+ attrs: { xmlns: XML_NAMESPACES.SPREADSHEET_ML },
138
+ children,
139
+ tag: "styleSheet",
140
+ }),
141
+ ].join("\n");
142
+ }