@evotecit/officeimo 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +1222 -2
- package/bundles/officeimo-canopyx.js +11741 -0
- package/bundles/officeimo-canopyx.mjs +11639 -0
- package/bundles/officeimo-csv.js +1264 -0
- package/bundles/officeimo-csv.mjs +1239 -0
- package/bundles/officeimo-datatables.js +12018 -0
- package/bundles/officeimo-datatables.mjs +11915 -0
- package/bundles/officeimo-execution.js +1513 -0
- package/bundles/officeimo-execution.mjs +1489 -0
- package/bundles/officeimo-pdf.js +7009 -0
- package/bundles/officeimo-pdf.mjs +6946 -0
- package/bundles/officeimo-xlsx.js +5344 -0
- package/bundles/officeimo-xlsx.mjs +5290 -0
- package/bundles/officeimo.js +11468 -0
- package/bundles/officeimo.mjs +11369 -0
- package/dist/core/dates.d.ts +1 -0
- package/dist/core/dates.js +16 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/grouped-report.d.ts +87 -0
- package/dist/core/grouped-report.js +252 -0
- package/dist/core/index.d.ts +74 -0
- package/dist/core/index.js +37 -0
- package/dist/core/iteration.d.ts +7 -0
- package/dist/core/iteration.js +149 -0
- package/dist/core/limits.d.ts +7 -0
- package/dist/core/limits.js +56 -0
- package/dist/core/links.d.ts +5 -0
- package/dist/core/links.js +14 -0
- package/dist/core/presentation.d.ts +29 -0
- package/dist/core/presentation.js +59 -0
- package/dist/core/progress.d.ts +1 -0
- package/dist/core/progress.js +10 -0
- package/dist/core/sinks.d.ts +33 -0
- package/dist/core/sinks.js +102 -0
- package/dist/core/table.d.ts +8 -0
- package/dist/core/table.js +1 -0
- package/dist/core/value-text.d.ts +7 -0
- package/dist/core/value-text.js +13 -0
- package/dist/csv/index.d.ts +44 -0
- package/dist/csv/index.js +133 -0
- package/dist/csv/options.d.ts +1 -0
- package/dist/csv/options.js +8 -0
- package/dist/csv/structured-rows.d.ts +3 -0
- package/dist/csv/structured-rows.js +15 -0
- package/dist/execution/host.d.ts +5 -0
- package/dist/execution/host.js +164 -0
- package/dist/execution/index.d.ts +3 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/protocol.d.ts +48 -0
- package/dist/execution/protocol.js +173 -0
- package/dist/execution/types.d.ts +42 -0
- package/dist/execution/types.js +1 -0
- package/dist/execution/worker.d.ts +3 -0
- package/dist/execution/worker.js +147 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +14 -0
- package/dist/integrations/canopyx/dates.d.ts +1 -0
- package/dist/integrations/canopyx/dates.js +34 -0
- package/dist/integrations/canopyx/index.d.ts +10 -0
- package/dist/integrations/canopyx/index.js +45 -0
- package/dist/integrations/canopyx/source.d.ts +3 -0
- package/dist/integrations/canopyx/source.js +122 -0
- package/dist/integrations/canopyx/types.d.ts +83 -0
- package/dist/integrations/canopyx/types.js +1 -0
- package/dist/integrations/datatables/api.d.ts +1 -0
- package/dist/integrations/datatables/api.js +27 -0
- package/dist/integrations/datatables/headings.d.ts +1 -0
- package/dist/integrations/datatables/headings.js +65 -0
- package/dist/integrations/datatables/index.d.ts +12 -0
- package/dist/integrations/datatables/index.js +167 -0
- package/dist/integrations/datatables/snapshot.d.ts +3 -0
- package/dist/integrations/datatables/snapshot.js +206 -0
- package/dist/integrations/datatables/types.d.ts +98 -0
- package/dist/integrations/datatables/types.js +1 -0
- package/dist/integrations/options.d.ts +13 -0
- package/dist/integrations/options.js +34 -0
- package/dist/internal/capture.d.ts +4 -0
- package/dist/internal/capture.js +14 -0
- package/dist/internal/columns.d.ts +27 -0
- package/dist/internal/columns.js +1 -0
- package/dist/internal/crc32.d.ts +6 -0
- package/dist/internal/crc32.js +17 -0
- package/dist/internal/export-values.d.ts +3 -0
- package/dist/internal/export-values.js +14 -0
- package/dist/internal/rows.d.ts +6 -0
- package/dist/internal/rows.js +77 -0
- package/dist/internal/table-spans.d.ts +11 -0
- package/dist/internal/table-spans.js +58 -0
- package/dist/internal/totals.d.ts +15 -0
- package/dist/internal/totals.js +42 -0
- package/dist/opc/index.d.ts +67 -0
- package/dist/opc/index.js +233 -0
- package/dist/opc/properties.d.ts +15 -0
- package/dist/opc/properties.js +18 -0
- package/dist/opc/uri.d.ts +5 -0
- package/dist/opc/uri.js +41 -0
- package/dist/pdf/annotations.d.ts +1 -0
- package/dist/pdf/annotations.js +38 -0
- package/dist/pdf/binary.d.ts +6 -0
- package/dist/pdf/binary.js +24 -0
- package/dist/pdf/document-drawings.d.ts +15 -0
- package/dist/pdf/document-drawings.js +237 -0
- package/dist/pdf/document-grouped.d.ts +1 -0
- package/dist/pdf/document-grouped.js +244 -0
- package/dist/pdf/document-images.d.ts +19 -0
- package/dist/pdf/document-images.js +125 -0
- package/dist/pdf/document-types.d.ts +160 -0
- package/dist/pdf/document-types.js +1 -0
- package/dist/pdf/document-vectors.d.ts +11 -0
- package/dist/pdf/document-vectors.js +77 -0
- package/dist/pdf/document.d.ts +9 -0
- package/dist/pdf/document.js +270 -0
- package/dist/pdf/drawing-types.d.ts +53 -0
- package/dist/pdf/drawing-types.js +1 -0
- package/dist/pdf/font-resources.d.ts +43 -0
- package/dist/pdf/font-resources.js +202 -0
- package/dist/pdf/font-support.d.ts +1 -0
- package/dist/pdf/font-support.js +60 -0
- package/dist/pdf/font.d.ts +17 -0
- package/dist/pdf/font.js +56 -0
- package/dist/pdf/grouped-table.d.ts +6 -0
- package/dist/pdf/grouped-table.js +40 -0
- package/dist/pdf/grouped-types.d.ts +34 -0
- package/dist/pdf/grouped-types.js +1 -0
- package/dist/pdf/grouped.d.ts +10 -0
- package/dist/pdf/grouped.js +37 -0
- package/dist/pdf/index.d.ts +22 -0
- package/dist/pdf/index.js +8 -0
- package/dist/pdf/inline-text.d.ts +30 -0
- package/dist/pdf/inline-text.js +183 -0
- package/dist/pdf/inline-types.d.ts +15 -0
- package/dist/pdf/inline-types.js +1 -0
- package/dist/pdf/list-layout.d.ts +6 -0
- package/dist/pdf/list-layout.js +49 -0
- package/dist/pdf/objects.d.ts +2 -0
- package/dist/pdf/objects.js +320 -0
- package/dist/pdf/outlines.d.ts +1 -0
- package/dist/pdf/outlines.js +55 -0
- package/dist/pdf/page-context.d.ts +1 -0
- package/dist/pdf/page-context.js +12 -0
- package/dist/pdf/pages.d.ts +1 -0
- package/dist/pdf/pages.js +678 -0
- package/dist/pdf/paragraph-layout.d.ts +21 -0
- package/dist/pdf/paragraph-layout.js +71 -0
- package/dist/pdf/paragraph-styles.d.ts +1 -0
- package/dist/pdf/paragraph-styles.js +66 -0
- package/dist/pdf/png.d.ts +9 -0
- package/dist/pdf/png.js +259 -0
- package/dist/pdf/preflight.d.ts +7 -0
- package/dist/pdf/preflight.js +32 -0
- package/dist/pdf/presentation-settings.d.ts +5 -0
- package/dist/pdf/presentation-settings.js +28 -0
- package/dist/pdf/providers/harfbuzz.d.ts +60 -0
- package/dist/pdf/providers/harfbuzz.js +112 -0
- package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
- package/dist/pdf/providers/unicode13-profile.js +115 -0
- package/dist/pdf/section-types.d.ts +15 -0
- package/dist/pdf/section-types.js +40 -0
- package/dist/pdf/session.d.ts +25 -0
- package/dist/pdf/session.js +55 -0
- package/dist/pdf/settings.d.ts +17 -0
- package/dist/pdf/settings.js +196 -0
- package/dist/pdf/shaped-text.d.ts +5 -0
- package/dist/pdf/shaped-text.js +205 -0
- package/dist/pdf/standard-widths.d.ts +1 -0
- package/dist/pdf/standard-widths.js +317 -0
- package/dist/pdf/structure-parent-tree.d.ts +1 -0
- package/dist/pdf/structure-parent-tree.js +57 -0
- package/dist/pdf/structure.d.ts +6 -0
- package/dist/pdf/structure.js +302 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +129 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +83 -0
- package/dist/pdf/table-layout.d.ts +75 -0
- package/dist/pdf/table-layout.js +217 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +108 -0
- package/dist/pdf/text-shaping-types.d.ts +39 -0
- package/dist/pdf/text-shaping-types.js +1 -0
- package/dist/pdf/text.d.ts +13 -0
- package/dist/pdf/text.js +101 -0
- package/dist/pdf/truetype-subset.d.ts +5 -0
- package/dist/pdf/truetype-subset.js +217 -0
- package/dist/pdf/truetype.d.ts +1 -0
- package/dist/pdf/truetype.js +235 -0
- package/dist/pdf/types.d.ts +171 -0
- package/dist/pdf/types.js +1 -0
- package/dist/pdf/write.d.ts +6 -0
- package/dist/pdf/write.js +57 -0
- package/dist/pdf/xref.d.ts +1 -0
- package/dist/pdf/xref.js +30 -0
- package/dist/xlsx/attachments.d.ts +30 -0
- package/dist/xlsx/attachments.js +67 -0
- package/dist/xlsx/chart-bindings.d.ts +1 -0
- package/dist/xlsx/chart-bindings.js +122 -0
- package/dist/xlsx/chart-types.d.ts +73 -0
- package/dist/xlsx/chart-types.js +1 -0
- package/dist/xlsx/chart-xml.d.ts +2 -0
- package/dist/xlsx/chart-xml.js +103 -0
- package/dist/xlsx/charts.d.ts +1 -0
- package/dist/xlsx/charts.js +159 -0
- package/dist/xlsx/conditional-formatting.d.ts +1 -0
- package/dist/xlsx/conditional-formatting.js +158 -0
- package/dist/xlsx/conditional-types.d.ts +64 -0
- package/dist/xlsx/conditional-types.js +1 -0
- package/dist/xlsx/data-row-ranges.d.ts +5 -0
- package/dist/xlsx/data-row-ranges.js +39 -0
- package/dist/xlsx/differential-styles.d.ts +1 -0
- package/dist/xlsx/differential-styles.js +102 -0
- package/dist/xlsx/formula.d.ts +8 -0
- package/dist/xlsx/formula.js +104 -0
- package/dist/xlsx/grouped-layout.d.ts +61 -0
- package/dist/xlsx/grouped-layout.js +79 -0
- package/dist/xlsx/grouped.d.ts +16 -0
- package/dist/xlsx/grouped.js +161 -0
- package/dist/xlsx/index.d.ts +24 -0
- package/dist/xlsx/index.js +10 -0
- package/dist/xlsx/layout.d.ts +1 -0
- package/dist/xlsx/layout.js +186 -0
- package/dist/xlsx/options.d.ts +1 -0
- package/dist/xlsx/options.js +16 -0
- package/dist/xlsx/portable.d.ts +27 -0
- package/dist/xlsx/portable.js +44 -0
- package/dist/xlsx/preservation.d.ts +1 -0
- package/dist/xlsx/preservation.js +60 -0
- package/dist/xlsx/ranges.d.ts +1 -0
- package/dist/xlsx/ranges.js +24 -0
- package/dist/xlsx/regions.d.ts +1 -0
- package/dist/xlsx/regions.js +97 -0
- package/dist/xlsx/structured-rows.d.ts +11 -0
- package/dist/xlsx/structured-rows.js +28 -0
- package/dist/xlsx/style-xml.d.ts +1 -0
- package/dist/xlsx/style-xml.js +6 -0
- package/dist/xlsx/styles.d.ts +70 -0
- package/dist/xlsx/styles.js +203 -0
- package/dist/xlsx/table.d.ts +12 -0
- package/dist/xlsx/table.js +35 -0
- package/dist/xlsx/types.d.ts +180 -0
- package/dist/xlsx/types.js +1 -0
- package/dist/xlsx/validation-types.d.ts +18 -0
- package/dist/xlsx/validation-types.js +1 -0
- package/dist/xlsx/validation.d.ts +1 -0
- package/dist/xlsx/validation.js +92 -0
- package/dist/xlsx/values.d.ts +13 -0
- package/dist/xlsx/values.js +87 -0
- package/dist/xlsx/workbook.d.ts +50 -0
- package/dist/xlsx/workbook.js +452 -0
- package/dist/xlsx/worksheet.d.ts +71 -0
- package/dist/xlsx/worksheet.js +700 -0
- package/dist/xlsx/write.d.ts +15 -0
- package/dist/xlsx/write.js +47 -0
- package/dist/xml/index.d.ts +24 -0
- package/dist/xml/index.js +74 -0
- package/dist/zip/entry.d.ts +39 -0
- package/dist/zip/entry.js +122 -0
- package/dist/zip/index.d.ts +44 -0
- package/dist/zip/index.js +248 -0
- package/package.json +104 -4
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { cleanXml, escapeXml } from "../xml/index.js";
|
|
2
|
+
import { assertExportValue, assertScalar } from "../core/presentation.js";
|
|
3
|
+
import { FormulaCell } from "./formula.js";
|
|
4
|
+
import { copyExportValue } from "../internal/export-values.js";
|
|
5
|
+
/** A typed value plus a workbook-local style index. */
|
|
6
|
+
export class Cell {
|
|
7
|
+
value;
|
|
8
|
+
style;
|
|
9
|
+
constructor(value, style) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
this.style = style;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** @internal Advanced worksheets admit cells registered on their owning workbook. */
|
|
15
|
+
export function assertXlsxValue(value) {
|
|
16
|
+
if (value instanceof FormulaCell)
|
|
17
|
+
return;
|
|
18
|
+
if (value instanceof Cell)
|
|
19
|
+
assertScalar(value.value);
|
|
20
|
+
else
|
|
21
|
+
assertExportValue(value);
|
|
22
|
+
}
|
|
23
|
+
/** @internal Buffered samples and footer definitions capture mutable Date values. */
|
|
24
|
+
export function copyValue(value) {
|
|
25
|
+
if (value instanceof FormulaCell)
|
|
26
|
+
return value;
|
|
27
|
+
if (value instanceof Cell)
|
|
28
|
+
return Object.freeze(new Cell(copyExportValue(value.value), value.style));
|
|
29
|
+
return copyExportValue(value);
|
|
30
|
+
}
|
|
31
|
+
export function cellText(value, policy = "strip") {
|
|
32
|
+
const text = cleanXml(value, policy);
|
|
33
|
+
if (text.length > 32767)
|
|
34
|
+
throw new RangeError("Excel cell text exceeds 32,767 UTF-16 code units.");
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
export function inlineText(value, policy) {
|
|
38
|
+
const text = cellText(value, policy), node = (part) => '<t xml:space="preserve">' + escapeXml(part, policy) + '</t>';
|
|
39
|
+
// OOXML escapes are decoded per run. Splitting the initial underscore preserves literal text in both reader kinds.
|
|
40
|
+
return /_x[0-9a-f]{4}_/i.test(text)
|
|
41
|
+
? text.split(/(?<=_)(?=x[0-9a-f]{4}_)/gi).map(part => '<r>' + node(part) + '</r>').join("") : node(text);
|
|
42
|
+
}
|
|
43
|
+
export function columnName(index) {
|
|
44
|
+
if (!Number.isInteger(index) || index < 1 || index > 16384)
|
|
45
|
+
throw new RangeError("Column index must be from 1 through 16,384.");
|
|
46
|
+
let name = "";
|
|
47
|
+
while (index) {
|
|
48
|
+
index--;
|
|
49
|
+
name = String.fromCharCode(65 + index % 26) + name;
|
|
50
|
+
index = Math.floor(index / 26);
|
|
51
|
+
}
|
|
52
|
+
return name;
|
|
53
|
+
}
|
|
54
|
+
function clipName(text, length) { return text.slice(0, length).replace(/[\ud800-\udbff]$/, ""); }
|
|
55
|
+
export function sheetName(requested, names, policy, register = true) {
|
|
56
|
+
if (typeof requested !== "string")
|
|
57
|
+
throw new TypeError("Sheet name must be a string.");
|
|
58
|
+
// Validate and deduplicate the decoded literal value, before ST_Xstring attribute encoding.
|
|
59
|
+
// A requested _xHHHH_ token is literal text; the serializer protects its leading underscore.
|
|
60
|
+
let base = cleanXml(requested, policy).replace(/[\[\]:*?/\\]/g, "_").replace(/^[\s']+|[\s']+$/g, "");
|
|
61
|
+
// Clipping can expose an internal apostrophe or the reserved History name.
|
|
62
|
+
base = clipName(base, 31).replace(/[\s']+$/g, "");
|
|
63
|
+
if (!base)
|
|
64
|
+
base = "Sheet";
|
|
65
|
+
if (base.toLowerCase() === "history")
|
|
66
|
+
base += "_";
|
|
67
|
+
let name = base, suffix = 2;
|
|
68
|
+
while (names.has(name.toLowerCase())) {
|
|
69
|
+
const tail = " (" + suffix++ + ")";
|
|
70
|
+
name = clipName(base, 31 - tail.length) + tail;
|
|
71
|
+
}
|
|
72
|
+
if (register)
|
|
73
|
+
names.add(name.toLowerCase());
|
|
74
|
+
return name;
|
|
75
|
+
}
|
|
76
|
+
export function excelDate(date, mode) {
|
|
77
|
+
if (!Number.isFinite(date.getTime()))
|
|
78
|
+
return null;
|
|
79
|
+
const utc = mode === "utc", year = utc ? date.getUTCFullYear() : date.getFullYear();
|
|
80
|
+
if (year < 1900 || year > 9999)
|
|
81
|
+
throw new RangeError("Excel dates must be in years 1900 through 9999.");
|
|
82
|
+
const wall = new Date(0);
|
|
83
|
+
wall.setUTCFullYear(year, utc ? date.getUTCMonth() : date.getMonth(), utc ? date.getUTCDate() : date.getDate());
|
|
84
|
+
wall.setUTCHours(utc ? date.getUTCHours() : date.getHours(), utc ? date.getUTCMinutes() : date.getMinutes(), utc ? date.getUTCSeconds() : date.getSeconds(), utc ? date.getUTCMilliseconds() : date.getMilliseconds());
|
|
85
|
+
const time = wall.getTime();
|
|
86
|
+
return (time - Date.UTC(1899, 11, 31)) / 86400000 + (time >= Date.UTC(1900, 2, 1) ? 1 : 0);
|
|
87
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { StyleRegistry } from "./styles.js";
|
|
2
|
+
import { Worksheet } from "./worksheet.js";
|
|
3
|
+
import type { WorkbookOptions, SheetOptions, ExtraPart, XlsxExportResult } from "./types.js";
|
|
4
|
+
/** Streaming writer model; append rows through Worksheets, then finalize once. */
|
|
5
|
+
export declare class Workbook {
|
|
6
|
+
private portableValues;
|
|
7
|
+
readonly styles: StyleRegistry;
|
|
8
|
+
private readonly names;
|
|
9
|
+
private readonly sheets;
|
|
10
|
+
private readonly tableNames;
|
|
11
|
+
private tableCount;
|
|
12
|
+
private readonly writers;
|
|
13
|
+
private readonly package;
|
|
14
|
+
private state;
|
|
15
|
+
private result;
|
|
16
|
+
private completion;
|
|
17
|
+
private activeSheet;
|
|
18
|
+
private failure;
|
|
19
|
+
private overflow;
|
|
20
|
+
private links;
|
|
21
|
+
private imageBytes;
|
|
22
|
+
private bufferedBytes;
|
|
23
|
+
private mergedRanges;
|
|
24
|
+
private conditionalFormats;
|
|
25
|
+
private dataValidationCount;
|
|
26
|
+
private validationTextCharacters;
|
|
27
|
+
private formulaCharacters;
|
|
28
|
+
private hasAuthoredFormulas;
|
|
29
|
+
private readonly nativeCharts;
|
|
30
|
+
private chartSourceSheets;
|
|
31
|
+
private chartPoints;
|
|
32
|
+
private chartTextCharacters;
|
|
33
|
+
private registering;
|
|
34
|
+
/** Caller-owned property getters may run during capture; they cannot register another workbook object. */
|
|
35
|
+
private registration;
|
|
36
|
+
constructor(options?: WorkbookOptions);
|
|
37
|
+
private addXmlPart;
|
|
38
|
+
private workbookXml;
|
|
39
|
+
private checkSheetLimit;
|
|
40
|
+
private checkChartText;
|
|
41
|
+
private resolveChartBinding;
|
|
42
|
+
get worksheets(): readonly Worksheet[];
|
|
43
|
+
/** Total report data rows; excludes title/header/footer and preservation records. */
|
|
44
|
+
get rowCount(): number;
|
|
45
|
+
addWorksheet<T = never>(name: string, options?: SheetOptions<T>): Worksheet<T>;
|
|
46
|
+
addPart(part: ExtraPart): void;
|
|
47
|
+
/** Complete the archive and return counts. Does not close or dispose a caller-owned sink. */
|
|
48
|
+
finish(): Promise<XlsxExportResult>;
|
|
49
|
+
toBlob(): Promise<Blob>;
|
|
50
|
+
}
|
|
@@ -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
|
+
}
|