@evotecit/officeimo 0.0.0-stage → 0.1.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 (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1219 -2
  3. package/bundles/officeimo-canopyx.js +11286 -0
  4. package/bundles/officeimo-canopyx.mjs +11185 -0
  5. package/bundles/officeimo-csv.js +1254 -0
  6. package/bundles/officeimo-csv.mjs +1229 -0
  7. package/bundles/officeimo-datatables.js +11572 -0
  8. package/bundles/officeimo-datatables.mjs +11470 -0
  9. package/bundles/officeimo-execution.js +1503 -0
  10. package/bundles/officeimo-execution.mjs +1479 -0
  11. package/bundles/officeimo-pdf.js +6564 -0
  12. package/bundles/officeimo-pdf.mjs +6502 -0
  13. package/bundles/officeimo-xlsx.js +5333 -0
  14. package/bundles/officeimo-xlsx.mjs +5279 -0
  15. package/bundles/officeimo.js +11022 -0
  16. package/bundles/officeimo.mjs +10924 -0
  17. package/dist/core/dates.d.ts +1 -0
  18. package/dist/core/dates.js +16 -0
  19. package/dist/core/errors.d.ts +12 -0
  20. package/dist/core/errors.js +31 -0
  21. package/dist/core/grouped-report.d.ts +87 -0
  22. package/dist/core/grouped-report.js +252 -0
  23. package/dist/core/index.d.ts +72 -0
  24. package/dist/core/index.js +37 -0
  25. package/dist/core/iteration.d.ts +7 -0
  26. package/dist/core/iteration.js +149 -0
  27. package/dist/core/limits.d.ts +7 -0
  28. package/dist/core/limits.js +56 -0
  29. package/dist/core/links.d.ts +5 -0
  30. package/dist/core/links.js +14 -0
  31. package/dist/core/presentation.d.ts +29 -0
  32. package/dist/core/presentation.js +59 -0
  33. package/dist/core/progress.d.ts +1 -0
  34. package/dist/core/progress.js +10 -0
  35. package/dist/core/sinks.d.ts +33 -0
  36. package/dist/core/sinks.js +101 -0
  37. package/dist/core/table.d.ts +8 -0
  38. package/dist/core/table.js +1 -0
  39. package/dist/core/value-text.d.ts +7 -0
  40. package/dist/core/value-text.js +13 -0
  41. package/dist/csv/index.d.ts +44 -0
  42. package/dist/csv/index.js +133 -0
  43. package/dist/csv/options.d.ts +1 -0
  44. package/dist/csv/options.js +8 -0
  45. package/dist/csv/structured-rows.d.ts +3 -0
  46. package/dist/csv/structured-rows.js +15 -0
  47. package/dist/execution/host.d.ts +5 -0
  48. package/dist/execution/host.js +164 -0
  49. package/dist/execution/index.d.ts +3 -0
  50. package/dist/execution/index.js +2 -0
  51. package/dist/execution/protocol.d.ts +48 -0
  52. package/dist/execution/protocol.js +173 -0
  53. package/dist/execution/types.d.ts +42 -0
  54. package/dist/execution/types.js +1 -0
  55. package/dist/execution/worker.d.ts +3 -0
  56. package/dist/execution/worker.js +147 -0
  57. package/dist/index.d.ts +32 -0
  58. package/dist/index.js +14 -0
  59. package/dist/integrations/canopyx/dates.d.ts +1 -0
  60. package/dist/integrations/canopyx/dates.js +34 -0
  61. package/dist/integrations/canopyx/index.d.ts +10 -0
  62. package/dist/integrations/canopyx/index.js +45 -0
  63. package/dist/integrations/canopyx/source.d.ts +3 -0
  64. package/dist/integrations/canopyx/source.js +116 -0
  65. package/dist/integrations/canopyx/types.d.ts +83 -0
  66. package/dist/integrations/canopyx/types.js +1 -0
  67. package/dist/integrations/datatables/api.d.ts +1 -0
  68. package/dist/integrations/datatables/api.js +27 -0
  69. package/dist/integrations/datatables/headings.d.ts +1 -0
  70. package/dist/integrations/datatables/headings.js +65 -0
  71. package/dist/integrations/datatables/index.d.ts +12 -0
  72. package/dist/integrations/datatables/index.js +167 -0
  73. package/dist/integrations/datatables/snapshot.d.ts +3 -0
  74. package/dist/integrations/datatables/snapshot.js +206 -0
  75. package/dist/integrations/datatables/types.d.ts +98 -0
  76. package/dist/integrations/datatables/types.js +1 -0
  77. package/dist/integrations/options.d.ts +13 -0
  78. package/dist/integrations/options.js +34 -0
  79. package/dist/internal/capture.d.ts +4 -0
  80. package/dist/internal/capture.js +14 -0
  81. package/dist/internal/columns.d.ts +27 -0
  82. package/dist/internal/columns.js +1 -0
  83. package/dist/internal/crc32.d.ts +6 -0
  84. package/dist/internal/crc32.js +17 -0
  85. package/dist/internal/export-values.d.ts +3 -0
  86. package/dist/internal/export-values.js +14 -0
  87. package/dist/internal/rows.d.ts +6 -0
  88. package/dist/internal/rows.js +68 -0
  89. package/dist/internal/table-spans.d.ts +11 -0
  90. package/dist/internal/table-spans.js +58 -0
  91. package/dist/internal/totals.d.ts +15 -0
  92. package/dist/internal/totals.js +42 -0
  93. package/dist/opc/index.d.ts +67 -0
  94. package/dist/opc/index.js +233 -0
  95. package/dist/opc/properties.d.ts +15 -0
  96. package/dist/opc/properties.js +18 -0
  97. package/dist/opc/uri.d.ts +5 -0
  98. package/dist/opc/uri.js +41 -0
  99. package/dist/pdf/annotations.d.ts +1 -0
  100. package/dist/pdf/annotations.js +38 -0
  101. package/dist/pdf/binary.d.ts +6 -0
  102. package/dist/pdf/binary.js +24 -0
  103. package/dist/pdf/document-drawings.d.ts +15 -0
  104. package/dist/pdf/document-drawings.js +237 -0
  105. package/dist/pdf/document-grouped.d.ts +1 -0
  106. package/dist/pdf/document-grouped.js +194 -0
  107. package/dist/pdf/document-images.d.ts +19 -0
  108. package/dist/pdf/document-images.js +125 -0
  109. package/dist/pdf/document-types.d.ts +160 -0
  110. package/dist/pdf/document-types.js +1 -0
  111. package/dist/pdf/document-vectors.d.ts +11 -0
  112. package/dist/pdf/document-vectors.js +77 -0
  113. package/dist/pdf/document.d.ts +9 -0
  114. package/dist/pdf/document.js +270 -0
  115. package/dist/pdf/drawing-types.d.ts +53 -0
  116. package/dist/pdf/drawing-types.js +1 -0
  117. package/dist/pdf/font-resources.d.ts +43 -0
  118. package/dist/pdf/font-resources.js +201 -0
  119. package/dist/pdf/font-support.d.ts +1 -0
  120. package/dist/pdf/font-support.js +60 -0
  121. package/dist/pdf/font.d.ts +17 -0
  122. package/dist/pdf/font.js +56 -0
  123. package/dist/pdf/grouped-table.d.ts +6 -0
  124. package/dist/pdf/grouped-table.js +40 -0
  125. package/dist/pdf/grouped-types.d.ts +34 -0
  126. package/dist/pdf/grouped-types.js +1 -0
  127. package/dist/pdf/grouped.d.ts +10 -0
  128. package/dist/pdf/grouped.js +37 -0
  129. package/dist/pdf/index.d.ts +22 -0
  130. package/dist/pdf/index.js +8 -0
  131. package/dist/pdf/inline-text.d.ts +30 -0
  132. package/dist/pdf/inline-text.js +142 -0
  133. package/dist/pdf/inline-types.d.ts +15 -0
  134. package/dist/pdf/inline-types.js +1 -0
  135. package/dist/pdf/list-layout.d.ts +6 -0
  136. package/dist/pdf/list-layout.js +49 -0
  137. package/dist/pdf/objects.d.ts +2 -0
  138. package/dist/pdf/objects.js +143 -0
  139. package/dist/pdf/outlines.d.ts +1 -0
  140. package/dist/pdf/outlines.js +55 -0
  141. package/dist/pdf/page-context.d.ts +1 -0
  142. package/dist/pdf/page-context.js +12 -0
  143. package/dist/pdf/pages.d.ts +1 -0
  144. package/dist/pdf/pages.js +679 -0
  145. package/dist/pdf/paragraph-layout.d.ts +19 -0
  146. package/dist/pdf/paragraph-layout.js +66 -0
  147. package/dist/pdf/paragraph-styles.d.ts +1 -0
  148. package/dist/pdf/paragraph-styles.js +66 -0
  149. package/dist/pdf/png.d.ts +9 -0
  150. package/dist/pdf/png.js +259 -0
  151. package/dist/pdf/preflight.d.ts +7 -0
  152. package/dist/pdf/preflight.js +32 -0
  153. package/dist/pdf/presentation-settings.d.ts +5 -0
  154. package/dist/pdf/presentation-settings.js +28 -0
  155. package/dist/pdf/providers/harfbuzz.d.ts +60 -0
  156. package/dist/pdf/providers/harfbuzz.js +112 -0
  157. package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
  158. package/dist/pdf/providers/unicode13-profile.js +115 -0
  159. package/dist/pdf/section-types.d.ts +15 -0
  160. package/dist/pdf/section-types.js +40 -0
  161. package/dist/pdf/session.d.ts +25 -0
  162. package/dist/pdf/session.js +55 -0
  163. package/dist/pdf/settings.d.ts +15 -0
  164. package/dist/pdf/settings.js +171 -0
  165. package/dist/pdf/shaped-text.d.ts +5 -0
  166. package/dist/pdf/shaped-text.js +205 -0
  167. package/dist/pdf/standard-widths.d.ts +1 -0
  168. package/dist/pdf/standard-widths.js +317 -0
  169. package/dist/pdf/structure-parent-tree.d.ts +1 -0
  170. package/dist/pdf/structure-parent-tree.js +57 -0
  171. package/dist/pdf/structure.d.ts +6 -0
  172. package/dist/pdf/structure.js +250 -0
  173. package/dist/pdf/table-band-flow.d.ts +34 -0
  174. package/dist/pdf/table-band-flow.js +114 -0
  175. package/dist/pdf/table-band-plan.d.ts +10 -0
  176. package/dist/pdf/table-band-plan.js +85 -0
  177. package/dist/pdf/table-layout.d.ts +71 -0
  178. package/dist/pdf/table-layout.js +205 -0
  179. package/dist/pdf/table-structure.d.ts +1 -0
  180. package/dist/pdf/table-structure.js +92 -0
  181. package/dist/pdf/text-shaping-types.d.ts +39 -0
  182. package/dist/pdf/text-shaping-types.js +1 -0
  183. package/dist/pdf/text.d.ts +13 -0
  184. package/dist/pdf/text.js +96 -0
  185. package/dist/pdf/truetype-subset.d.ts +5 -0
  186. package/dist/pdf/truetype-subset.js +217 -0
  187. package/dist/pdf/truetype.d.ts +1 -0
  188. package/dist/pdf/truetype.js +235 -0
  189. package/dist/pdf/types.d.ts +169 -0
  190. package/dist/pdf/types.js +1 -0
  191. package/dist/pdf/write.d.ts +6 -0
  192. package/dist/pdf/write.js +57 -0
  193. package/dist/xlsx/attachments.d.ts +30 -0
  194. package/dist/xlsx/attachments.js +67 -0
  195. package/dist/xlsx/chart-bindings.d.ts +1 -0
  196. package/dist/xlsx/chart-bindings.js +122 -0
  197. package/dist/xlsx/chart-types.d.ts +73 -0
  198. package/dist/xlsx/chart-types.js +1 -0
  199. package/dist/xlsx/chart-xml.d.ts +2 -0
  200. package/dist/xlsx/chart-xml.js +103 -0
  201. package/dist/xlsx/charts.d.ts +1 -0
  202. package/dist/xlsx/charts.js +159 -0
  203. package/dist/xlsx/conditional-formatting.d.ts +1 -0
  204. package/dist/xlsx/conditional-formatting.js +158 -0
  205. package/dist/xlsx/conditional-types.d.ts +64 -0
  206. package/dist/xlsx/conditional-types.js +1 -0
  207. package/dist/xlsx/data-row-ranges.d.ts +5 -0
  208. package/dist/xlsx/data-row-ranges.js +39 -0
  209. package/dist/xlsx/differential-styles.d.ts +1 -0
  210. package/dist/xlsx/differential-styles.js +102 -0
  211. package/dist/xlsx/formula.d.ts +8 -0
  212. package/dist/xlsx/formula.js +104 -0
  213. package/dist/xlsx/grouped-layout.d.ts +61 -0
  214. package/dist/xlsx/grouped-layout.js +79 -0
  215. package/dist/xlsx/grouped.d.ts +16 -0
  216. package/dist/xlsx/grouped.js +161 -0
  217. package/dist/xlsx/index.d.ts +24 -0
  218. package/dist/xlsx/index.js +10 -0
  219. package/dist/xlsx/layout.d.ts +1 -0
  220. package/dist/xlsx/layout.js +186 -0
  221. package/dist/xlsx/options.d.ts +1 -0
  222. package/dist/xlsx/options.js +16 -0
  223. package/dist/xlsx/portable.d.ts +27 -0
  224. package/dist/xlsx/portable.js +44 -0
  225. package/dist/xlsx/preservation.d.ts +1 -0
  226. package/dist/xlsx/preservation.js +60 -0
  227. package/dist/xlsx/ranges.d.ts +1 -0
  228. package/dist/xlsx/ranges.js +24 -0
  229. package/dist/xlsx/regions.d.ts +1 -0
  230. package/dist/xlsx/regions.js +97 -0
  231. package/dist/xlsx/structured-rows.d.ts +11 -0
  232. package/dist/xlsx/structured-rows.js +28 -0
  233. package/dist/xlsx/style-xml.d.ts +1 -0
  234. package/dist/xlsx/style-xml.js +6 -0
  235. package/dist/xlsx/styles.d.ts +70 -0
  236. package/dist/xlsx/styles.js +203 -0
  237. package/dist/xlsx/table.d.ts +12 -0
  238. package/dist/xlsx/table.js +35 -0
  239. package/dist/xlsx/types.d.ts +180 -0
  240. package/dist/xlsx/types.js +1 -0
  241. package/dist/xlsx/validation-types.d.ts +18 -0
  242. package/dist/xlsx/validation-types.js +1 -0
  243. package/dist/xlsx/validation.d.ts +1 -0
  244. package/dist/xlsx/validation.js +92 -0
  245. package/dist/xlsx/values.d.ts +13 -0
  246. package/dist/xlsx/values.js +86 -0
  247. package/dist/xlsx/workbook.d.ts +50 -0
  248. package/dist/xlsx/workbook.js +452 -0
  249. package/dist/xlsx/worksheet.d.ts +71 -0
  250. package/dist/xlsx/worksheet.js +700 -0
  251. package/dist/xlsx/write.d.ts +15 -0
  252. package/dist/xlsx/write.js +47 -0
  253. package/dist/xml/index.d.ts +24 -0
  254. package/dist/xml/index.js +74 -0
  255. package/dist/zip/entry.d.ts +39 -0
  256. package/dist/zip/entry.js +122 -0
  257. package/dist/zip/index.d.ts +44 -0
  258. package/dist/zip/index.js +248 -0
  259. package/package.json +104 -4
@@ -0,0 +1,452 @@
1
+ import { beginTask, checkAbort, pause, taskYieldDue } from "../core/iteration.js";
2
+ import { OfficeIMOError } from "../core/errors.js";
3
+ import { notifyProgress } from "../core/progress.js";
4
+ import { ChunkedTextSink } from "../core/sinks.js";
5
+ import { ExportBudget } from "../core/limits.js";
6
+ import { OpcPackage, officeRelationshipsNamespace, relationshipTypes, corePropertiesXml } from "../opc/index.js";
7
+ import { escapeOoxmlAttribute, cleanXml, xmlDeclaration } from "../xml/index.js";
8
+ import { StyleRegistry, spreadsheetNamespace } from "./styles.js";
9
+ import { sheetName, assertXlsxValue, copyValue } from "./values.js";
10
+ import { copyColumns } from "../internal/rows.js";
11
+ import { captureFields } from "../internal/capture.js";
12
+ import { captureTableSpans } from "../internal/table-spans.js";
13
+ import { assertExportValue } from "../core/presentation.js";
14
+ import { Worksheet } from "./worksheet.js";
15
+ import { defineTable, tableXml } from "./table.js";
16
+ import { copyHyperlink, drawingXml, drawingContentType } from "./attachments.js";
17
+ import { TextOverflow } from "./preservation.js";
18
+ import { captureWorkbookOptions, sheetOptionFields } from "./options.js";
19
+ import { prepareChart } from "./charts.js";
20
+ import { qualifiedColumnRange, resolveDataColumn } from "./ranges.js";
21
+ import { chartContentType, writeChartXml } from "./chart-xml.js";
22
+ const xlsxMime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
23
+ const formatType = (name) => "application/vnd.openxmlformats-officedocument.spreadsheetml." + name + "+xml";
24
+ /** Capture registered worksheet metadata before preflight and construction inspect it. */
25
+ function captureSheet(options, policy) {
26
+ const captured = captureFields(options, sheetOptionFields);
27
+ const columns = copyColumns(captured.columns ?? [], true);
28
+ const footer = captured.footer && captureFields(captured.footer, ["rows", "values", "totals", "style"]);
29
+ const print = captured.print && captureFields(captured.print, ["paper", "orientation", "fitToWidth", "fitToHeight", "repeatHeaders", "margins", "header", "footer"]);
30
+ return { ...captured, columns,
31
+ ...(captured.headerRows === undefined ? {} : { headerRows: captureTableSpans(captured.headerRows, columns.length) }),
32
+ ...(footer ? { footer: { ...footer,
33
+ ...(footer.rows === undefined ? {} : { rows: captureTableSpans(footer.rows, columns.length) }),
34
+ ...(footer.values === undefined ? {} : { values: footer.values.map(copyValue) }) } } : {}),
35
+ ...(captured.table ? { table: captureFields(captured.table, ["name", "style", "bandedRows", "bandedColumns", "firstColumn", "lastColumn"]) } : {}),
36
+ ...(captured.autoSize ? { autoSize: captureFields(captured.autoSize, ["sampleRows", "minWidth", "maxWidth"]) } : {}),
37
+ ...(captured.title ? { title: captureFields(captured.title, ["text", "style", "height"]) } : {}),
38
+ ...(print ? { print: { ...print, ...(print.margins === undefined ? {} : {
39
+ margins: captureFields(print.margins, ["left", "right", "top", "bottom", "header", "footer"])
40
+ }) } } : {}),
41
+ ...(captured.hyperlinks ? { hyperlinks: captured.hyperlinks.map(link => copyHyperlink(link, policy)) } : {}) };
42
+ }
43
+ /** Streaming writer model; append rows through Worksheets, then finalize once. */
44
+ export class Workbook {
45
+ portableValues = false;
46
+ /** @internal One-table helpers share the writer while admitting only portable values. */
47
+ static forTable(options) {
48
+ const book = new Workbook(options);
49
+ book.portableValues = true;
50
+ return book;
51
+ }
52
+ /** @internal Captured once by the worksheet projector. */
53
+ get valueValidator() { return this.portableValues ? assertExportValue : assertXlsxValue; }
54
+ styles;
55
+ names = new Set();
56
+ sheets = [];
57
+ tableNames = new Set();
58
+ tableCount = 0;
59
+ writers;
60
+ package;
61
+ state = "open";
62
+ result;
63
+ completion;
64
+ activeSheet;
65
+ failure;
66
+ overflow;
67
+ links = 0;
68
+ imageBytes = 0;
69
+ bufferedBytes = 0;
70
+ mergedRanges = 0;
71
+ conditionalFormats = 0;
72
+ dataValidationCount = 0;
73
+ validationTextCharacters = 0;
74
+ formulaCharacters = 0;
75
+ hasAuthoredFormulas = false;
76
+ nativeCharts = [];
77
+ chartSourceSheets = 0;
78
+ chartPoints = 0;
79
+ chartTextCharacters = 0;
80
+ registering = false;
81
+ /** Caller-owned property getters may run during capture; they cannot register another workbook object. */
82
+ registration(action) {
83
+ this.assertOpen();
84
+ if (this.registering)
85
+ throw new OfficeIMOError("INVALID_STATE", "Workbook registrations cannot be reentered from input getters.");
86
+ this.registering = true;
87
+ try {
88
+ return action();
89
+ }
90
+ finally {
91
+ this.registering = false;
92
+ }
93
+ }
94
+ /** @internal Shared budget across worksheet headers and data. */
95
+ budget;
96
+ /** @internal Immutable settings used by the worksheet owner. */
97
+ settings;
98
+ constructor(options = {}) {
99
+ options = captureWorkbookOptions(options);
100
+ const dateMode = options.dateMode ?? "local", compression = options.compression ?? "auto", policy = options.invalidCharacterPolicy ?? "strip";
101
+ if (dateMode !== "local" && dateMode !== "utc")
102
+ throw new RangeError("dateMode must be local or utc.");
103
+ if (compression !== "auto" && compression !== "store")
104
+ throw new RangeError("compression must be auto or store.");
105
+ if (options.oversizedText !== undefined && !["reject", "preserve"].includes(options.oversizedText))
106
+ throw new RangeError("oversizedText must be reject or preserve.");
107
+ cleanXml("", policy);
108
+ corePropertiesXml(options, policy);
109
+ this.budget = new ExportBudget(options.limits);
110
+ this.styles = new StyleRegistry(policy, options.limits?.maxStyles, options.limits?.maxDifferentialStyles);
111
+ if (options.sink !== undefined && typeof options.sink.write !== "function")
112
+ throw new TypeError("sink must be a ByteSink.");
113
+ this.settings = Object.freeze({ ...options, ...(options.limits ? { limits: Object.freeze({ ...options.limits }) } : {}), dateMode, compression, invalidCharacterPolicy: policy });
114
+ this.writers = Object.freeze({ ...options.cellValueWriters });
115
+ for (const writer of Object.values(this.writers))
116
+ if (typeof writer !== "function")
117
+ throw new TypeError("Cell value writers must be functions.");
118
+ // One write phase covers every worksheet and incremental append in this book.
119
+ beginTask();
120
+ this.package = new OpcPackage({ compression, invalidCharacterPolicy: policy, ...(options.signal ? { signal: options.signal } : {}),
121
+ ...(options.sink ? { sink: options.sink } : {}), ...(options.limits?.maxOutputBytes === undefined ? {} : { maxOutputBytes: options.limits.maxOutputBytes }) });
122
+ // Property dates and app settings are captured before an asynchronous export begins.
123
+ this.package.setProperties(options, options.appProperties);
124
+ // Register fixed generated parts now; deferred XML sees the final sheets and styles.
125
+ this.addXmlPart("/xl/workbook.xml", formatType("sheet.main"), () => this.workbookXml());
126
+ this.addXmlPart("/xl/styles.xml", formatType("styles"), () => this.styles.toXml());
127
+ }
128
+ addXmlPart(uri, contentType, xml) {
129
+ this.package.addPart({ uri, contentType, data: async (sink) => {
130
+ const text = new ChunkedTextSink(sink, this.settings.signal);
131
+ await text.write(xml());
132
+ await text.close();
133
+ } });
134
+ }
135
+ workbookXml() {
136
+ return xmlDeclaration + '<workbook xmlns="' + spreadsheetNamespace + '" xmlns:r="' + officeRelationshipsNamespace + '"><workbookPr date1904="0"/><bookViews><workbookView/></bookViews><sheets>' +
137
+ this.sheets.map((s, i) => '<sheet name="' + escapeOoxmlAttribute(s.name, this.settings.invalidCharacterPolicy) + '" sheetId="' + (i + 1) + '"' + (s.hidden ? ' state="hidden"' : "") + ' r:id="rId' + (i + 1) + '"/>').join("") + '</sheets>' +
138
+ (this.sheets.some(s => s.printSettings) ? '<definedNames>' + this.sheets.map((s, i) => {
139
+ const quoted = "'" + s.name.replace(/'/g, "''") + "'!";
140
+ return s.printSettings ? '<definedName name="_xlnm.Print_Area" localSheetId="' + i + '">' + escapeOoxmlAttribute(quoted + '$A$1:$' + s.lastColumn + '$' + s.totalRows, this.settings.invalidCharacterPolicy) + '</definedName>' +
141
+ (s.printSettings.repeatHeaders && s.firstHeaderRow ? '<definedName name="_xlnm.Print_Titles" localSheetId="' + i + '">' + escapeOoxmlAttribute(quoted + '$' + s.firstHeaderRow + ':$' + s.headerRowCount, this.settings.invalidCharacterPolicy) + '</definedName>' : "") : "";
142
+ }).join("") + '</definedNames>' : "") + (this.hasAuthoredFormulas ? '<calcPr calcMode="auto" fullCalcOnLoad="1" forceFullCalc="1"/>' : "") + '</workbook>';
143
+ }
144
+ /** @internal */
145
+ assertOpen() {
146
+ try {
147
+ checkAbort(this.settings.signal);
148
+ }
149
+ catch (error) {
150
+ void this.discard(error).catch(() => { });
151
+ throw error;
152
+ }
153
+ if (this.state === "failed")
154
+ throw this.failure;
155
+ if (this.state !== "open")
156
+ throw new OfficeIMOError("INVALID_STATE", "Workbook is already finalized.");
157
+ }
158
+ /** @internal */
159
+ writerFor(type) { return Object.prototype.hasOwnProperty.call(this.writers, type) ? this.writers[type] : undefined; }
160
+ /** @internal */
161
+ preserveText(sheet, cell, text) {
162
+ if (this.settings.oversizedText !== "preserve")
163
+ throw new RangeError("Excel cell text exceeds 32,767 UTF-16 code units.");
164
+ if (!this.overflow) {
165
+ this.checkSheetLimit(this.sheets.length + this.chartSourceSheets + 1);
166
+ this.budget.reserve(5, "SheetCellPartTextParts".length);
167
+ this.overflow = new TextOverflow(sheetName("Text overflow", this.names, this.settings.invalidCharacterPolicy), this.settings.limits?.maxOverflowCharacters ?? 4000000, this.settings.invalidCharacterPolicy, (cells, characters, rows) => { this.budget.row(rows); this.budget.reserve(cells, characters); });
168
+ }
169
+ return this.overflow.add(sheet, cell, text);
170
+ }
171
+ checkSheetLimit(count) {
172
+ if (count > 65526)
173
+ throw new RangeError("Workbook has too many sheets.");
174
+ if (this.settings.limits?.maxSheets !== undefined && count > this.settings.limits.maxSheets)
175
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxSheets exceeded.");
176
+ }
177
+ /** @internal */
178
+ checkLinks(count) {
179
+ if (this.settings.limits?.maxHyperlinks !== undefined && this.links + count > this.settings.limits.maxHyperlinks)
180
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxHyperlinks exceeded.");
181
+ }
182
+ /** @internal Reserve a preflighted worksheet batch or one row hyperlink. */
183
+ retainLink(count = 1) { this.checkLinks(count); this.links += count; }
184
+ /** @internal */
185
+ checkImage(bytes) {
186
+ if (this.settings.limits?.maxImageBytes !== undefined && this.imageBytes + bytes > this.settings.limits.maxImageBytes)
187
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxImageBytes exceeded.");
188
+ }
189
+ /** @internal Reserve only a validated image, after checking its budget before copying. */
190
+ retainImage(bytes) {
191
+ this.checkImage(bytes);
192
+ this.imageBytes += bytes;
193
+ }
194
+ /** @internal Bound compressed worksheet retention in Blob mode before final packaging. */
195
+ retainBufferedBytes(bytes) { this.budget.check("maxOutputBytes", this.bufferedBytes + bytes); this.bufferedBytes += bytes; }
196
+ /** @internal Includes generated report merges across all sheets. */
197
+ checkMerges(count) { if (this.mergedRanges + count > (this.settings.limits?.maxMergedRanges ?? 10000))
198
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxMergedRanges exceeded."); }
199
+ /** @internal */
200
+ checkConditionalFormats(count) { if (this.conditionalFormats + count > (this.settings.limits?.maxConditionalFormats ?? 1000))
201
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxConditionalFormats exceeded."); }
202
+ /** @internal Remaining immutable editing-rule budgets, checked before capture allocation. */
203
+ get availableDataValidations() { return (this.settings.limits?.maxDataValidations ?? 1000) - this.dataValidationCount; }
204
+ /** @internal */
205
+ get availableValidationTextCharacters() { return (this.settings.limits?.maxValidationTextCharacters ?? 1000000) - this.validationTextCharacters; }
206
+ /** @internal Authored formula text is counted once when its cell is serialized. */
207
+ retainFormula(characters) {
208
+ if (this.formulaCharacters + characters > (this.settings.limits?.maxFormulaCharacters ?? 1000000))
209
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxFormulaCharacters exceeded.");
210
+ this.formulaCharacters += characters;
211
+ this.hasAuthoredFormulas = true;
212
+ }
213
+ checkChartText(characters) {
214
+ if (this.chartTextCharacters + characters > (this.settings.limits?.maxChartTextCharacters ?? 1000000))
215
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxChartTextCharacters exceeded.");
216
+ }
217
+ /** @internal Category caches consume the same cumulative budget as chart metadata. */
218
+ retainChartText(characters) {
219
+ this.checkChartText(characters);
220
+ this.chartTextCharacters += characters;
221
+ }
222
+ resolveChartBinding(input) {
223
+ const source = input.sheet;
224
+ if (!this.sheets.includes(source) || source.hidden)
225
+ throw new TypeError("Bound charts require a visible worksheet in the same workbook.");
226
+ const metadata = source.chartBindingMetadata, first = input.firstRow ?? 0;
227
+ if (source.isBusy || metadata.closed || first < metadata.rows)
228
+ throw new OfficeIMOError("INVALID_STATE", "Register a chart before its first selected data row is written, while the source worksheet is idle.");
229
+ const category = resolveDataColumn(metadata.columns, input.categoryColumn, "Chart");
230
+ const series = input.series.map(item => resolveDataColumn(metadata.columns, item.column, "Chart"));
231
+ const start = metadata.headerRows + first + 1;
232
+ return { descriptor: { source, sheetName: metadata.name, headerRows: metadata.headerRows, categoryColumn: category, seriesColumns: series,
233
+ seriesNames: series.map(column => metadata.columns[column - 1].header),
234
+ categoryFormula: qualifiedColumnRange(metadata.name, category, start, input.rowCount),
235
+ valueFormulas: series.map(column => qualifiedColumnRange(metadata.name, column, start, input.rowCount)),
236
+ nameFormulas: series.map((column, index) => input.series[index].name === undefined
237
+ ? metadata.headerReferences[column - 1] : undefined) }, retainText: characters => this.retainChartText(characters) };
238
+ }
239
+ /** @internal Reserve chart snapshots and their source-sheet names before any output is generated. */
240
+ registerChart(input) {
241
+ return this.registration(() => {
242
+ if (this.nativeCharts.length >= (this.settings.limits?.maxCharts ?? 100))
243
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxCharts exceeded.");
244
+ const chart = prepareChart(input, this.settings.invalidCharacterPolicy, (this.settings.limits?.maxChartPoints ?? 100000) - this.chartPoints, (this.settings.limits?.maxChartTextCharacters ?? 1000000) - this.chartTextCharacters, this.settings.signal, value => this.resolveChartBinding(value));
245
+ this.assertOpen();
246
+ // Input getters may write categories into an existing bound chart's cache.
247
+ this.checkChartText(chart.textCharacters);
248
+ // Input getters may append rows to an existing sheet and allocate text preservation.
249
+ this.checkSheetLimit(this.sheets.length + this.chartSourceSheets + (chart.binding ? 0 : 1) + (this.overflow ? 1 : 0));
250
+ const registered = Object.freeze({ ...chart, id: this.nativeCharts.length + 1,
251
+ dataSheetName: chart.binding ? chart.binding.source.name : sheetName("Chart data " + (this.nativeCharts.length + 1), this.names, this.settings.invalidCharacterPolicy) });
252
+ this.nativeCharts.push(registered);
253
+ this.chartPoints += chart.points;
254
+ this.chartTextCharacters += chart.textCharacters;
255
+ if (chart.binding)
256
+ chart.binding.source.retainChartBinding(chart.binding);
257
+ else
258
+ this.chartSourceSheets++;
259
+ return registered;
260
+ });
261
+ }
262
+ /** @internal Streamed parts cannot interleave. Starting a new sheet completes the preceding one. */
263
+ async openSheet(sheet) {
264
+ if (this.activeSheet && this.activeSheet !== sheet) {
265
+ if (this.activeSheet.isBusy)
266
+ throw new OfficeIMOError("INVALID_STATE", "Await the active streamed worksheet before starting another.");
267
+ await this.activeSheet.finish();
268
+ }
269
+ this.activeSheet = sheet;
270
+ return this.package.openPart("/xl/worksheets/sheet" + (this.sheets.indexOf(sheet) + 1) + ".xml", formatType("worksheet"));
271
+ }
272
+ /** @internal Preserve the producer's failure while releasing all owned output. */
273
+ async discard(error) {
274
+ this.state = "failed";
275
+ this.failure = error;
276
+ await Promise.allSettled(this.sheets.map(sheet => sheet.discard(error)));
277
+ await this.package.discard(error);
278
+ this.overflow?.clear();
279
+ this.nativeCharts.length = 0;
280
+ }
281
+ get worksheets() { return Object.freeze([...this.sheets]); }
282
+ /** Total report data rows; excludes title/header/footer and preservation records. */
283
+ get rowCount() { return this.sheets.reduce((sum, sheet) => sum + sheet.reportRowCount, 0); }
284
+ addWorksheet(name, options = {}) {
285
+ return this.registration(() => {
286
+ this.checkSheetLimit(this.sheets.length + this.chartSourceSheets + 1 + (this.overflow ? 1 : 0));
287
+ // Validation/serialization use erased column metadata; projection later receives T rows.
288
+ const metadata = captureSheet(options, this.settings.invalidCharacterPolicy);
289
+ const prepared = Worksheet.validate(this, metadata);
290
+ let tableOptions = metadata.table;
291
+ if (tableOptions && tableOptions.name === undefined) {
292
+ let suffix = this.tableCount + 1;
293
+ while (this.tableNames.has(("Table" + suffix).toLowerCase()))
294
+ suffix++;
295
+ tableOptions = { ...tableOptions, name: "Table" + suffix };
296
+ }
297
+ const table = tableOptions ? defineTable(this.tableCount + 1, tableOptions, metadata.columns ?? [], this.settings.invalidCharacterPolicy) : undefined;
298
+ if (table && this.tableNames.has(table.name.toLowerCase()))
299
+ throw new TypeError("Duplicate Excel table name: " + table.name);
300
+ sheetName(name, this.names, this.settings.invalidCharacterPolicy, false);
301
+ const sheet = Worksheet.create(this, () => sheetName(name, this.names, this.settings.invalidCharacterPolicy, false), metadata, table, false, prepared.conditional, prepared.validations);
302
+ this.assertOpen();
303
+ this.checkSheetLimit(this.sheets.length + this.chartSourceSheets + 1 + (this.overflow ? 1 : 0));
304
+ this.names.add(sheet.name.toLowerCase());
305
+ this.mergedRanges += sheet.mergeCount;
306
+ this.conditionalFormats += sheet.conditionalFormatCount;
307
+ this.dataValidationCount += sheet.dataValidations.count;
308
+ this.validationTextCharacters += sheet.dataValidations.textCharacters;
309
+ if (table) {
310
+ this.tableNames.add(table.name.toLowerCase());
311
+ this.tableCount++;
312
+ }
313
+ this.sheets.push(sheet);
314
+ return sheet;
315
+ });
316
+ }
317
+ addPart(part) {
318
+ this.assertOpen();
319
+ // Reserve now so collisions and invalid names fail at the call site.
320
+ this.package.addPart(part);
321
+ if (part.relationship) {
322
+ const { source, ...rel } = part.relationship;
323
+ this.package.addRelationship(source ?? "/xl/workbook.xml", { ...rel, target: part.uri });
324
+ }
325
+ }
326
+ /** Complete the archive and return counts. Does not close or dispose a caller-owned sink. */
327
+ finish() {
328
+ try {
329
+ checkAbort(this.settings.signal);
330
+ }
331
+ catch (error) {
332
+ return this.discard(error).then(() => { throw error; });
333
+ }
334
+ if (this.completion)
335
+ return this.completion;
336
+ if (this.state === "failed")
337
+ return Promise.reject(this.failure);
338
+ this.assertOpen();
339
+ if (this.sheets.some(s => s.isBusy))
340
+ throw new OfficeIMOError("INVALID_STATE", "Await addRows before finalizing the workbook.");
341
+ if (!this.sheets.length)
342
+ this.addWorksheet("Sheet1");
343
+ this.state = "finalizing";
344
+ this.completion = (async () => {
345
+ try {
346
+ const rows = this.rowCount;
347
+ for (let i = 0; i < this.sheets.length; i++) {
348
+ const sheet = this.sheets[i], uri = "/xl/worksheets/sheet" + (i + 1) + ".xml";
349
+ await sheet.finish();
350
+ const prepared = sheet.takePrepared();
351
+ if (prepared)
352
+ this.package.addPrepared(uri, formatType("worksheet"), prepared);
353
+ const table = sheet.tableDefinition;
354
+ if (table) {
355
+ const tableUri = "/xl/tables/table" + table.id + ".xml";
356
+ this.addXmlPart(tableUri, formatType("table"), () => tableXml(table, sheet.rowCount, this.settings.invalidCharacterPolicy, sheet.headerRowCount, sheet.footerSettings));
357
+ this.package.addRelationship(uri, { id: "table", type: officeRelationshipsNamespace + "/table", target: tableUri });
358
+ }
359
+ for (let j = 0; j < sheet.hyperlinks.length; j++) {
360
+ checkAbort(this.settings.signal);
361
+ this.package.addRelationship(uri, { id: "link" + (j + 1), type: officeRelationshipsNamespace + "/hyperlink", target: sheet.hyperlinks[j].target, external: true });
362
+ if ((j & 255) === 255 && taskYieldDue()) {
363
+ await pause();
364
+ checkAbort(this.settings.signal);
365
+ }
366
+ }
367
+ if (sheet.images.length || sheet.charts.length) {
368
+ const drawingUri = "/xl/drawings/drawing" + (i + 1) + ".xml";
369
+ this.addXmlPart(drawingUri, drawingContentType, () => drawingXml(sheet.images, this.settings.invalidCharacterPolicy, sheet.charts));
370
+ this.package.addRelationship(uri, { id: "drawing", type: officeRelationshipsNamespace + "/drawing", target: drawingUri });
371
+ for (let j = 0; j < sheet.images.length; j++) {
372
+ checkAbort(this.settings.signal);
373
+ const imageUri = "/xl/media/sheet" + (i + 1) + "-image" + (j + 1) + ".png";
374
+ this.package.addPart({ uri: imageUri, contentType: "image/png", data: sheet.images[j].data });
375
+ this.package.addRelationship(drawingUri, { id: "image" + (j + 1), type: officeRelationshipsNamespace + "/image", target: imageUri });
376
+ if ((j & 255) === 255 && taskYieldDue()) {
377
+ await pause();
378
+ checkAbort(this.settings.signal);
379
+ }
380
+ }
381
+ for (const chart of sheet.charts) {
382
+ checkAbort(this.settings.signal);
383
+ const chartUri = "/xl/charts/chart" + chart.id + ".xml";
384
+ this.package.addPart({ uri: chartUri, contentType: chartContentType, data: sink => writeChartXml(chart, sink, this.settings.invalidCharacterPolicy, this.settings.signal) });
385
+ this.package.addRelationship(drawingUri, { id: "chart" + chart.id, type: officeRelationshipsNamespace + "/chart", target: chartUri });
386
+ }
387
+ }
388
+ }
389
+ if (this.overflow) {
390
+ const sheet = Worksheet.create(this, this.overflow.sheetName, { columns: [{ header: "Sheet" }, { header: "Cell" }, { header: "Part" }, { header: "Text", width: 80, wrapText: true }, { header: "Parts" }], freezeHeader: true }, undefined, true);
391
+ this.sheets.push(sheet);
392
+ await sheet.finish(this.overflow.values());
393
+ const prepared = sheet.takePrepared();
394
+ if (prepared)
395
+ this.package.addPrepared("/xl/worksheets/sheet" + this.sheets.length + ".xml", formatType("worksheet"), prepared);
396
+ this.overflow.clear();
397
+ }
398
+ for (const chart of this.nativeCharts) {
399
+ checkAbort(this.settings.signal);
400
+ if (chart.binding)
401
+ continue;
402
+ const sheet = Worksheet.createChartData(this, chart);
403
+ this.sheets.push(sheet);
404
+ function* rows() { for (let i = 0; i < chart.data.categories.length; i++)
405
+ yield [chart.data.categories[i], ...chart.data.series.map(series => series.values[i])]; }
406
+ await sheet.finish(rows());
407
+ const prepared = sheet.takePrepared();
408
+ if (prepared)
409
+ this.package.addPrepared("/xl/worksheets/sheet" + this.sheets.length + ".xml", formatType("worksheet"), prepared);
410
+ }
411
+ this.package.addRelationship("/", { id: "workbook", type: relationshipTypes.officeDocument, target: "/xl/workbook.xml" });
412
+ for (let i = 0; i < this.sheets.length; i++) {
413
+ checkAbort(this.settings.signal);
414
+ this.package.addRelationship("/xl/workbook.xml", { id: "rId" + (i + 1), type: relationshipTypes.worksheet, target: "/xl/worksheets/sheet" + (i + 1) + ".xml" });
415
+ if ((i & 255) === 255 && taskYieldDue()) {
416
+ await pause();
417
+ checkAbort(this.settings.signal);
418
+ }
419
+ }
420
+ this.package.addRelationship("/xl/workbook.xml", { id: "styles", type: relationshipTypes.styles, target: "/xl/styles.xml" });
421
+ const bytes = await this.package.finish();
422
+ for (const sheet of this.sheets)
423
+ sheet.releaseCharts();
424
+ this.nativeCharts.length = 0;
425
+ notifyProgress(this.settings.onProgress, { phase: "complete", rows, bytes });
426
+ checkAbort(this.settings.signal);
427
+ this.state = "complete";
428
+ return { rows, sheets: this.sheets.length, bytes };
429
+ }
430
+ catch (error) {
431
+ await this.discard(error);
432
+ throw error;
433
+ }
434
+ })();
435
+ return this.completion;
436
+ }
437
+ toBlob() {
438
+ try {
439
+ checkAbort(this.settings.signal);
440
+ }
441
+ catch (error) {
442
+ return this.discard(error).then(() => { throw error; });
443
+ }
444
+ if (this.settings.sink)
445
+ throw new OfficeIMOError("INVALID_STATE", "This workbook uses a caller-owned sink; use finish().");
446
+ if (!this.result) {
447
+ const completion = this.finish();
448
+ this.result = completion.then(() => this.package.toBlob(xlsxMime));
449
+ }
450
+ return this.result;
451
+ }
452
+ }
@@ -0,0 +1,71 @@
1
+ import { Cell } from "./values.js";
2
+ import type { XlsxRows } from "./types.js";
3
+ import type { Hyperlink, WorksheetImage } from "./attachments.js";
4
+ import type { ExportValue } from "../core/presentation.js";
5
+ import type { WorksheetChart } from "./chart-types.js";
6
+ import { FormulaCell } from "./formula.js";
7
+ /** Worksheet rows are written once in order; the model retains compressed output rather than source data. */
8
+ export declare class Worksheet<T = never> {
9
+ private readonly book;
10
+ private readonly table?;
11
+ private readonly preserved;
12
+ private readonly chartData;
13
+ private readonly validations;
14
+ private readonly columns;
15
+ private readonly project;
16
+ /** Final unique workbook name, captured before row projection begins. */
17
+ readonly name: string;
18
+ private readonly declared;
19
+ private readonly options;
20
+ private entry;
21
+ private completion;
22
+ private prepared;
23
+ private buffer;
24
+ private readonly output;
25
+ private started;
26
+ private busy;
27
+ private failed;
28
+ private error;
29
+ private count;
30
+ private readonly headerRows;
31
+ private readonly layout;
32
+ private completedRegions;
33
+ private readonly titleStyle;
34
+ private pending;
35
+ private pendingCharacters;
36
+ private reservedLayout;
37
+ private readonly internalLinks;
38
+ private readonly links;
39
+ private readonly linkedCells;
40
+ private readonly pictures;
41
+ private readonly nativeCharts;
42
+ private readonly conditional;
43
+ private completed;
44
+ private readonly chartSources;
45
+ private constructor();
46
+ private cell;
47
+ private rowXml;
48
+ private outline;
49
+ private writeRow;
50
+ private sampleRow;
51
+ private reserveLayout;
52
+ private start;
53
+ addRows(rows: [T] extends [never] ? XlsxRows : Iterable<T> | AsyncIterable<T>): Promise<void>;
54
+ addRows<U extends {
55
+ readonly [K in keyof U]: ExportValue | Cell | FormulaCell;
56
+ }>(rows: [T] extends [never] ? Iterable<U> | AsyncIterable<U> : never): Promise<void>;
57
+ private progress;
58
+ get rowCount(): number;
59
+ /** A finite formula reference to a completed data column, excluding report decorations.
60
+ * Empty data returns undefined; discontiguous grouped details require explicit detailRanges instead. */
61
+ dataRange(column: string | number): string | undefined;
62
+ /** Capture a bounded native chart. Source cells and caches remain independent of later caller mutations. */
63
+ addChart(chart: WorksheetChart): void;
64
+ /** Register a PNG such as a chart; bytes are copied so the caller can reuse its buffer. */
65
+ addImage(image: WorksheetImage): void;
66
+ /** Attach an external report link to a cell without changing its literal value. */
67
+ addHyperlink(link: Hyperlink): void;
68
+ private appendHyperlink;
69
+ /** Complete this worksheet. A streamed workbook can then start its next worksheet. */
70
+ close(): Promise<void>;
71
+ }