@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.
- package/LICENSE +21 -0
- package/README.md +1219 -2
- package/bundles/officeimo-canopyx.js +11286 -0
- package/bundles/officeimo-canopyx.mjs +11185 -0
- package/bundles/officeimo-csv.js +1254 -0
- package/bundles/officeimo-csv.mjs +1229 -0
- package/bundles/officeimo-datatables.js +11572 -0
- package/bundles/officeimo-datatables.mjs +11470 -0
- package/bundles/officeimo-execution.js +1503 -0
- package/bundles/officeimo-execution.mjs +1479 -0
- package/bundles/officeimo-pdf.js +6564 -0
- package/bundles/officeimo-pdf.mjs +6502 -0
- package/bundles/officeimo-xlsx.js +5333 -0
- package/bundles/officeimo-xlsx.mjs +5279 -0
- package/bundles/officeimo.js +11022 -0
- package/bundles/officeimo.mjs +10924 -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 +72 -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 +101 -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 +116 -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 +68 -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 +194 -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 +201 -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 +142 -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 +143 -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 +679 -0
- package/dist/pdf/paragraph-layout.d.ts +19 -0
- package/dist/pdf/paragraph-layout.js +66 -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 +15 -0
- package/dist/pdf/settings.js +171 -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 +250 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +114 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +85 -0
- package/dist/pdf/table-layout.d.ts +71 -0
- package/dist/pdf/table-layout.js +205 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +92 -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 +96 -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 +169 -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/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 +86 -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,700 @@
|
|
|
1
|
+
import { checkAbort, consumeRows, inputRows, pause, taskYieldDue } from "../core/iteration.js";
|
|
2
|
+
import { ChunkedTextSink, BlobByteSink } from "../core/sinks.js";
|
|
3
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
4
|
+
import { notifyProgress } from "../core/progress.js";
|
|
5
|
+
import { copyColumns, createRowProjector } from "../internal/rows.js";
|
|
6
|
+
import { EntryWriter } from "../zip/entry.js";
|
|
7
|
+
import { xmlDeclaration } from "../xml/index.js";
|
|
8
|
+
import { officeRelationshipsNamespace } from "../opc/index.js";
|
|
9
|
+
import { Cell, cellText, columnName, inlineText, excelDate, copyValue } from "./values.js";
|
|
10
|
+
import { spreadsheetNamespace, colorArgb, validateStylePatch, copyStylePatch } from "./styles.js";
|
|
11
|
+
import { copyHyperlink, copyImage, hyperlinksXml, cellPosition } from "./attachments.js";
|
|
12
|
+
import { ExportCell, assertScalar } from "../core/presentation.js";
|
|
13
|
+
import { ReportLayout, ComputedTotal, printXml } from "./layout.js";
|
|
14
|
+
import { cleanXml } from "../xml/index.js";
|
|
15
|
+
import { ConditionalFormats, prepareConditionalFormats } from "./conditional-formatting.js";
|
|
16
|
+
import { isDate } from "../core/dates.js";
|
|
17
|
+
import { FormulaCell, formulaXml, isSubtotalFormula } from "./formula.js";
|
|
18
|
+
import { prepareDataValidations, dataValidationsXml } from "./validation.js";
|
|
19
|
+
import { qualifiedCell, qualifiedColumnRange, resolveDataColumn } from "./ranges.js";
|
|
20
|
+
import { dataRowRanges, dataRowCount } from "./data-row-ranges.js";
|
|
21
|
+
const maximumHyperlinks = 65530;
|
|
22
|
+
function checkHyperlinkCount(count) {
|
|
23
|
+
if (count > maximumHyperlinks)
|
|
24
|
+
throw new RangeError("Excel supports at most 65,530 hyperlinks per worksheet; split the sheet.");
|
|
25
|
+
}
|
|
26
|
+
/** Worksheet rows are written once in order; the model retains compressed output rather than source data. */
|
|
27
|
+
export class Worksheet {
|
|
28
|
+
book;
|
|
29
|
+
table;
|
|
30
|
+
preserved;
|
|
31
|
+
chartData;
|
|
32
|
+
validations;
|
|
33
|
+
columns;
|
|
34
|
+
project;
|
|
35
|
+
/** Final unique workbook name, captured before row projection begins. */
|
|
36
|
+
name;
|
|
37
|
+
declared;
|
|
38
|
+
options;
|
|
39
|
+
entry;
|
|
40
|
+
completion;
|
|
41
|
+
prepared;
|
|
42
|
+
buffer;
|
|
43
|
+
output = new BlobByteSink();
|
|
44
|
+
started = false;
|
|
45
|
+
busy = false;
|
|
46
|
+
failed = false;
|
|
47
|
+
error;
|
|
48
|
+
count = 0;
|
|
49
|
+
headerRows;
|
|
50
|
+
layout;
|
|
51
|
+
completedRegions;
|
|
52
|
+
titleStyle;
|
|
53
|
+
pending = [];
|
|
54
|
+
pendingCharacters = 0;
|
|
55
|
+
reservedLayout = false;
|
|
56
|
+
internalLinks = [];
|
|
57
|
+
links = [];
|
|
58
|
+
linkedCells = new Set();
|
|
59
|
+
pictures = [];
|
|
60
|
+
nativeCharts = [];
|
|
61
|
+
conditional;
|
|
62
|
+
completed = false;
|
|
63
|
+
chartSources = [];
|
|
64
|
+
constructor(book, name, options, table, preserved = false, conditional = [], chartData = false, validations = { rules: [], count: 0, textCharacters: 0 }) {
|
|
65
|
+
this.book = book;
|
|
66
|
+
this.table = table;
|
|
67
|
+
this.preserved = preserved;
|
|
68
|
+
this.chartData = chartData;
|
|
69
|
+
this.validations = validations;
|
|
70
|
+
this.columns = copyColumns(options.columns ?? [], true).map(c => Object.freeze(c));
|
|
71
|
+
const alternate = options.alternatingRowStyle;
|
|
72
|
+
this.options = { ...options, ...(options.autoSize ? { autoSize: { ...options.autoSize } } : {}),
|
|
73
|
+
...(options.mergedCells ? { mergedCells: [...options.mergedCells] } : {}),
|
|
74
|
+
...(options.title ? { title: { ...options.title, ...(options.title.style ? { style: copyStylePatch(options.title.style) } : {}) } } : {}),
|
|
75
|
+
...(options.print ? { print: { ...options.print, ...(options.print.margins ? { margins: { ...options.print.margins } } : {}) } } : {}),
|
|
76
|
+
...(options.footer ? { footer: { ...options.footer, ...(options.footer.values ? { values: options.footer.values.map(copyValue) } : {}), ...(options.footer.totals ? { totals: { ...options.footer.totals } } : {}), ...(options.footer.style ? { style: copyStylePatch(options.footer.style) } : {}) } } : {}),
|
|
77
|
+
...(alternate ? { alternatingRowStyle: copyStylePatch(alternate) } : {}) };
|
|
78
|
+
this.layout = new ReportLayout(this.columns, this.options, book.settings.invalidCharacterPolicy, book.settings.limits?.maxMergedRanges ?? 10000, book.settings.limits?.maxBufferedCells ?? 100000);
|
|
79
|
+
this.titleStyle = options.title ? book.styles.compose(book.styles.compose(0, { font: { bold: true, size: 18 } }), this.options.title.style ?? {}) : 0;
|
|
80
|
+
this.headerRows = this.layout.headerRows;
|
|
81
|
+
for (const link of options.hyperlinks ?? []) {
|
|
82
|
+
const copied = copyHyperlink(link, book.settings.invalidCharacterPolicy);
|
|
83
|
+
this.links.push(copied);
|
|
84
|
+
this.linkedCells.add(copied.cell);
|
|
85
|
+
}
|
|
86
|
+
book.checkLinks(this.links.length);
|
|
87
|
+
this.declared = this.columns.map((column, i) => ({ column, letter: columnName(i + 1),
|
|
88
|
+
style: book.styles.forColumn(column), dateStyle: book.styles.forColumn(column, false, undefined, true),
|
|
89
|
+
headerStyle: this.headerRows ? options.headerStyle ?? book.styles.forColumn({ header: column.header, ...(column.wrapText === undefined ? {} : { wrapText: column.wrapText }),
|
|
90
|
+
...(column.alignment === undefined ? {} : { alignment: column.alignment }) }, options.boldHeader !== false, options.headerFill) : 0
|
|
91
|
+
}));
|
|
92
|
+
// Register conditional styles only after worksheet construction can no longer reject its options.
|
|
93
|
+
this.conditional = new ConditionalFormats(conditional, book.styles);
|
|
94
|
+
book.retainLink(this.links.length);
|
|
95
|
+
// Application getters may allocate an overflow sheet while metadata is captured.
|
|
96
|
+
// Resolve the name only after those callbacks, before binding any row context.
|
|
97
|
+
this.name = typeof name === "function" ? name() : name;
|
|
98
|
+
this.project = createRowProjector(this.columns, { sheetName: this.name, firstDataRow: this.headerRows + 1 }, book.settings.signal, book.valueValidator);
|
|
99
|
+
}
|
|
100
|
+
/** @internal */
|
|
101
|
+
static create(book, name, options, table, preserved = false, conditional, validations) { return new Worksheet(book, name, options, table, preserved, conditional, false, validations); }
|
|
102
|
+
/** @internal Generated source cells use the normal worksheet serializer and budgets. */
|
|
103
|
+
static createChartData(book, chart) {
|
|
104
|
+
return new Worksheet(book, chart.dataSheetName, { columns: [{ header: "Category" }, ...chart.data.series.map(series => ({ header: series.name, type: "number" }))] }, undefined, false, [], true);
|
|
105
|
+
}
|
|
106
|
+
/** @internal Validate before allocating native compressor resources or registering the sheet name. */
|
|
107
|
+
static validate(book, options) {
|
|
108
|
+
const columns = copyColumns(options.columns ?? [], true);
|
|
109
|
+
const validations = prepareDataValidations(options.dataValidation, columns, book.availableDataValidations, book.availableValidationTextCharacters, book.settings.signal);
|
|
110
|
+
book.checkConditionalFormats(options.conditionalFormats?.length ?? 0);
|
|
111
|
+
const conditional = prepareConditionalFormats(options.conditionalFormats, columns, book.settings.invalidCharacterPolicy);
|
|
112
|
+
book.styles.checkDifferentials(conditional.flatMap(rule => rule.style ? [rule.style] : []));
|
|
113
|
+
const layout = new ReportLayout(columns, options, book.settings.invalidCharacterPolicy, book.settings.limits?.maxMergedRanges ?? 10000, book.settings.limits?.maxBufferedCells ?? 100000);
|
|
114
|
+
book.checkMerges(layout.mergeCount);
|
|
115
|
+
if (options.title?.style)
|
|
116
|
+
validateStylePatch(options.title.style);
|
|
117
|
+
if (options.title)
|
|
118
|
+
book.styles.compose(book.styles.compose(0, { font: { bold: true, size: 18 } }), options.title.style ?? {});
|
|
119
|
+
if (options.footer?.style)
|
|
120
|
+
book.styles.compose(0, options.footer.style);
|
|
121
|
+
if (options.hyperlinks !== undefined) {
|
|
122
|
+
if (!Array.isArray(options.hyperlinks))
|
|
123
|
+
throw new TypeError("Hyperlinks must be an array.");
|
|
124
|
+
checkHyperlinkCount(options.hyperlinks.length);
|
|
125
|
+
const seen = new Set();
|
|
126
|
+
for (const requested of options.hyperlinks) {
|
|
127
|
+
const link = copyHyperlink(requested, book.settings.invalidCharacterPolicy);
|
|
128
|
+
const position = cellPosition(link.cell);
|
|
129
|
+
if (layout.regions.covered(position.column, position.row))
|
|
130
|
+
throw new TypeError("Hyperlinks cannot address a covered merged cell.");
|
|
131
|
+
if (seen.has(link.cell))
|
|
132
|
+
throw new TypeError("Duplicate hyperlink cell: " + link.cell);
|
|
133
|
+
seen.add(link.cell);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (columns.length > 16384)
|
|
137
|
+
throw new RangeError("Excel supports at most 16,384 columns.");
|
|
138
|
+
if (options.includeHeader === false && (options.freezeHeader || options.autoFilter))
|
|
139
|
+
throw new TypeError("A frozen header or autofilter requires a header row.");
|
|
140
|
+
if (options.headerFill !== undefined)
|
|
141
|
+
colorArgb(options.headerFill);
|
|
142
|
+
if (options.headerStyle !== undefined)
|
|
143
|
+
book.styles.validateStyle(options.headerStyle);
|
|
144
|
+
if (options.alternatingRowStyle !== undefined)
|
|
145
|
+
book.styles.compose(0, options.alternatingRowStyle);
|
|
146
|
+
for (const callback of [options.rowStyle, options.cellStyle, options.rowOutline])
|
|
147
|
+
if (callback !== undefined && typeof callback !== "function")
|
|
148
|
+
throw new TypeError("Row and style callbacks must be functions.");
|
|
149
|
+
if (options.maxOutlineLevel !== undefined && (!Number.isInteger(options.maxOutlineLevel) || options.maxOutlineLevel < 0 || options.maxOutlineLevel > 7))
|
|
150
|
+
throw new RangeError("Maximum outline level must be an integer from zero through seven.");
|
|
151
|
+
if (options.rowOutline && options.maxOutlineLevel === undefined)
|
|
152
|
+
throw new TypeError("A row outline callback requires maxOutlineLevel before streamed rows are written.");
|
|
153
|
+
if (!options.rowOutline && options.maxOutlineLevel !== undefined)
|
|
154
|
+
throw new TypeError("maxOutlineLevel requires a row outline callback.");
|
|
155
|
+
for (const height of [options.rowHeight, options.headerHeight])
|
|
156
|
+
if (height !== undefined && (!Number.isFinite(height) || height <= 0 || height > 409))
|
|
157
|
+
throw new RangeError("Row height must be positive and at most 409 points.");
|
|
158
|
+
if (options.freezeColumns !== undefined && (!Number.isInteger(options.freezeColumns) || options.freezeColumns < 0 || options.freezeColumns > columns.length || options.freezeColumns >= 16384))
|
|
159
|
+
throw new RangeError("Frozen columns must be within the declared columns and leave a valid scrollable column.");
|
|
160
|
+
if (options.table && options.includeHeader === false)
|
|
161
|
+
throw new TypeError("Excel tables require a header row.");
|
|
162
|
+
if (options.defaultColumnWidth !== undefined && (!Number.isFinite(options.defaultColumnWidth) || options.defaultColumnWidth < 0 || options.defaultColumnWidth > 255))
|
|
163
|
+
throw new RangeError("Default column width must be from 0 through 255 characters.");
|
|
164
|
+
for (const c of columns) {
|
|
165
|
+
if (c.width !== undefined && (!Number.isFinite(c.width) || c.width < 0 || c.width > 255))
|
|
166
|
+
throw new RangeError("Column width must be from 0 through 255 characters.");
|
|
167
|
+
if (c.type !== undefined && !["string", "number", "boolean", "date"].includes(c.type) && !book.writerFor(c.type))
|
|
168
|
+
throw new TypeError("Invalid column type: " + c.type);
|
|
169
|
+
if (c.alignment !== undefined && !["left", "center", "right", "fill", "justify", "distributed"].includes(c.alignment))
|
|
170
|
+
throw new TypeError("Invalid horizontal alignment.");
|
|
171
|
+
cellText(c.header, book.settings.invalidCharacterPolicy);
|
|
172
|
+
if (c.style !== undefined)
|
|
173
|
+
book.styles.validateStyle(c.style);
|
|
174
|
+
if (c.format !== undefined && (typeof c.format !== "string" || c.format.length > 255))
|
|
175
|
+
throw new TypeError("Column format must be a string of at most 255 characters.");
|
|
176
|
+
}
|
|
177
|
+
return { conditional, validations };
|
|
178
|
+
}
|
|
179
|
+
cell(value, i, row, header = false, rowStyle, context, rowStyles, footer = false, title = false, visible = true) {
|
|
180
|
+
const col = this.declared[i];
|
|
181
|
+
const total = value instanceof ComputedTotal ? value : undefined;
|
|
182
|
+
if (total)
|
|
183
|
+
value = total.value;
|
|
184
|
+
let formula = value instanceof FormulaCell ? value : undefined;
|
|
185
|
+
if (formula)
|
|
186
|
+
value = formula.cachedValue;
|
|
187
|
+
let presentation = value instanceof ExportCell ? value.presentation : undefined;
|
|
188
|
+
let link = value instanceof ExportCell ? value.link : undefined;
|
|
189
|
+
if (value instanceof ExportCell)
|
|
190
|
+
value = value.value;
|
|
191
|
+
const suppliedStyle = formula?.style ?? (value instanceof Cell ? value.style : undefined);
|
|
192
|
+
const covered = (this.completedRegions ?? this.layout.regions).covered(i + 1, row);
|
|
193
|
+
const input = value instanceof Cell ? value.value : value, emptyCovered = covered && (input == null || input === "");
|
|
194
|
+
if (!header && !footer && col.column.type && !emptyCovered) {
|
|
195
|
+
const writer = this.book.writerFor(col.column.type);
|
|
196
|
+
if (writer) {
|
|
197
|
+
if (formula)
|
|
198
|
+
throw new TypeError("An explicit FormulaCell cannot also pass through a custom cell value writer; return it from the writer instead.");
|
|
199
|
+
value = writer(value instanceof Cell ? value.value : value, { column: col.column, rowIndex: row - this.headerRows - 1, worksheetRow: row, columnIndex: i, sheetName: this.name });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (value instanceof FormulaCell) {
|
|
203
|
+
formula = value;
|
|
204
|
+
value = formula.cachedValue;
|
|
205
|
+
}
|
|
206
|
+
if (value instanceof ExportCell) {
|
|
207
|
+
presentation = value.presentation;
|
|
208
|
+
link = value.link;
|
|
209
|
+
value = value.value;
|
|
210
|
+
}
|
|
211
|
+
const explicitStyle = formula?.style ?? (value instanceof Cell ? value.style ?? suppliedStyle : suppliedStyle);
|
|
212
|
+
if (value instanceof Cell)
|
|
213
|
+
value = value.value;
|
|
214
|
+
assertScalar(value);
|
|
215
|
+
if (covered) {
|
|
216
|
+
if (link)
|
|
217
|
+
throw new TypeError("Hyperlinks cannot address a covered merged cell.");
|
|
218
|
+
if (formula || total || (value != null && value !== ""))
|
|
219
|
+
throw new TypeError("A merged range would hide the value at " + col.letter + row + "; covered cells must be empty.");
|
|
220
|
+
value = undefined; // An empty string in a covered typed column is an empty cell, not a type mismatch.
|
|
221
|
+
}
|
|
222
|
+
const type = isDate(value) ? "date" : typeof value;
|
|
223
|
+
if (!header && !footer && value != null && col.column.type && !this.book.writerFor(col.column.type) && col.column.type !== type)
|
|
224
|
+
throw new TypeError("Cell " + col.letter + row + " does not match column type " + col.column.type + ".");
|
|
225
|
+
let style = explicitStyle === undefined ? header ? col.headerStyle : total?.operation === "count" ? 0 : type === "date" ? col.dateStyle : col.style : this.book.styles.validateStyle(explicitStyle);
|
|
226
|
+
if (header && type === "date" && explicitStyle === undefined && this.options.headerStyle === undefined)
|
|
227
|
+
style = this.book.styles.dateHeader(style, col.dateStyle);
|
|
228
|
+
if (title)
|
|
229
|
+
style = this.titleStyle;
|
|
230
|
+
if (!header && !footer) {
|
|
231
|
+
if (explicitStyle === undefined) {
|
|
232
|
+
const base = style, cached = rowStyles?.get(base);
|
|
233
|
+
if (cached !== undefined)
|
|
234
|
+
style = cached;
|
|
235
|
+
else {
|
|
236
|
+
if ((row - this.headerRows) % 2 === 0 && this.options.alternatingRowStyle)
|
|
237
|
+
style = this.book.styles.compose(style, this.options.alternatingRowStyle);
|
|
238
|
+
if (rowStyle !== undefined)
|
|
239
|
+
style = this.book.styles.compose(style, rowStyle);
|
|
240
|
+
rowStyles?.set(base, style);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const patch = context && this.options.cellStyle?.({ ...context, value: value, column: col.column, columnIndex: i });
|
|
244
|
+
if (patch !== undefined)
|
|
245
|
+
style = this.book.styles.compose(style, patch);
|
|
246
|
+
}
|
|
247
|
+
if (footer && this.options.footer?.style && explicitStyle === undefined)
|
|
248
|
+
style = this.book.styles.compose(style, this.options.footer.style);
|
|
249
|
+
if (presentation && explicitStyle === undefined)
|
|
250
|
+
style = this.book.styles.compose(style, {
|
|
251
|
+
...(presentation.background === undefined ? {} : { fill: { color: presentation.background } }),
|
|
252
|
+
...(presentation.color === undefined && presentation.bold === undefined && presentation.italic === undefined ? {} : { font: {
|
|
253
|
+
...(presentation.color === undefined ? {} : { color: presentation.color }), ...(presentation.bold === undefined ? {} : { bold: presentation.bold }), ...(presentation.italic === undefined ? {} : { italic: presentation.italic })
|
|
254
|
+
} }),
|
|
255
|
+
...(presentation.wrapText === undefined ? {} : { wrapText: presentation.wrapText }), ...(presentation.alignment === undefined ? {} : { alignment: presentation.alignment }),
|
|
256
|
+
...(presentation.numberFormat === undefined ? {} : { numberFormat: presentation.numberFormat })
|
|
257
|
+
});
|
|
258
|
+
const originalCharacters = typeof value === "string" ? value.length : 0;
|
|
259
|
+
let reservedCharacters = this.preserved || header || footer ? originalCharacters : undefined;
|
|
260
|
+
if (type === "string" && cleanXml(value, this.book.settings.invalidCharacterPolicy).length > 32767) {
|
|
261
|
+
const cell = col.letter + row;
|
|
262
|
+
if (link || this.linkedCells.has(cell))
|
|
263
|
+
throw new TypeError("A text-preservation cell cannot also have an external hyperlink.");
|
|
264
|
+
checkHyperlinkCount(this.links.length + this.internalLinks.length + 1);
|
|
265
|
+
this.book.checkLinks(1);
|
|
266
|
+
// Replace a generated cell's original reservation with its preview and overflow records.
|
|
267
|
+
if (reservedCharacters !== undefined) {
|
|
268
|
+
this.book.budget.release(1, reservedCharacters);
|
|
269
|
+
reservedCharacters = undefined;
|
|
270
|
+
}
|
|
271
|
+
const preserved = this.book.preserveText(this.name, cell, value);
|
|
272
|
+
this.book.retainLink();
|
|
273
|
+
this.internalLinks.push({ cell, location: preserved.location });
|
|
274
|
+
this.linkedCells.add(cell);
|
|
275
|
+
value = preserved.preview;
|
|
276
|
+
}
|
|
277
|
+
if (link)
|
|
278
|
+
this.appendHyperlink({ cell: col.letter + row, ...link });
|
|
279
|
+
if (type === "date")
|
|
280
|
+
value = excelDate(value, this.book.settings.dateMode);
|
|
281
|
+
if (!header && !footer && visible && (!formula || !isSubtotalFormula(formula)))
|
|
282
|
+
this.layout.accept(i, value);
|
|
283
|
+
if (!header && !footer && this.chartSources.length)
|
|
284
|
+
for (const binding of this.chartSources)
|
|
285
|
+
binding.observe(row - this.headerRows - 1, i + 1, value);
|
|
286
|
+
const prefix = '<c r="' + col.letter + row + '" s="' + style + '"';
|
|
287
|
+
if (formula)
|
|
288
|
+
this.book.retainFormula(formula.formula.length);
|
|
289
|
+
this.book.budget.cell(formula ? formula.formula : value, reservedCharacters);
|
|
290
|
+
if (formula)
|
|
291
|
+
return prefix + (value === null ? ' t="str"' : "") + '>' + formulaXml(formula) + '</c>';
|
|
292
|
+
if (total)
|
|
293
|
+
return prefix + (value === null ? ' t="str"' : "") + '><f>' + total.formula.replace(/"/g, """) + '</f><v>' + (value ?? "") + '</v></c>';
|
|
294
|
+
if (value == null || (type === "number" && !Number.isFinite(value)))
|
|
295
|
+
return prefix + '/>';
|
|
296
|
+
if (value == null)
|
|
297
|
+
return prefix + '/>';
|
|
298
|
+
if (type === "string")
|
|
299
|
+
return prefix + ' t="inlineStr"><is>' + inlineText(value, this.book.settings.invalidCharacterPolicy) + '</is></c>';
|
|
300
|
+
if (type === "boolean")
|
|
301
|
+
return prefix + ' t="b"><v>' + (value ? 1 : 0) + '</v></c>';
|
|
302
|
+
if (type !== "number" && type !== "date")
|
|
303
|
+
throw new TypeError("Excel cells must be strings, numbers, booleans, Dates or null.");
|
|
304
|
+
return prefix + '><v>' + value + '</v></c>';
|
|
305
|
+
}
|
|
306
|
+
*rowXml(values, number, header, footer = false, title = false) {
|
|
307
|
+
const height = title ? this.options.title?.height ?? 28 : header ? this.options.headerHeight : this.options.rowHeight;
|
|
308
|
+
const context = !header && !footer && (this.options.rowStyle || this.options.cellStyle || this.options.rowOutline) ? Object.freeze({ rowIndex: number - this.headerRows - 1, worksheetRow: number, sheetName: this.name,
|
|
309
|
+
values: Object.freeze(this.columns.map((_, i) => values[i] instanceof FormulaCell ? values[i].cachedValue : values[i] instanceof Cell || values[i] instanceof ExportCell ? values[i].value : values[i])) }) : undefined;
|
|
310
|
+
const rowStyle = context && this.options.rowStyle?.(context);
|
|
311
|
+
if (rowStyle !== undefined)
|
|
312
|
+
validateStylePatch(rowStyle);
|
|
313
|
+
const outline = this.outline(context);
|
|
314
|
+
const rowStyles = !header && (rowStyle || this.options.alternatingRowStyle) ? new Map() : undefined;
|
|
315
|
+
yield '<row r="' + number + '"' + (height === undefined ? "" : ' ht="' + height + '" customHeight="1"') +
|
|
316
|
+
(outline?.level ? ' outlineLevel="' + outline.level + '"' : "") + (outline?.hidden ? ' hidden="1"' : "") + (outline?.collapsed ? ' collapsed="1"' : "") + '>';
|
|
317
|
+
for (let i = 0; i < this.columns.length; i++)
|
|
318
|
+
yield this.cell(values[i], i, number, header, rowStyle, context, rowStyles, footer, title, outline?.hidden !== true);
|
|
319
|
+
yield '</row>';
|
|
320
|
+
}
|
|
321
|
+
outline(context) {
|
|
322
|
+
if (!context || !this.options.rowOutline)
|
|
323
|
+
return undefined;
|
|
324
|
+
const requested = this.options.rowOutline(context);
|
|
325
|
+
if (requested === undefined)
|
|
326
|
+
return undefined;
|
|
327
|
+
if (requested === null || typeof requested !== "object" || Array.isArray(requested))
|
|
328
|
+
throw new TypeError("Row outline callbacks must return metadata or undefined.");
|
|
329
|
+
if (typeof requested.then === "function") {
|
|
330
|
+
void Promise.resolve(requested).catch(() => { });
|
|
331
|
+
throw new TypeError("Row outline callbacks must return synchronous metadata.");
|
|
332
|
+
}
|
|
333
|
+
const { level: requestedLevel, hidden, collapsed } = requested, level = requestedLevel ?? 0;
|
|
334
|
+
if (!Number.isInteger(level) || level < 0 || level > this.options.maxOutlineLevel)
|
|
335
|
+
throw new RangeError("Row outline level exceeds the declared maximum.");
|
|
336
|
+
for (const value of [hidden, collapsed])
|
|
337
|
+
if (value !== undefined && typeof value !== "boolean")
|
|
338
|
+
throw new TypeError("Row outline flags must be booleans.");
|
|
339
|
+
return Object.freeze({ level, ...(hidden === undefined ? {} : { hidden }), ...(collapsed === undefined ? {} : { collapsed }) });
|
|
340
|
+
}
|
|
341
|
+
writeRow(values, number, header, footer = false, title = false) {
|
|
342
|
+
const chunks = this.rowXml(values, number, header, footer, title);
|
|
343
|
+
const append = () => {
|
|
344
|
+
while (true) {
|
|
345
|
+
const next = chunks.next();
|
|
346
|
+
if (next.done)
|
|
347
|
+
return;
|
|
348
|
+
if (this.buffer.append(next.value))
|
|
349
|
+
return this.buffer.flush().then(append);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
return append();
|
|
353
|
+
}
|
|
354
|
+
async sampleRow(values) {
|
|
355
|
+
if ((this.pending.length + 1) * this.columns.length > (this.book.settings.limits?.maxBufferedCells ?? 100000))
|
|
356
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "Width sampling cell limit exceeded; reduce sampleRows or raise its bounded limit.");
|
|
357
|
+
const encoded = [];
|
|
358
|
+
let chunks = 0;
|
|
359
|
+
for (const chunk of this.rowXml(values, this.count + this.headerRows + 1, false)) {
|
|
360
|
+
if (!this.started && this.pendingCharacters + chunk.length > (this.book.settings.limits?.maxBufferedCharacters ?? 1000000)) {
|
|
361
|
+
if (this.options.autoSize?.sampleRows !== undefined)
|
|
362
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "Width sampling text limit exceeded; reduce sampleRows or raise its bounded limit.");
|
|
363
|
+
// Resume this serialized row so callbacks, totals and links run only once.
|
|
364
|
+
this.layout.sample(values);
|
|
365
|
+
await this.start();
|
|
366
|
+
for (const retained of encoded)
|
|
367
|
+
if (this.buffer.append(retained))
|
|
368
|
+
await this.buffer.flush();
|
|
369
|
+
encoded.length = 0;
|
|
370
|
+
}
|
|
371
|
+
if (this.started) {
|
|
372
|
+
if (this.buffer.append(chunk))
|
|
373
|
+
await this.buffer.flush();
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
this.pendingCharacters += chunk.length;
|
|
377
|
+
encoded.push(chunk);
|
|
378
|
+
}
|
|
379
|
+
if (!this.started && (++chunks & 127) === 0 && taskYieldDue()) {
|
|
380
|
+
await pause();
|
|
381
|
+
checkAbort(this.book.settings.signal);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (!this.started) {
|
|
385
|
+
this.layout.sample(values);
|
|
386
|
+
this.pending.push(encoded);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
reserveLayout() {
|
|
390
|
+
if (this.reservedLayout)
|
|
391
|
+
return;
|
|
392
|
+
if (!this.preserved) {
|
|
393
|
+
let characters = 0;
|
|
394
|
+
if (this.options.title)
|
|
395
|
+
characters += this.options.title.text.length;
|
|
396
|
+
if (this.layout.firstHeaderRow) {
|
|
397
|
+
for (const heading of this.layout.headings)
|
|
398
|
+
for (const value of heading) {
|
|
399
|
+
const raw = value instanceof ExportCell || value instanceof Cell ? value.value : value;
|
|
400
|
+
if (typeof raw === "string")
|
|
401
|
+
characters += raw.length;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (this.layout.structuredFooter) {
|
|
405
|
+
for (const row of this.layout.structuredFooter.values)
|
|
406
|
+
for (const value of row) {
|
|
407
|
+
const raw = value instanceof ExportCell ? value.value : value;
|
|
408
|
+
if (typeof raw === "string")
|
|
409
|
+
characters += raw.length;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
else if (this.options.footer)
|
|
413
|
+
for (let i = 0; i < this.columns.length; i++) {
|
|
414
|
+
const column = this.columns[i], totals = this.options.footer.totals;
|
|
415
|
+
if (totals && Object.prototype.hasOwnProperty.call(totals, column.key ?? column.header))
|
|
416
|
+
continue;
|
|
417
|
+
const value = this.options.footer.values?.[i], raw = value instanceof ExportCell || value instanceof Cell ? value.value : value;
|
|
418
|
+
if (typeof raw === "string")
|
|
419
|
+
characters += raw.length;
|
|
420
|
+
}
|
|
421
|
+
this.book.budget.reserve(this.columns.length * (this.headerRows + this.layout.footerRows), characters);
|
|
422
|
+
}
|
|
423
|
+
this.reservedLayout = true;
|
|
424
|
+
}
|
|
425
|
+
async start() {
|
|
426
|
+
if (this.started)
|
|
427
|
+
return;
|
|
428
|
+
this.reserveLayout();
|
|
429
|
+
this.started = true;
|
|
430
|
+
this.entry = this.book.settings.sink ? await this.book.openSheet(this) : new EntryWriter(this.book.settings.compression, { write: bytes => { this.book.retainBufferedBytes(bytes.length); this.output.write(bytes); } }, this.book.settings.signal);
|
|
431
|
+
const buffer = this.buffer = new ChunkedTextSink(this.entry, this.book.settings.signal), opts = this.options;
|
|
432
|
+
const x = opts.freezeColumns ?? 0, y = opts.freezeHeader ? this.headerRows : 0;
|
|
433
|
+
const pane = x && y ? "bottomRight" : x ? "topRight" : "bottomLeft", cell = columnName(x + 1) + (y + 1);
|
|
434
|
+
await buffer.write(xmlDeclaration + '<worksheet xmlns="' + spreadsheetNamespace + '" xmlns:r="' + officeRelationshipsNamespace + '">' +
|
|
435
|
+
(opts.print || opts.rowOutline ? '<sheetPr>' + (opts.rowOutline ? '<outlinePr summaryBelow="1" showOutlineSymbols="1"/>' : "") + (opts.print ? '<pageSetUpPr fitToPage="1"/>' : "") + '</sheetPr>' : "") + '<sheetViews><sheetView workbookViewId="0">' +
|
|
436
|
+
(x || y ? '<pane' + (x ? ' xSplit="' + x + '"' : "") + (y ? ' ySplit="' + y + '"' : "") + ' topLeftCell="' + cell + '" activePane="' + pane + '" state="frozen"/><selection pane="' + pane + '" activeCell="' + cell + '" sqref="' + cell + '"/>' : "") + '</sheetView></sheetViews>' +
|
|
437
|
+
(opts.rowOutline ? '<sheetFormatPr defaultRowHeight="15" outlineLevelRow="' + opts.maxOutlineLevel + '"/>' : ""));
|
|
438
|
+
const defaultWidth = opts.defaultColumnWidth ?? (this.table ? 20 : undefined);
|
|
439
|
+
if (this.columns.some((_, i) => this.layout.widths[i] !== undefined || defaultWidth !== undefined)) {
|
|
440
|
+
await buffer.write('<cols>');
|
|
441
|
+
for (let i = 0; i < this.columns.length; i++) {
|
|
442
|
+
const width = this.layout.widths[i] ?? defaultWidth;
|
|
443
|
+
if (width !== undefined)
|
|
444
|
+
await buffer.write('<col min="' + (i + 1) + '" max="' + (i + 1) + '" width="' + width + '" customWidth="1"/>');
|
|
445
|
+
}
|
|
446
|
+
await buffer.write('</cols>');
|
|
447
|
+
}
|
|
448
|
+
await buffer.write('<sheetData>');
|
|
449
|
+
if (opts.title)
|
|
450
|
+
await this.writeRow(this.columns.map((_, i) => i ? "" : opts.title.text), 1, true, false, true);
|
|
451
|
+
if (this.layout.firstHeaderRow) {
|
|
452
|
+
for (let i = 0; i < this.layout.headings.length; i++)
|
|
453
|
+
await this.writeRow(this.layout.headings[i], i + this.layout.firstHeaderRow, true);
|
|
454
|
+
}
|
|
455
|
+
for (const row of this.pending)
|
|
456
|
+
for (const chunk of row)
|
|
457
|
+
if (buffer.append(chunk))
|
|
458
|
+
await buffer.flush();
|
|
459
|
+
this.pending = [];
|
|
460
|
+
this.pendingCharacters = 0;
|
|
461
|
+
}
|
|
462
|
+
async addRows(rows) {
|
|
463
|
+
this.book.assertOpen();
|
|
464
|
+
if (this.completion)
|
|
465
|
+
throw new OfficeIMOError("INVALID_STATE", "Worksheet is closed.");
|
|
466
|
+
if (this.busy)
|
|
467
|
+
throw new OfficeIMOError("INVALID_STATE", "Await the current addRows call before writing more rows to this sheet.");
|
|
468
|
+
if (this.failed)
|
|
469
|
+
throw this.error;
|
|
470
|
+
this.busy = true;
|
|
471
|
+
try {
|
|
472
|
+
this.reserveLayout();
|
|
473
|
+
if (!this.layout.sampleRows)
|
|
474
|
+
await this.start();
|
|
475
|
+
let checkpoint = performance.now();
|
|
476
|
+
const progress = () => { if (performance.now() - checkpoint >= 50) {
|
|
477
|
+
this.progress();
|
|
478
|
+
checkpoint = performance.now();
|
|
479
|
+
} };
|
|
480
|
+
const completed = () => {
|
|
481
|
+
this.count++;
|
|
482
|
+
if (!this.started && this.pending.length >= this.layout.sampleRows)
|
|
483
|
+
return this.start().then(progress);
|
|
484
|
+
if (!this.started && taskYieldDue())
|
|
485
|
+
return pause().then(() => { checkAbort(this.book.settings.signal); progress(); });
|
|
486
|
+
progress();
|
|
487
|
+
};
|
|
488
|
+
await consumeRows(rows, this.book.settings.signal, row => {
|
|
489
|
+
if (this.count + this.headerRows + this.layout.footerRows >= 1048576)
|
|
490
|
+
throw new RangeError("Excel supports at most 1,048,576 rows including headers and footers; split the sheet.");
|
|
491
|
+
this.book.budget.row(this.count + 1);
|
|
492
|
+
if (!this.columns.length)
|
|
493
|
+
throw new RangeError("Declare columns before adding rows.");
|
|
494
|
+
const values = this.project(row, this.count);
|
|
495
|
+
const pending = !this.started ? this.sampleRow(values) : this.writeRow(values, this.count + this.headerRows + 1, false);
|
|
496
|
+
return pending ? pending.then(completed) : completed();
|
|
497
|
+
});
|
|
498
|
+
if (this.buffer)
|
|
499
|
+
await this.buffer.flush();
|
|
500
|
+
this.progress();
|
|
501
|
+
checkAbort(this.book.settings.signal);
|
|
502
|
+
}
|
|
503
|
+
catch (error) {
|
|
504
|
+
this.failed = true;
|
|
505
|
+
this.error = error;
|
|
506
|
+
await this.book.discard(error);
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
finally {
|
|
510
|
+
this.busy = false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
progress() { if (!this.preserved && !this.chartData)
|
|
514
|
+
notifyProgress(this.book.settings.onProgress, { phase: "rows", rows: this.book.rowCount, sheetRows: this.reportRowCount, sheetName: this.name }); }
|
|
515
|
+
get rowCount() { return this.count; }
|
|
516
|
+
/** A finite formula reference to a completed data column, excluding report decorations.
|
|
517
|
+
* Empty data returns undefined; discontiguous grouped details require explicit detailRanges instead. */
|
|
518
|
+
dataRange(column) {
|
|
519
|
+
if (this.failed)
|
|
520
|
+
throw this.error;
|
|
521
|
+
if (!this.completed)
|
|
522
|
+
throw new OfficeIMOError("INVALID_STATE", "Close the worksheet before resolving its data range.");
|
|
523
|
+
const selected = resolveDataColumn(this.columns, column);
|
|
524
|
+
const ranges = dataRowRanges(this);
|
|
525
|
+
if (ranges !== undefined) {
|
|
526
|
+
if (ranges.length > 1)
|
|
527
|
+
throw new OfficeIMOError("INVALID_STATE", "Grouped details are discontiguous; use the grouped result's detailRanges for explicit formula references.");
|
|
528
|
+
const range = ranges[0];
|
|
529
|
+
return range ? qualifiedColumnRange(this.name, selected, range.firstRow, range.lastRow - range.firstRow + 1) : undefined;
|
|
530
|
+
}
|
|
531
|
+
return this.count ? qualifiedColumnRange(this.name, selected, this.headerRows + 1, this.count) : undefined;
|
|
532
|
+
}
|
|
533
|
+
/** @internal Immutable schema plus current source timing, used only by workbook-owned chart registration. */
|
|
534
|
+
get chartBindingMetadata() {
|
|
535
|
+
const leaf = this.layout.headings.at(-1);
|
|
536
|
+
return { name: this.name, columns: this.columns, headerRows: this.headerRows, rows: this.count, closed: this.completed || !!this.completion,
|
|
537
|
+
headerReferences: this.columns.map((column, index) => {
|
|
538
|
+
const cell = leaf?.[index], raw = cell instanceof ExportCell ? cell.value : cell;
|
|
539
|
+
return this.layout.firstHeaderRow && raw === column.header && !this.layout.regions.covered(index + 1, this.headerRows)
|
|
540
|
+
? qualifiedCell(this.name, index + 1, this.headerRows) : undefined;
|
|
541
|
+
}) };
|
|
542
|
+
}
|
|
543
|
+
/** @internal Only the workbook may attach a preflighted, bounded source observer. */
|
|
544
|
+
retainChartBinding(binding) { this.chartSources.push(binding); }
|
|
545
|
+
/** @internal Preservation records are metadata, not report data rows. */
|
|
546
|
+
get reportRowCount() { return this.preserved || this.chartData ? 0 : dataRowCount(this, this.headerRows, this.count); }
|
|
547
|
+
/** @internal Chart data is editable through Excel's Unhide worksheet command. */
|
|
548
|
+
get hidden() { return this.chartData; }
|
|
549
|
+
/** Capture a bounded native chart. Source cells and caches remain independent of later caller mutations. */
|
|
550
|
+
addChart(chart) {
|
|
551
|
+
this.book.assertOpen();
|
|
552
|
+
if (this.completion)
|
|
553
|
+
throw new OfficeIMOError("INVALID_STATE", "Worksheet is closed.");
|
|
554
|
+
if (this.failed)
|
|
555
|
+
throw this.error;
|
|
556
|
+
this.nativeCharts.push(this.book.registerChart(chart));
|
|
557
|
+
}
|
|
558
|
+
/** @internal */
|
|
559
|
+
get charts() { return this.nativeCharts; }
|
|
560
|
+
/** @internal Completed chart parts no longer need their source snapshots. */
|
|
561
|
+
releaseCharts() { this.nativeCharts.length = 0; }
|
|
562
|
+
/** Register a PNG such as a chart; bytes are copied so the caller can reuse its buffer. */
|
|
563
|
+
addImage(image) {
|
|
564
|
+
this.book.assertOpen();
|
|
565
|
+
if (this.completion)
|
|
566
|
+
throw new OfficeIMOError("INVALID_STATE", "Worksheet is closed.");
|
|
567
|
+
if (this.failed)
|
|
568
|
+
throw this.error;
|
|
569
|
+
const copied = copyImage(image, this.book.settings.invalidCharacterPolicy, bytes => this.book.checkImage(bytes));
|
|
570
|
+
this.book.retainImage(copied.data.length);
|
|
571
|
+
this.pictures.push(copied);
|
|
572
|
+
}
|
|
573
|
+
/** Attach an external report link to a cell without changing its literal value. */
|
|
574
|
+
addHyperlink(link) {
|
|
575
|
+
this.book.assertOpen();
|
|
576
|
+
if (this.completion)
|
|
577
|
+
throw new OfficeIMOError("INVALID_STATE", "Worksheet is closed.");
|
|
578
|
+
if (this.failed)
|
|
579
|
+
throw this.error;
|
|
580
|
+
this.appendHyperlink(link);
|
|
581
|
+
}
|
|
582
|
+
appendHyperlink(link) {
|
|
583
|
+
const copied = copyHyperlink(link, this.book.settings.invalidCharacterPolicy);
|
|
584
|
+
const position = cellPosition(copied.cell);
|
|
585
|
+
if (this.layout.regions.covered(position.column, position.row))
|
|
586
|
+
throw new TypeError("Hyperlinks cannot address a covered merged cell.");
|
|
587
|
+
if (this.linkedCells.has(copied.cell))
|
|
588
|
+
throw new TypeError("Duplicate hyperlink cell: " + copied.cell);
|
|
589
|
+
checkHyperlinkCount(this.links.length + this.internalLinks.length + 1);
|
|
590
|
+
this.book.retainLink();
|
|
591
|
+
this.links.push(copied);
|
|
592
|
+
this.linkedCells.add(copied.cell);
|
|
593
|
+
}
|
|
594
|
+
/** @internal */
|
|
595
|
+
get hyperlinks() { return this.links; }
|
|
596
|
+
/** @internal */
|
|
597
|
+
get images() { return this.pictures; }
|
|
598
|
+
/** @internal An empty export has no data table; it still emits the declared headers. */
|
|
599
|
+
get tableDefinition() { return this.count ? this.table : undefined; }
|
|
600
|
+
/** @internal */
|
|
601
|
+
get isBusy() { return this.busy; }
|
|
602
|
+
/** @internal */
|
|
603
|
+
get headerRowCount() { return this.headerRows; }
|
|
604
|
+
/** @internal */
|
|
605
|
+
get firstHeaderRow() { return this.layout.firstHeaderRow; }
|
|
606
|
+
/** @internal */
|
|
607
|
+
get mergeCount() { return this.layout.mergeCount; }
|
|
608
|
+
/** @internal */
|
|
609
|
+
get conditionalFormatCount() { return this.conditional.count; }
|
|
610
|
+
/** @internal Captured editing metadata reserves workbook-wide limits even for empty data. */
|
|
611
|
+
get dataValidations() { return this.validations; }
|
|
612
|
+
/** @internal */
|
|
613
|
+
get totalRows() { return Math.max(1, this.headerRows + this.count + this.layout.footerRows); }
|
|
614
|
+
/** @internal */
|
|
615
|
+
get lastColumn() { return columnName(Math.max(1, this.columns.length)); }
|
|
616
|
+
/** @internal */
|
|
617
|
+
get printSettings() { return this.options.print; }
|
|
618
|
+
/** @internal */
|
|
619
|
+
get footerSettings() { return this.layout.structuredFooter ? undefined : this.options.footer; }
|
|
620
|
+
/** Complete this worksheet. A streamed workbook can then start its next worksheet. */
|
|
621
|
+
async close() {
|
|
622
|
+
this.book.assertOpen();
|
|
623
|
+
if (this.busy)
|
|
624
|
+
throw new OfficeIMOError("INVALID_STATE", "Await addRows before closing the worksheet.");
|
|
625
|
+
try {
|
|
626
|
+
await this.finish();
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
await this.book.discard(error);
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
/** @internal */
|
|
634
|
+
finish(preservedRows) {
|
|
635
|
+
if (this.completion)
|
|
636
|
+
return this.completion;
|
|
637
|
+
if (this.failed)
|
|
638
|
+
throw this.error;
|
|
639
|
+
return this.completion = (async () => {
|
|
640
|
+
try {
|
|
641
|
+
const regions = this.completedRegions = this.layout.finalRegions(this.count, this.book.settings.limits?.maxMergedRanges ?? 10000);
|
|
642
|
+
regions.validateRows(this.totalRows);
|
|
643
|
+
for (const binding of this.chartSources)
|
|
644
|
+
binding.finish();
|
|
645
|
+
this.chartSources.length = 0;
|
|
646
|
+
for (const link of this.links) {
|
|
647
|
+
const position = cellPosition(link.cell);
|
|
648
|
+
if (position.column > this.columns.length || position.row > this.totalRows)
|
|
649
|
+
throw new RangeError("Hyperlinks must address cells within the exported rows and columns.");
|
|
650
|
+
if (regions.covered(position.column, position.row))
|
|
651
|
+
throw new TypeError("Hyperlinks cannot address a covered merged cell.");
|
|
652
|
+
}
|
|
653
|
+
await this.start();
|
|
654
|
+
if (preservedRows)
|
|
655
|
+
for await (const row of inputRows(preservedRows, this.book.settings.signal)) {
|
|
656
|
+
this.book.budget.row(this.count + 1);
|
|
657
|
+
await this.writeRow(this.project(row, this.count), this.headerRows + this.count + 1, false);
|
|
658
|
+
this.count++;
|
|
659
|
+
}
|
|
660
|
+
if (this.layout.structuredFooter) {
|
|
661
|
+
for (let index = 0; index < this.layout.footerRows; index++)
|
|
662
|
+
await this.writeRow(this.layout.structuredFooter.values[index], this.headerRows + this.count + index + 1, false, true);
|
|
663
|
+
}
|
|
664
|
+
else if (this.options.footer)
|
|
665
|
+
await this.writeRow(this.layout.footer(this.count), this.headerRows + this.count + 1, false, true);
|
|
666
|
+
await this.buffer.write('</sheetData>');
|
|
667
|
+
if (this.options.autoFilter && this.headerRows && !this.tableDefinition)
|
|
668
|
+
await this.buffer.write('<autoFilter ref="A' + this.headerRows + ':' + columnName(this.columns.length) + (this.count + this.headerRows) + '"/>');
|
|
669
|
+
if (regions.references.length)
|
|
670
|
+
await this.buffer.write('<mergeCells count="' + regions.references.length + '">' + regions.references.map(ref => '<mergeCell ref="' + ref + '"/>').join("") + '</mergeCells>');
|
|
671
|
+
const detailRanges = dataRowRanges(this);
|
|
672
|
+
for (const xml of this.conditional.xml(this.headerRows, this.count, this.totalRows, detailRanges))
|
|
673
|
+
await this.buffer.write(xml);
|
|
674
|
+
for (const xml of dataValidationsXml(this.validations, this.headerRows, this.count, detailRanges))
|
|
675
|
+
await this.buffer.write(xml);
|
|
676
|
+
if (this.links.length || this.internalLinks.length)
|
|
677
|
+
await this.buffer.write(hyperlinksXml(this.links, this.book.settings.invalidCharacterPolicy, this.internalLinks));
|
|
678
|
+
await this.buffer.write(printXml(this.options.print, this.book.settings.invalidCharacterPolicy));
|
|
679
|
+
if (this.pictures.length || this.nativeCharts.length)
|
|
680
|
+
await this.buffer.write('<drawing r:id="drawing"/>');
|
|
681
|
+
if (this.tableDefinition)
|
|
682
|
+
await this.buffer.write('<tableParts count="1"><tablePart r:id="table"/></tableParts>');
|
|
683
|
+
await this.buffer.write('</worksheet>');
|
|
684
|
+
await this.buffer.close();
|
|
685
|
+
const info = await this.entry.close();
|
|
686
|
+
if (!this.book.settings.sink && !this.failed)
|
|
687
|
+
this.prepared = { ...info, chunks: this.output.takeChunks() };
|
|
688
|
+
this.completed = true;
|
|
689
|
+
}
|
|
690
|
+
catch (error) {
|
|
691
|
+
await this.discard(error);
|
|
692
|
+
throw error;
|
|
693
|
+
}
|
|
694
|
+
})();
|
|
695
|
+
}
|
|
696
|
+
/** @internal */
|
|
697
|
+
takePrepared() { const prepared = this.prepared; this.prepared = undefined; return prepared; }
|
|
698
|
+
/** @internal */
|
|
699
|
+
async discard(error) { this.failed = true; this.error = error; this.pending = []; this.pendingCharacters = 0; this.prepared = undefined; this.buffer = undefined; this.nativeCharts.length = 0; this.chartSources.length = 0; this.output.discard(); await this.entry?.discard(error); }
|
|
700
|
+
}
|