@quirekit/xlsx 0.0.0-stage → 0.3.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.md +21 -0
- package/THIRD_PARTY_NOTICES.md +150 -0
- package/dist/index.d.mts +122 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +19167 -0
- package/dist/index.mjs.map +1 -0
- package/licenses/saxes.LICENSE +64 -0
- package/licenses/xmlchars.LICENSE +18 -0
- package/package.json +52 -3
- package/src/bundle.ts +17 -0
- package/src/comments/legacy/read.ts +68 -0
- package/src/comments/legacy/write.ts +64 -0
- package/src/comments/package/ranges.ts +78 -0
- package/src/comments/package/read.ts +95 -0
- package/src/comments/package/roundtrip.test.ts +106 -0
- package/src/comments/package/write.ts +130 -0
- package/src/comments/thread/read.ts +69 -0
- package/src/comments/thread/write.ts +61 -0
- package/src/comments/types.ts +43 -0
- package/src/comments/xml.ts +50 -0
- package/src/create.ts +13 -0
- package/src/drawing/cache.ts +48 -0
- package/src/drawing/chart.test.ts +96 -0
- package/src/drawing/chart.ts +87 -0
- package/src/drawing/definition/plots.ts +76 -0
- package/src/drawing/definition/read.ts +65 -0
- package/src/drawing/definition/shape.ts +40 -0
- package/src/drawing/definition/types.ts +24 -0
- package/src/drawing/editable-picture.ts +80 -0
- package/src/drawing/graph.ts +148 -0
- package/src/drawing/identity.ts +22 -0
- package/src/drawing/import.ts +133 -0
- package/src/drawing/namespaces.ts +11 -0
- package/src/drawing/picture.test.ts +98 -0
- package/src/drawing/picture.ts +54 -0
- package/src/drawing/placement/anchor.ts +71 -0
- package/src/drawing/placement/axes.ts +113 -0
- package/src/drawing/placement/marker.ts +16 -0
- package/src/drawing/placement/transform.ts +132 -0
- package/src/drawing/placement/types.ts +20 -0
- package/src/drawing/presentation/cartesian/axes.ts +103 -0
- package/src/drawing/presentation/cartesian/plot.ts +109 -0
- package/src/drawing/presentation/cartesian/types.ts +9 -0
- package/src/drawing/presentation/layout.ts +29 -0
- package/src/drawing/presentation/paint.ts +56 -0
- package/src/drawing/presentation/properties.ts +31 -0
- package/src/drawing/read.ts +85 -0
- package/src/drawing/shapes/connector.ts +55 -0
- package/src/drawing/shapes/paint.ts +108 -0
- package/src/drawing/shapes/read.ts +89 -0
- package/src/drawing/shapes/text.ts +89 -0
- package/src/drawing/shapes/types.ts +13 -0
- package/src/drawing/sources.test.ts +27 -0
- package/src/drawing/sources.ts +43 -0
- package/src/drawing/types.ts +54 -0
- package/src/drawing/worksheet-connector.test.ts +135 -0
- package/src/drawing/worksheet-shape.test.ts +61 -0
- package/src/drawing/write/anchor.ts +17 -0
- package/src/drawing/write/axes.ts +23 -0
- package/src/drawing/write/chart.ts +56 -0
- package/src/drawing/write/drawing.ts +61 -0
- package/src/drawing/write/pictures.ts +68 -0
- package/src/drawing/write/series.ts +43 -0
- package/src/drawing/write/shapes.ts +113 -0
- package/src/drawing/write/text.ts +26 -0
- package/src/drawing/write/workbook.test.ts +199 -0
- package/src/drawing/write/workbook.ts +217 -0
- package/src/external-links/formulas.ts +21 -0
- package/src/external-links/read.test.ts +93 -0
- package/src/external-links/read.ts +104 -0
- package/src/external-links/types.ts +9 -0
- package/src/external-links/write.ts +120 -0
- package/src/image/excel.ts +122 -0
- package/src/image/native.ts +21 -0
- package/src/image/ownership.ts +77 -0
- package/src/image/parts.ts +45 -0
- package/src/image/raster.ts +25 -0
- package/src/image/read.test.ts +95 -0
- package/src/image/read.ts +66 -0
- package/src/image/relationships.ts +26 -0
- package/src/image/types.ts +48 -0
- package/src/image/wps.ts +43 -0
- package/src/image/write.test.ts +53 -0
- package/src/image/write.ts +118 -0
- package/src/index.ts +6 -0
- package/src/metadata/dynamic-array.ts +105 -0
- package/src/metadata/package.ts +75 -0
- package/src/metadata/types.ts +7 -0
- package/src/opc/archive.ts +26 -0
- package/src/opc/ownership.ts +40 -0
- package/src/opc/relationships.ts +98 -0
- package/src/opc/revision.ts +28 -0
- package/src/opc/shape.ts +23 -0
- package/src/opc/types.ts +13 -0
- package/src/opc/xml.test.ts +49 -0
- package/src/opc/xml.ts +128 -0
- package/src/optimization/blanks.ts +53 -0
- package/src/optimization/optimize.test.ts +117 -0
- package/src/optimization/optimize.ts +41 -0
- package/src/optimization/package.ts +65 -0
- package/src/optimization/rules.ts +46 -0
- package/src/optimization/styles.ts +76 -0
- package/src/optimization/types.ts +33 -0
- package/src/package/assembly.test.ts +117 -0
- package/src/package/calculation-metadata.test.ts +85 -0
- package/src/package/calculation-metadata.ts +88 -0
- package/src/package/checkboxes.ts +37 -0
- package/src/package/cleanup.test.ts +61 -0
- package/src/package/dashboard.test.ts +53 -0
- package/src/package/dashboard.ts +84 -0
- package/src/package/extensions.ts +16 -0
- package/src/package/merge-recipes.ts +84 -0
- package/src/package/names/conditional.ts +64 -0
- package/src/package/names/defined.test.ts +52 -0
- package/src/package/names/defined.ts +75 -0
- package/src/package/names/filter.ts +68 -0
- package/src/package/names/print-layout.ts +62 -0
- package/src/package/names/print.ts +62 -0
- package/src/package/new.ts +46 -0
- package/src/package/open/appearance.ts +46 -0
- package/src/package/open/directory.ts +61 -0
- package/src/package/open/restrictions.ts +27 -0
- package/src/package/open/snapshot.test.ts +48 -0
- package/src/package/open/snapshot.ts +34 -0
- package/src/package/open/source-write.ts +130 -0
- package/src/package/open/types.ts +73 -0
- package/src/package/open/worksheets.ts +149 -0
- package/src/package/positions.ts +40 -0
- package/src/package/read.ts +143 -0
- package/src/package/save/types.ts +57 -0
- package/src/package/save/worksheets.ts +126 -0
- package/src/package/selection-copy.test.ts +70 -0
- package/src/package/styles.ts +39 -0
- package/src/package/types.ts +52 -0
- package/src/package/workbook-views.ts +34 -0
- package/src/package/worksheets.test.ts +190 -0
- package/src/package/worksheets.ts +162 -0
- package/src/package/write.ts +175 -0
- package/src/pivot/cache/calculated.ts +34 -0
- package/src/pivot/cache/dates.ts +124 -0
- package/src/pivot/cache/groups.ts +11 -0
- package/src/pivot/cache/members.ts +99 -0
- package/src/pivot/cache/numbers.ts +63 -0
- package/src/pivot/cache/read.ts +131 -0
- package/src/pivot/cache/time.ts +20 -0
- package/src/pivot/cache/values.ts +43 -0
- package/src/pivot/cache/write.ts +117 -0
- package/src/pivot/definition/axes.ts +35 -0
- package/src/pivot/definition/dates.ts +87 -0
- package/src/pivot/definition/display.ts +86 -0
- package/src/pivot/definition/extension.ts +57 -0
- package/src/pivot/definition/field-settings/caption.ts +38 -0
- package/src/pivot/definition/field-settings/comparison.ts +47 -0
- package/src/pivot/definition/field-settings/field.ts +66 -0
- package/src/pivot/definition/field-settings/filters.ts +86 -0
- package/src/pivot/definition/field-settings/measure.ts +79 -0
- package/src/pivot/definition/field-settings/types.ts +19 -0
- package/src/pivot/definition/functions.ts +21 -0
- package/src/pivot/definition/items.ts +144 -0
- package/src/pivot/definition/members.ts +98 -0
- package/src/pivot/definition/numbers.ts +19 -0
- package/src/pivot/definition/presentation.ts +52 -0
- package/src/pivot/definition/read.ts +143 -0
- package/src/pivot/definition/report-layout.ts +39 -0
- package/src/pivot/definition/write.ts +157 -0
- package/src/pivot/package/calculated.test.ts +103 -0
- package/src/pivot/package/dates.test.ts +266 -0
- package/src/pivot/package/display.test.ts +133 -0
- package/src/pivot/package/field-settings.test.ts +59 -0
- package/src/pivot/package/layout.test.ts +148 -0
- package/src/pivot/package/measure-settings.test.ts +85 -0
- package/src/pivot/package/members.test.ts +167 -0
- package/src/pivot/package/numbers.test.ts +120 -0
- package/src/pivot/package/read.ts +93 -0
- package/src/pivot/package/roundtrip.test.ts +244 -0
- package/src/pivot/package/write.ts +103 -0
- package/src/pivot/types.ts +36 -0
- package/src/pivot/xml.ts +50 -0
- package/src/presentation.test.ts +186 -0
- package/src/roundtrip.test.ts +72 -0
- package/src/styles/checkbox/bags.ts +85 -0
- package/src/styles/checkbox/style.ts +45 -0
- package/src/styles/checkbox/types.ts +7 -0
- package/src/styles/color.ts +64 -0
- package/src/styles/differential-preview.ts +60 -0
- package/src/styles/differential.ts +119 -0
- package/src/styles/numbers.ts +9 -0
- package/src/styles/parts.ts +107 -0
- package/src/styles/read.ts +95 -0
- package/src/styles/reference.ts +14 -0
- package/src/styles/types.ts +9 -0
- package/src/styles/write.test.ts +88 -0
- package/src/styles/write.ts +162 -0
- package/src/table/conversion.test.ts +73 -0
- package/src/table/definition.ts +89 -0
- package/src/table/filter.ts +42 -0
- package/src/table/ownership.ts +112 -0
- package/src/table/read.test.ts +274 -0
- package/src/table/read.ts +117 -0
- package/src/table/remove.ts +55 -0
- package/src/table/sort.test.ts +79 -0
- package/src/table/styles/builtin.ts +40 -0
- package/src/table/styles/differential.ts +55 -0
- package/src/table/styles/preset-data.ts +17 -0
- package/src/table/styles/presets/dark.ts +104 -0
- package/src/table/styles/presets/light-grid.ts +68 -0
- package/src/table/styles/presets/light.ts +145 -0
- package/src/table/styles/presets/medium-edges.ts +131 -0
- package/src/table/styles/presets/medium.ts +131 -0
- package/src/table/styles/read.ts +70 -0
- package/src/table/styles/types.ts +27 -0
- package/src/table/totals.test.ts +77 -0
- package/src/table/totals.ts +79 -0
- package/src/table/types.ts +21 -0
- package/src/table/write.ts +189 -0
- package/src/types.ts +74 -0
- package/src/value/date.ts +21 -0
- package/src/worksheet/appearance.ts +55 -0
- package/src/worksheet/axis.test.ts +185 -0
- package/src/worksheet/axis.ts +131 -0
- package/src/worksheet/background/read.ts +41 -0
- package/src/worksheet/background/roundtrip.test.ts +77 -0
- package/src/worksheet/background/write.ts +96 -0
- package/src/worksheet/cell-shift.ts +44 -0
- package/src/worksheet/cells.ts +75 -0
- package/src/worksheet/checkbox/formats.ts +53 -0
- package/src/worksheet/checkbox/ranges.ts +112 -0
- package/src/worksheet/checkbox/write.test.ts +62 -0
- package/src/worksheet/conditional/color-scale.ts +61 -0
- package/src/worksheet/conditional/data-bar.ts +76 -0
- package/src/worksheet/conditional/extensions.ts +95 -0
- package/src/worksheet/conditional/icon-reference.ts +69 -0
- package/src/worksheet/conditional/icon-set.test.ts +60 -0
- package/src/worksheet/conditional/icon-set.ts +79 -0
- package/src/worksheet/conditional/preview.ts +44 -0
- package/src/worksheet/conditional/read.ts +185 -0
- package/src/worksheet/conditional/text.test.ts +71 -0
- package/src/worksheet/conditional/text.ts +122 -0
- package/src/worksheet/conditional/threshold.ts +47 -0
- package/src/worksheet/conditional/types.ts +20 -0
- package/src/worksheet/conditional/write-bar.ts +66 -0
- package/src/worksheet/conditional/write.test.ts +218 -0
- package/src/worksheet/conditional/write.ts +103 -0
- package/src/worksheet/copied-axis.ts +70 -0
- package/src/worksheet/data/rows.ts +59 -0
- package/src/worksheet/data/source.test.ts +68 -0
- package/src/worksheet/data/source.ts +31 -0
- package/src/worksheet/data/types.ts +9 -0
- package/src/worksheet/data.test.ts +314 -0
- package/src/worksheet/dimensions/columns.ts +87 -0
- package/src/worksheet/dimensions/write.test.ts +110 -0
- package/src/worksheet/dimensions/write.ts +36 -0
- package/src/worksheet/filters/advanced.test.ts +57 -0
- package/src/worksheet/filters/color.ts +105 -0
- package/src/worksheet/filters/icon.test.ts +83 -0
- package/src/worksheet/filters/icon.ts +39 -0
- package/src/worksheet/filters/read.ts +114 -0
- package/src/worksheet/filters/relative-dates.ts +47 -0
- package/src/worksheet/filters/restore-icons.ts +33 -0
- package/src/worksheet/filters/restore.ts +169 -0
- package/src/worksheet/filters/rules.ts +23 -0
- package/src/worksheet/filters/types.ts +23 -0
- package/src/worksheet/filters/visibility.ts +188 -0
- package/src/worksheet/filters/write.ts +121 -0
- package/src/worksheet/formula/cache.ts +29 -0
- package/src/worksheet/formula/external-results.test.ts +39 -0
- package/src/worksheet/formula/functions.ts +29 -0
- package/src/worksheet/formula/prefix.ts +27 -0
- package/src/worksheet/formula/shared/read.test.ts +121 -0
- package/src/worksheet/formula/shared/read.ts +94 -0
- package/src/worksheet/formula/shared/references.ts +100 -0
- package/src/worksheet/formula/shared/types.ts +21 -0
- package/src/worksheet/formula/spill-read.ts +54 -0
- package/src/worksheet/formula/spill-write.ts +32 -0
- package/src/worksheet/formula/spill.test.ts +101 -0
- package/src/worksheet/formula/write.test.ts +26 -0
- package/src/worksheet/hyperlinks/location.ts +59 -0
- package/src/worksheet/hyperlinks/model.ts +22 -0
- package/src/worksheet/hyperlinks/read.ts +70 -0
- package/src/worksheet/hyperlinks/types.ts +21 -0
- package/src/worksheet/hyperlinks/write.test.ts +203 -0
- package/src/worksheet/hyperlinks/write.ts +51 -0
- package/src/worksheet/merge.test.ts +58 -0
- package/src/worksheet/merge.ts +61 -0
- package/src/worksheet/outline/columns.ts +46 -0
- package/src/worksheet/outline/metadata.ts +68 -0
- package/src/worksheet/outline/native.ts +81 -0
- package/src/worksheet/outline/read.ts +50 -0
- package/src/worksheet/outline/roundtrip.test.ts +73 -0
- package/src/worksheet/outline/types.ts +37 -0
- package/src/worksheet/outline/write.ts +110 -0
- package/src/worksheet/positions.test.ts +46 -0
- package/src/worksheet/positions.ts +48 -0
- package/src/worksheet/print/breaks.ts +72 -0
- package/src/worksheet/print/elements.ts +16 -0
- package/src/worksheet/print/page-text.ts +139 -0
- package/src/worksheet/print/setup.test.ts +141 -0
- package/src/worksheet/print/setup.ts +116 -0
- package/src/worksheet/progress/read.ts +73 -0
- package/src/worksheet/progress/solid.ts +26 -0
- package/src/worksheet/progress/types.ts +11 -0
- package/src/worksheet/progress/write.test.ts +63 -0
- package/src/worksheet/progress/write.ts +46 -0
- package/src/worksheet/read.ts +197 -0
- package/src/worksheet/rich-text.test.ts +41 -0
- package/src/worksheet/rich-text.ts +76 -0
- package/src/worksheet/sort/read.ts +115 -0
- package/src/worksheet/sort/relocate.ts +62 -0
- package/src/worksheet/sort/roundtrip.test.ts +116 -0
- package/src/worksheet/sort/types.ts +11 -0
- package/src/worksheet/sort/write.ts +77 -0
- package/src/worksheet/template.ts +9 -0
- package/src/worksheet/text.ts +33 -0
- package/src/worksheet/validation/extended.ts +36 -0
- package/src/worksheet/validation/image.ts +7 -0
- package/src/worksheet/validation/list-options/portable.ts +90 -0
- package/src/worksheet/validation/list-options/types.ts +14 -0
- package/src/worksheet/validation/list-options/wps.ts +51 -0
- package/src/worksheet/validation/read.ts +162 -0
- package/src/worksheet/validation/text.ts +13 -0
- package/src/worksheet/validation/write.test.ts +259 -0
- package/src/worksheet/validation/write.ts +99 -0
- package/src/worksheet/view/coordinates.ts +39 -0
- package/src/worksheet/view/named/read.ts +116 -0
- package/src/worksheet/view/named/roundtrip.test.ts +136 -0
- package/src/worksheet/view/named/sort.ts +57 -0
- package/src/worksheet/view/named/types.ts +15 -0
- package/src/worksheet/view/named/write.ts +107 -0
- package/src/worksheet/view/named/xml.ts +33 -0
- package/src/worksheet/view/panes.ts +75 -0
- package/src/worksheet/view/read.ts +61 -0
- package/src/worksheet/view/relocate.ts +57 -0
- package/src/worksheet/view/write.test.ts +91 -0
- package/src/worksheet/view/write.ts +57 -0
- package/src/worksheet/write.ts +86 -0
- package/README.md +0 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
|
|
3
|
+
import type { WorkbookSnapshot } from "@quirekit/workbook";
|
|
4
|
+
import { createXlsx, openXlsx } from "./index";
|
|
5
|
+
|
|
6
|
+
const sample: WorkbookSnapshot = { version: 1, revision: 0, id: "reconciliation", name: "回款", dateSystem: "1904", sheets: [
|
|
7
|
+
{ id: "orders", name: "订单", cells: [
|
|
8
|
+
{ row: 0, column: 0, input: { kind: "value", value: "0007" } },
|
|
9
|
+
{ row: 0, column: 1, input: { kind: "value", value: true } },
|
|
10
|
+
{ row: 1, column: 0, input: { kind: "value", value: " 公示<&>_x0001_\r\n 复核\u0001 " } }
|
|
11
|
+
] },
|
|
12
|
+
{ id: "summary", name: "汇总", cells: [{ row: 0, column: 0, input: { kind: "formula", formula: "=IF('订单'!B1,120,0)" } }] }
|
|
13
|
+
] };
|
|
14
|
+
|
|
15
|
+
describe("XLSX exchange", () => {
|
|
16
|
+
it("writes native typed cells and a cross-sheet formula using the original date system", () => {
|
|
17
|
+
const bytes = createXlsx(sample, { result: () => ({ status: "value", value: 120 }) });
|
|
18
|
+
const parts = unzipSync(bytes);
|
|
19
|
+
expect(strFromU8(parts["xl/worksheets/sheet2.xml"]!)).toContain("<v>120</v>");
|
|
20
|
+
const reopened = openXlsx("回款", bytes);
|
|
21
|
+
expect(reopened.restrictions).toEqual([]);
|
|
22
|
+
expect(reopened.snapshot.dateSystem).toBe("1904");
|
|
23
|
+
expect(reopened.snapshot.sheets.map(sheet => sheet.cells)).toEqual(sample.sheets.map(sheet => sheet.cells));
|
|
24
|
+
expect(reopened.write(reopened.snapshot)).toEqual(bytes);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("updates existing parts without losing other XML, and refreshes dependencies' caches", () => {
|
|
28
|
+
const parts = unzipSync(createXlsx(sample));
|
|
29
|
+
parts["customXml/item1.xml"] = strToU8("<record><answer>32</answer></record>");
|
|
30
|
+
const worksheet = "xl/worksheets/sheet1.xml";
|
|
31
|
+
parts[worksheet] = strToU8(strFromU8(parts[worksheet]!).replace("<sheetData>", '<sheetViews><sheetView workbookViewId="0"/></sheetViews><sheetData>'));
|
|
32
|
+
const original = zipSync(parts);
|
|
33
|
+
const file = openXlsx("交易", original);
|
|
34
|
+
const snapshot = file.snapshot;
|
|
35
|
+
const [orders, ...other] = snapshot.sheets;
|
|
36
|
+
const updated = {
|
|
37
|
+
...snapshot, revision: 1, sheets: [
|
|
38
|
+
{ ...orders!, cells: orders!.cells.map(cell => cell.column === 1 ? { ...cell, input: { kind: "value" as const, value: false } } : cell) },
|
|
39
|
+
...other
|
|
40
|
+
]
|
|
41
|
+
};
|
|
42
|
+
const bytes = file.write(updated, { result: () => ({ status: "value", value: 0 }) });
|
|
43
|
+
const exported = unzipSync(bytes);
|
|
44
|
+
expect(exported["customXml/item1.xml"]).toEqual(parts["customXml/item1.xml"]);
|
|
45
|
+
expect(strFromU8(exported[worksheet]!)).toContain('workbookViewId="0"');
|
|
46
|
+
expect(strFromU8(exported["xl/worksheets/sheet2.xml"]!)).toContain("<v>0</v>");
|
|
47
|
+
expect(openXlsx("交易", bytes).snapshot.sheets[0]?.cells[1]?.input).toEqual({ kind: "value", value: false });
|
|
48
|
+
expect(file.write(snapshot)).toEqual(original);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns an unchanged protected original and reports its editing limitation", () => {
|
|
52
|
+
const parts = unzipSync(createXlsx(sample));
|
|
53
|
+
const path = "xl/worksheets/sheet1.xml";
|
|
54
|
+
parts[path] = strToU8(strFromU8(parts[path]!).replace("</sheetData>", "</sheetData><sheetProtection/>"));
|
|
55
|
+
const bytes = zipSync(parts);
|
|
56
|
+
const file = openXlsx("锁定", bytes);
|
|
57
|
+
expect(file.restrictions).toContain("工作表保护");
|
|
58
|
+
expect(file.write(file.snapshot)).toEqual(bytes);
|
|
59
|
+
const [orders, ...other] = file.snapshot.sheets;
|
|
60
|
+
expect(() => file.write({ ...file.snapshot, sheets: [{ ...orders!, cells: [] }, ...other] })).toThrow("工作表保护");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("distinguishes an empty workbook protection record from configured protection", () => {
|
|
64
|
+
const parts = unzipSync(createXlsx(sample));
|
|
65
|
+
const path = "xl/workbook.xml";
|
|
66
|
+
const document = strFromU8(parts[path]!);
|
|
67
|
+
parts[path] = strToU8(document.replace("<sheets>", "<workbookProtection/><sheets>"));
|
|
68
|
+
expect(openXlsx("普通工作簿", zipSync(parts)).restrictions).toEqual([]);
|
|
69
|
+
parts[path] = strToU8(document.replace("<sheets>", '<workbookProtection lockStructure="1"/><sheets>'));
|
|
70
|
+
expect(openXlsx("保护工作簿", zipSync(parts)).restrictions).toContain("工作簿保护");
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { hasKnownAttributes, hasKnownChildren } from "../../opc/shape";
|
|
3
|
+
import type { PackageRelationship } from "../../opc/types";
|
|
4
|
+
import { child, children, parsePart, rootOf } from "../../opc/xml";
|
|
5
|
+
import { featurePropertyNs, readXlsxComplement } from "./style";
|
|
6
|
+
import type { XlsxCheckboxStyles } from "./types";
|
|
7
|
+
|
|
8
|
+
/** Workbook to feature-property-bag relationship type. */
|
|
9
|
+
export const featurePropertyRelationship = "http://schemas.microsoft.com/office/2022/11/relationships/FeaturePropertyBag";
|
|
10
|
+
/** Feature property bag content type from the XLSX workbook format. */
|
|
11
|
+
export const featurePropertyContentType = "application/vnd.ms-excel.featurepropertybag+xml";
|
|
12
|
+
/** Minimal empty checkbox graph and its mapper entry at index zero. */
|
|
13
|
+
export const checkboxFeatureXml = `<FeaturePropertyBags xmlns="${featurePropertyNs}"><bag type="Checkbox"/><bag type="XFControls"><bagId k="CellControl">0</bagId></bag><bag type="XFComplement"><bagId k="XFControls">1</bagId></bag><bag type="XFComplements" extRef="XFComplementsMapperExtRef"><a k="MappedFeaturePropertyBags"><bagId>2</bagId></a></bag></FeaturePropertyBags>`;
|
|
14
|
+
|
|
15
|
+
/** @param node - Feature bag reference. @returns A nonnegative integer reference. */
|
|
16
|
+
function reference(node: Element | undefined): number | undefined {
|
|
17
|
+
const value = node?.textContent?.trim();
|
|
18
|
+
if (!value || !/^\d+$/u.test(value) || !Number.isSafeInteger(Number(value))) return undefined;
|
|
19
|
+
return Number(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @param parts - Retained package. @param relationships - Workbook relationships.
|
|
23
|
+
* @param stylesheet - Native styles used by cells. @param restrictions - Package write restrictions.
|
|
24
|
+
* @returns Known native checkbox styles, when the graph can be decoded. */
|
|
25
|
+
export function readXlsxCheckboxStyles(
|
|
26
|
+
parts: Readonly<Record<string, Uint8Array>>, relationships: ReadonlyMap<string, PackageRelationship>,
|
|
27
|
+
stylesheet: Element | undefined, restrictions: Set<string>
|
|
28
|
+
): XlsxCheckboxStyles | undefined {
|
|
29
|
+
const native = [...relationships.values()].filter(rel => rel.type === featurePropertyRelationship);
|
|
30
|
+
const xfs = stylesheet && child(stylesheet, "cellXfs");
|
|
31
|
+
const entries = xfs ? children(xfs, "xf") : [];
|
|
32
|
+
const used = entries.map(readXlsxComplement);
|
|
33
|
+
const bases = stylesheet && child(stylesheet, "cellStyleXfs");
|
|
34
|
+
if (bases && children(bases, "xf").some(xf => readXlsxComplement(xf) !== null)) restrictions.add("继承的单元格控件");
|
|
35
|
+
if (!native.length && used.every(index => index === null)) return;
|
|
36
|
+
try {
|
|
37
|
+
const path = native[0]?.path;
|
|
38
|
+
if (native.length !== 1 || !path) throw new Error("Unsupported feature property part");
|
|
39
|
+
const root = rootOf(parsePart(parts[path], path), featurePropertyNs, "FeaturePropertyBags");
|
|
40
|
+
const bags = children(root);
|
|
41
|
+
const seen = new Set<Element>();
|
|
42
|
+
const mappers = bags.filter(node => node.getAttribute("type") === "XFComplements");
|
|
43
|
+
const [mapper] = mappers;
|
|
44
|
+
if (!mapper || mappers.length !== 1 || mapper.getAttribute("extRef") !== "XFComplementsMapperExtRef"
|
|
45
|
+
|| !hasKnownAttributes(mapper, ["type", "extRef"]) || !hasKnownChildren(mapper, ["a"], featurePropertyNs)) throw new Error("Invalid mapper");
|
|
46
|
+
seen.add(mapper);
|
|
47
|
+
const mapping = child(mapper, "a");
|
|
48
|
+
if (!mapping || !hasKnownAttributes(mapping, ["k"]) || mapping.getAttribute("k") !== "MappedFeaturePropertyBags") throw new Error("Invalid mapper key");
|
|
49
|
+
const indices = children(mapping);
|
|
50
|
+
if (!indices.length) throw new Error("Missing checkbox complement");
|
|
51
|
+
const supported = new Set<number>();
|
|
52
|
+
for (const [index, node] of indices.entries()) {
|
|
53
|
+
if (node.localName !== "bagId" || node.namespaceURI !== featurePropertyNs || !hasKnownAttributes(node, []) || children(node).length) continue;
|
|
54
|
+
const complement = reference(node);
|
|
55
|
+
const controls = resolve(bags, complement, "XFComplement", "XFControls", seen);
|
|
56
|
+
const checkbox = resolve(bags, controls, "XFControls", "CellControl", seen);
|
|
57
|
+
const control = checkbox === undefined ? undefined : bags[checkbox];
|
|
58
|
+
if (!control || control.localName !== "bag" || control.namespaceURI !== featurePropertyNs || control.getAttribute("type") !== "Checkbox"
|
|
59
|
+
|| !hasKnownAttributes(control, ["type"]) || children(control).length) continue;
|
|
60
|
+
seen.add(control);
|
|
61
|
+
supported.add(index);
|
|
62
|
+
}
|
|
63
|
+
if (seen.size !== bags.length || supported.size !== indices.length || used.some(index => index !== null && (index === undefined || !supported.has(index)))) {
|
|
64
|
+
restrictions.add("暂未支持的单元格控件");
|
|
65
|
+
}
|
|
66
|
+
const complement = supported.values().next().value;
|
|
67
|
+
if (complement === undefined) throw new Error("Missing checkbox style");
|
|
68
|
+
return { complement, indexes: new Set(used.flatMap((index, i) => index != null && supported.has(index) ? [i] : [])) };
|
|
69
|
+
} catch {
|
|
70
|
+
restrictions.add("暂未支持的单元格控件");
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @param bags - Zero-based feature property bag table. @param index - Referenced bag. @param type - Required bag type.
|
|
76
|
+
* @param key - Expected relationship key. @param seen - Known graph nodes. @returns The dependent bag index. */
|
|
77
|
+
function resolve(bags: readonly Element[], index: number | undefined, type: string, key: string, seen: Set<Element>): number | undefined {
|
|
78
|
+
const bag = index === undefined ? undefined : bags[index];
|
|
79
|
+
if (!bag || bag.localName !== "bag" || bag.namespaceURI !== featurePropertyNs || bag.getAttribute("type") !== type
|
|
80
|
+
|| !hasKnownAttributes(bag, ["type"]) || !hasKnownChildren(bag, ["bagId"], featurePropertyNs)) return;
|
|
81
|
+
const childRef = child(bag, "bagId");
|
|
82
|
+
if (!childRef || childRef.getAttribute("k") !== key || !hasKnownAttributes(childRef, ["k"]) || children(childRef).length) return;
|
|
83
|
+
seen.add(bag);
|
|
84
|
+
return reference(childRef);
|
|
85
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { hasKnownAttributes, hasKnownChildren } from "../../opc/shape";
|
|
3
|
+
import { child, children, readIndex, spreadNs } from "../../opc/xml";
|
|
4
|
+
|
|
5
|
+
/** Native namespace for XF complement references and feature property bags. */
|
|
6
|
+
export const featurePropertyNs = "http://schemas.microsoft.com/office/spreadsheetml/2022/featurepropertybag";
|
|
7
|
+
const complementUri = "{C7286773-470A-42A8-94C5-96B5CB345126}";
|
|
8
|
+
|
|
9
|
+
/** @param xf - Native cell or inherited style record. @returns References owned by the XF complement extension. */
|
|
10
|
+
function complementRecords(xf: Element): Element[] {
|
|
11
|
+
const list = child(xf, "extLst");
|
|
12
|
+
return list ? children(list, "ext").filter(node => node.getAttribute("uri")?.toUpperCase() === complementUri) : [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @param xf - Native style record. @returns The mapped complement index, null if absent, or undefined if ambiguous. */
|
|
16
|
+
export function readXlsxComplement(xf: Element): number | null | undefined {
|
|
17
|
+
const records = complementRecords(xf);
|
|
18
|
+
if (!records.length) return null;
|
|
19
|
+
const [record] = records;
|
|
20
|
+
if (records.length !== 1 || record?.namespaceURI !== spreadNs || !hasKnownAttributes(record, ["uri"])
|
|
21
|
+
|| !hasKnownChildren(record, ["xfComplement"], featurePropertyNs)) return undefined;
|
|
22
|
+
const reference = child(record, "xfComplement");
|
|
23
|
+
return reference && hasKnownAttributes(reference, ["i"]) && !children(reference).length ? readIndex(reference, "i") : undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param xf - Independent clone of a native cell style. @param complement - Mapper index of the checkbox, or null to remove it. */
|
|
27
|
+
export function writeXlsxComplement(xf: Element, complement: number | null): void {
|
|
28
|
+
let list = child(xf, "extLst");
|
|
29
|
+
if (list) for (const record of complementRecords(xf)) list.removeChild(record);
|
|
30
|
+
if (complement === null) {
|
|
31
|
+
if (list && !children(list).length) xf.removeChild(list);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const document = xf.ownerDocument!;
|
|
35
|
+
if (!list) {
|
|
36
|
+
list = document.createElementNS(spreadNs, "extLst");
|
|
37
|
+
xf.appendChild(list);
|
|
38
|
+
}
|
|
39
|
+
const record = document.createElementNS(spreadNs, "ext");
|
|
40
|
+
record.setAttribute("uri", complementUri);
|
|
41
|
+
const reference = document.createElementNS(featurePropertyNs, "xfpb:xfComplement");
|
|
42
|
+
reference.setAttribute("i", String(complement));
|
|
43
|
+
record.appendChild(reference);
|
|
44
|
+
list.appendChild(record);
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Supported native checkbox indexes from the workbook's feature property bag. */
|
|
2
|
+
export interface XlsxCheckboxStyles {
|
|
3
|
+
/** Cell style indexes that carry a recognized checkbox feature. */
|
|
4
|
+
readonly indexes: ReadonlySet<number>;
|
|
5
|
+
/** Index in the XF complements mapper used for outgoing checkbox styles. */
|
|
6
|
+
readonly complement: number;
|
|
7
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { child, children } from "../opc/xml";
|
|
3
|
+
|
|
4
|
+
const themeSlots = ["lt1", "dk1", "lt2", "dk2", "accent1", "accent2", "accent3", "accent4", "accent5", "accent6", "hlink", "folHlink"];
|
|
5
|
+
const defaultTheme = ["FFFFFF", "000000", "EEECE1", "1F497D", "4F81BD", "C0504D", "9BBB59", "8064A2", "4BACC6", "F79646", "0000FF", "800080"];
|
|
6
|
+
const defaultIndexed = (
|
|
7
|
+
"000000 FFFFFF FF0000 00FF00 0000FF FFFF00 FF00FF 00FFFF 000000 FFFFFF FF0000 00FF00 0000FF FFFF00 FF00FF 00FFFF " +
|
|
8
|
+
"800000 008000 000080 808000 800080 008080 C0C0C0 808080 9999FF 993366 FFFFCC CCFFFF 660066 FF8080 0066CC CCCCFF " +
|
|
9
|
+
"000080 FF00FF FFFF00 00FFFF 800080 800000 008080 0000FF 00CCFF CCFFFF CCFFCC FFFF99 99CCFF FF99CC CC99FF FFCC99 " +
|
|
10
|
+
"3366FF 33CCCC 99CC00 FFCC00 FF9900 FF6600 666699 969696 003366 339966 003300 333300 993300 993366 333399 333333"
|
|
11
|
+
).split(" ");
|
|
12
|
+
|
|
13
|
+
/** @param raw - Six-digit RGB or eight-digit ARGB. @returns Opaque CSS RGB if well-formed. */
|
|
14
|
+
function rgb(raw: string | null | undefined): string | undefined {
|
|
15
|
+
return /^(?:[a-f\d]{2})?[a-f\d]{6}$/iu.test(raw ?? "") ? "#" + raw!.slice(-6).toUpperCase() : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @param theme - Workbook theme palette in SpreadsheetML order. @param slot - DrawingML theme name.
|
|
19
|
+
* @returns Its corresponding opaque color, if the slot is defined. */
|
|
20
|
+
export function drawingThemeColor(theme: readonly string[], slot: string): string | undefined {
|
|
21
|
+
const aliases = { bg1: "lt1", tx1: "dk1", bg2: "lt2", tx2: "dk2" } as const;
|
|
22
|
+
return theme[themeSlots.indexOf(Object.hasOwn(aliases, slot) ? aliases[slot as keyof typeof aliases] : slot)];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param root - Optional DrawingML theme root. @returns Theme colors in SpreadsheetML index order. */
|
|
26
|
+
export function readTheme(root: Element | undefined): readonly string[] {
|
|
27
|
+
const elements = root && child(root, "themeElements");
|
|
28
|
+
const scheme = elements && child(elements, "clrScheme");
|
|
29
|
+
return themeSlots.map((slot, index) => {
|
|
30
|
+
const color = scheme && child(scheme, slot);
|
|
31
|
+
return (color && rgb(child(color, "srgbClr")?.getAttribute("val") ?? child(color, "sysClr")?.getAttribute("lastClr")))
|
|
32
|
+
?? "#" + defaultTheme[index]!;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @param value - Opaque RGB. @param tint - SpreadsheetML lightness change. @returns Same hue and saturation at the adjusted lightness. */
|
|
37
|
+
export function tintXlsxColor(value: string, tint: number): string {
|
|
38
|
+
if (!Number.isFinite(tint) || tint === 0 || Math.abs(tint) > 1) return value;
|
|
39
|
+
const channels = [1, 3, 5].map(offset => parseInt(value.slice(offset, offset + 2), 16) / 255);
|
|
40
|
+
const lightness = (Math.min(...channels) + Math.max(...channels)) / 2;
|
|
41
|
+
const next = tint < 0 ? lightness * (1 + tint) : lightness + tint * (1 - lightness);
|
|
42
|
+
const radius = Math.min(lightness, 1 - lightness);
|
|
43
|
+
const scale = radius > 0 ? Math.min(next, 1 - next) / radius : 0;
|
|
44
|
+
return "#" + channels.map(channel => (
|
|
45
|
+
Math.round(Math.max(0, Math.min(1, next + (channel - lightness) * scale)) * 255).toString(16).padStart(2, "0")
|
|
46
|
+
)).join("").toUpperCase();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param node - Optional style color table. @param theme - Theme colors by SpreadsheetML index.
|
|
50
|
+
* @returns Resolves a style color against its workbook palette. */
|
|
51
|
+
export function styleColorReader(node: Element | undefined, theme: readonly string[]) {
|
|
52
|
+
const indexed = node && child(node, "indexedColors");
|
|
53
|
+
const palette = indexed ? children(indexed, "rgbColor").map(color => rgb(color.getAttribute("rgb"))) : defaultIndexed.map(rgb);
|
|
54
|
+
/** @param color - SpreadsheetML color with optional tint. @returns Opaque CSS RGB when the producer defines a color. */
|
|
55
|
+
return (color: Element | undefined): string | undefined => {
|
|
56
|
+
if (!color) return undefined;
|
|
57
|
+
const themeIndex = color.getAttribute("theme");
|
|
58
|
+
const paletteIndex = color.getAttribute("indexed");
|
|
59
|
+
const source = rgb(color.getAttribute("rgb"))
|
|
60
|
+
?? (themeIndex ? theme[Number(themeIndex)] : undefined)
|
|
61
|
+
?? (paletteIndex ? palette[Number(paletteIndex)] : undefined);
|
|
62
|
+
return source === undefined ? undefined : tintXlsxColor(source, Number(color.getAttribute("tint")));
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import type { WorkbookBorderSide, WorkbookCellAppearance, WorkbookConditionalFormulaPreview } from "@quirekit/workbook";
|
|
3
|
+
import { hasKnownAttributes, hasKnownChildren } from "../opc/shape";
|
|
4
|
+
import { child, children, spreadNs } from "../opc/xml";
|
|
5
|
+
import type { styleColorReader } from "./color";
|
|
6
|
+
import { readXlsxDifferential } from "./differential";
|
|
7
|
+
import { readWorkbookBorderStroke } from "./read";
|
|
8
|
+
import type { XlsxDifferentialStyle } from "./types";
|
|
9
|
+
|
|
10
|
+
const sides = ["top", "right", "bottom", "left"] as const;
|
|
11
|
+
type PreviewStyle = Pick<WorkbookConditionalFormulaPreview, "style" | "clearBorders">;
|
|
12
|
+
|
|
13
|
+
/** @param node - Differential record. @param color - Workbook palette.
|
|
14
|
+
* @returns Incremental preview style, including explicit side clears, when all properties are modeled. */
|
|
15
|
+
function readPreviewStyle(node: Element, color: ReturnType<typeof styleColorReader>): PreviewStyle | undefined {
|
|
16
|
+
if (node.localName !== "dxf" || node.namespaceURI !== spreadNs || !hasKnownAttributes(node, [])
|
|
17
|
+
|| !hasKnownChildren(node, ["font", "fill", "border"])) return;
|
|
18
|
+
const border = child(node, "border");
|
|
19
|
+
if (!border || !hasKnownAttributes(border, []) || !hasKnownChildren(border, [...sides, "vertical", "horizontal"])) return;
|
|
20
|
+
let base: XlsxDifferentialStyle | undefined;
|
|
21
|
+
if (children(node).length > 1) {
|
|
22
|
+
const plain = node.cloneNode(true) as Element;
|
|
23
|
+
plain.removeChild(child(plain, "border")!);
|
|
24
|
+
base = readXlsxDifferential(plain, color);
|
|
25
|
+
if (!base || base.noFill) return;
|
|
26
|
+
}
|
|
27
|
+
for (const name of ["vertical", "horizontal"]) {
|
|
28
|
+
const inside = child(border, name);
|
|
29
|
+
if (inside && (!hasKnownAttributes(inside, []) || !hasKnownChildren(inside, []))) return;
|
|
30
|
+
}
|
|
31
|
+
const edges: NonNullable<WorkbookCellAppearance["borders"]> = {};
|
|
32
|
+
const clearBorders: WorkbookBorderSide[] = [];
|
|
33
|
+
for (const side of sides) {
|
|
34
|
+
const edge = child(border, side);
|
|
35
|
+
if (!edge) continue;
|
|
36
|
+
if (!hasKnownAttributes(edge, ["style"]) || !hasKnownChildren(edge, ["color"])) return;
|
|
37
|
+
const ink = child(edge, "color");
|
|
38
|
+
if ((!edge.hasAttribute("style") || edge.getAttribute("style") === "none") && !ink) {
|
|
39
|
+
clearBorders.push(side);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const stroke = readWorkbookBorderStroke(edge);
|
|
43
|
+
const resolved = ink ? color(ink) : "#000000";
|
|
44
|
+
if (!stroke || !resolved || ink && (!hasKnownAttributes(ink, ["rgb", "theme", "indexed", "tint"]) || !hasKnownChildren(ink, []))) return;
|
|
45
|
+
edges[side] = { stroke, color: resolved };
|
|
46
|
+
}
|
|
47
|
+
const style = { ...base, ...(Object.keys(edges).length && { borders: edges }) };
|
|
48
|
+
return Object.keys(style).length || clearBorders.length ? { style, ...(clearBorders.length && { clearBorders }) } : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param styleSheet - Original styles part. @param color - Workbook palette. @param ordinary - Already decoded styles.
|
|
52
|
+
* @returns Preview styles by native index. */
|
|
53
|
+
export function readXlsxDifferentialPreviews(styleSheet: Element | undefined, color: ReturnType<typeof styleColorReader>,
|
|
54
|
+
ordinary: readonly (XlsxDifferentialStyle | undefined)[]): readonly (PreviewStyle | undefined)[] {
|
|
55
|
+
const dxfs = styleSheet && child(styleSheet, "dxfs");
|
|
56
|
+
return dxfs ? children(dxfs).map((node, index) => {
|
|
57
|
+
const style = ordinary[index];
|
|
58
|
+
return style ? style.progressPercent ? undefined : { style } : readPreviewStyle(node, color);
|
|
59
|
+
}) : [];
|
|
60
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Document, Element } from "@xmldom/xmldom";
|
|
2
|
+
import { workbookProgressNumberFormat, type WorkbookFilterColorSource } from "@quirekit/workbook";
|
|
3
|
+
import { hasKnownAttributes, hasKnownChildren } from "../opc/shape";
|
|
4
|
+
import { child, children, readIndex, spreadNs } from "../opc/xml";
|
|
5
|
+
import type { styleColorReader } from "./color";
|
|
6
|
+
import type { XlsxDifferentialStyle } from "./types";
|
|
7
|
+
|
|
8
|
+
const fontFields = [["b", "bold"], ["i", "italic"], ["u", "underline"], ["strike", "strike"]] as const;
|
|
9
|
+
|
|
10
|
+
/** @param style - A referenced differential style. @param source - The criterion's color channel.
|
|
11
|
+
* @param fontFromFill - Whether a producer uses a lone fill to store the font color.
|
|
12
|
+
* @returns An explicit color, null for no fill, or undefined for an unsupported style. */
|
|
13
|
+
export function xlsxDifferentialColor(style: XlsxDifferentialStyle | undefined, source: WorkbookFilterColorSource,
|
|
14
|
+
fontFromFill = false): string | null | undefined {
|
|
15
|
+
if (source === "fill") return style?.noFill ? null : style?.background;
|
|
16
|
+
return style?.color ?? (fontFromFill && style && Object.keys(style).length === 1 ? style.background : undefined);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @param source - Requested color channel. @param color - Exact comparison color or no fill.
|
|
20
|
+
* @returns A differential containing only this criterion's color. */
|
|
21
|
+
export function xlsxColorDifferential(source: WorkbookFilterColorSource, color: string | null): XlsxDifferentialStyle {
|
|
22
|
+
return color === null ? { noFill: true } : source === "fill" ? { background: color } : { color };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param node - Native on/off element. @param underline - Whether this is the underline enumeration.
|
|
26
|
+
* @returns The explicit style override, if valid. */
|
|
27
|
+
function fontFlag(node: Element, underline: boolean): boolean | undefined {
|
|
28
|
+
if (!hasKnownChildren(node, []) || !hasKnownAttributes(node, ["val"])) return undefined;
|
|
29
|
+
const value = node.getAttribute("val");
|
|
30
|
+
if (underline) return !node.hasAttribute("val") || value === "single" ? true : value === "none" ? false : undefined;
|
|
31
|
+
return !node.hasAttribute("val") || value === "1" || value === "true" ? true : value === "0" || value === "false" ? false : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @param node - Differential format. @param color - Resolver for its associated theme and palette.
|
|
35
|
+
* @param namespace - Namespace of the dxf wrapper, whose children remain standard style records.
|
|
36
|
+
* @returns The supported incremental style or undefined for an unmodeled record. */
|
|
37
|
+
export function readXlsxDifferential(node: Element, color: ReturnType<typeof styleColorReader>, namespace = spreadNs): XlsxDifferentialStyle | undefined {
|
|
38
|
+
if (node.namespaceURI !== namespace || !hasKnownAttributes(node, []) || !hasKnownChildren(node, ["font", "fill", "numFmt"], spreadNs)) return undefined;
|
|
39
|
+
const style: Record<string, string | boolean> = {};
|
|
40
|
+
const font = child(node, "font");
|
|
41
|
+
if (font) {
|
|
42
|
+
if (!hasKnownAttributes(font, []) || !hasKnownChildren(font, [...fontFields.map(([tag]) => tag), "color"])) return undefined;
|
|
43
|
+
for (const [tag, key] of fontFields) {
|
|
44
|
+
const entry = child(font, tag);
|
|
45
|
+
if (!entry) continue;
|
|
46
|
+
const value = fontFlag(entry, tag === "u");
|
|
47
|
+
if (value === undefined) return undefined;
|
|
48
|
+
style[key] = value;
|
|
49
|
+
}
|
|
50
|
+
const foreground = child(font, "color");
|
|
51
|
+
if (foreground) {
|
|
52
|
+
const value = color(foreground);
|
|
53
|
+
if (!value || !hasKnownAttributes(foreground, ["rgb", "theme", "indexed", "tint"])) return undefined;
|
|
54
|
+
style.color = value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const fill = child(node, "fill");
|
|
58
|
+
if (fill) {
|
|
59
|
+
const pattern = child(fill, "patternFill");
|
|
60
|
+
if (!pattern || !hasKnownAttributes(fill, []) || !hasKnownChildren(fill, ["patternFill"])
|
|
61
|
+
|| !hasKnownChildren(pattern, ["fgColor", "bgColor"]) || !hasKnownAttributes(pattern, ["patternType"])) return undefined;
|
|
62
|
+
const type = pattern.getAttribute("patternType");
|
|
63
|
+
if (type === "none" && !children(pattern).length) style.noFill = true;
|
|
64
|
+
else {
|
|
65
|
+
if (type && type !== "solid") return undefined;
|
|
66
|
+
const foreground = child(pattern, "fgColor"), background = child(pattern, "bgColor");
|
|
67
|
+
const selected = type === "solid" ? foreground ?? background : background ?? foreground;
|
|
68
|
+
const value = selected && color(selected);
|
|
69
|
+
if (!value || !hasKnownAttributes(selected!, ["rgb", "theme", "indexed", "tint"])) return undefined;
|
|
70
|
+
style.background = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const numbers = children(node, "numFmt");
|
|
74
|
+
if (numbers.length) {
|
|
75
|
+
const format = numbers[0]!;
|
|
76
|
+
if (numbers.length !== 1 || readIndex(format, "numFmtId") !== 9 || format.getAttribute("formatCode") !== workbookProgressNumberFormat
|
|
77
|
+
|| !hasKnownAttributes(format, ["numFmtId", "formatCode"]) || !hasKnownChildren(format, [])) return undefined;
|
|
78
|
+
return { ...style, progressPercent: true };
|
|
79
|
+
}
|
|
80
|
+
return Object.keys(style).length ? style : undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** @param document - Owner of the style nodes. @param style - Incremental style.
|
|
84
|
+
* @param node - Optional wrapper in an extended worksheet rule. @returns Native dxf record. */
|
|
85
|
+
export function writeXlsxDifferential(document: Document, style: XlsxDifferentialStyle, node = document.createElementNS(spreadNs, "dxf")): Element {
|
|
86
|
+
const font = document.createElementNS(spreadNs, "font");
|
|
87
|
+
for (const [tag, key] of fontFields) {
|
|
88
|
+
const value = style[key];
|
|
89
|
+
if (value === undefined) continue;
|
|
90
|
+
const property = document.createElementNS(spreadNs, tag);
|
|
91
|
+
property.setAttribute("val", tag === "u" ? value ? "single" : "none" : value ? "1" : "0");
|
|
92
|
+
font.appendChild(property);
|
|
93
|
+
}
|
|
94
|
+
if (style.color) {
|
|
95
|
+
const color = document.createElementNS(spreadNs, "color");
|
|
96
|
+
color.setAttribute("rgb", "FF" + style.color.slice(1).toUpperCase());
|
|
97
|
+
font.appendChild(color);
|
|
98
|
+
}
|
|
99
|
+
if (font.hasChildNodes()) node.appendChild(font);
|
|
100
|
+
if (style.progressPercent) {
|
|
101
|
+
const format = document.createElementNS(spreadNs, "numFmt");
|
|
102
|
+
format.setAttribute("numFmtId", "9");
|
|
103
|
+
format.setAttribute("formatCode", workbookProgressNumberFormat);
|
|
104
|
+
node.appendChild(format);
|
|
105
|
+
}
|
|
106
|
+
if (style.background || style.noFill) {
|
|
107
|
+
// Excel encodes a differential background in bgColor, independently of normal fills.
|
|
108
|
+
const fill = document.createElementNS(spreadNs, "fill");
|
|
109
|
+
const pattern = document.createElementNS(spreadNs, "patternFill");
|
|
110
|
+
if (style.background) {
|
|
111
|
+
const background = document.createElementNS(spreadNs, "bgColor");
|
|
112
|
+
background.setAttribute("rgb", "FF" + style.background.slice(1).toUpperCase());
|
|
113
|
+
pattern.appendChild(background);
|
|
114
|
+
} else pattern.setAttribute("patternType", "none");
|
|
115
|
+
fill.appendChild(pattern);
|
|
116
|
+
node.appendChild(fill);
|
|
117
|
+
}
|
|
118
|
+
return node;
|
|
119
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Built-in SpreadsheetML format codes that are independent of the UI language. */
|
|
2
|
+
export const builtinNumberFormats: ReadonlyMap<number, string> = new Map([
|
|
3
|
+
[0, "General"], [1, "0"], [2, "0.00"], [3, "#,##0"], [4, "#,##0.00"],
|
|
4
|
+
[9, "0%"], [10, "0.00%"], [11, "0.00E+00"], [12, "# ?/?"], [13, "# ??/??"],
|
|
5
|
+
[14, "mm-dd-yy"], [15, "d-mmm-yy"], [16, "d-mmm"], [17, "mmm-yy"],
|
|
6
|
+
[18, "h:mm AM/PM"], [19, "h:mm:ss AM/PM"], [20, "h:mm"], [21, "h:mm:ss"], [22, "m/d/yy h:mm"],
|
|
7
|
+
[37, "#,##0 ;(#,##0)"], [38, "#,##0 ;[Red](#,##0)"], [39, "#,##0.00;(#,##0.00)"], [40, "#,##0.00;[Red](#,##0.00)"],
|
|
8
|
+
[45, "mm:ss"], [46, "[h]:mm:ss"], [47, "mmss.0"], [48, "##0.0E+0"], [49, "@"]
|
|
9
|
+
]);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Document, Element } from "@xmldom/xmldom";
|
|
2
|
+
import { workbookBorderSides, type WorkbookCellFormat } from "@quirekit/workbook";
|
|
3
|
+
import { child, children, spreadNs } from "../opc/xml";
|
|
4
|
+
|
|
5
|
+
/** @param rgb - Canonical opaque color. @returns SpreadsheetML ARGB. */
|
|
6
|
+
function argb(rgb: string): string { return "FF" + rgb.slice(1).toUpperCase(); }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param font - A clone of the active font.
|
|
10
|
+
* @param name - Property element name.
|
|
11
|
+
* @param value - Explicit value, or null to remove the property.
|
|
12
|
+
*/
|
|
13
|
+
function writeFontValue(font: Element, name: string, value: string | null): void {
|
|
14
|
+
const existing = child(font, name);
|
|
15
|
+
if (existing) font.removeChild(existing);
|
|
16
|
+
if (value === null) return;
|
|
17
|
+
const property = font.ownerDocument!.createElementNS(spreadNs, name);
|
|
18
|
+
property.setAttribute("val", value);
|
|
19
|
+
font.appendChild(property);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @param font - Independent font record. @param patch - Supported fields to modify. */
|
|
23
|
+
export function writeXlsxFont(font: Element, patch: WorkbookCellFormat): void {
|
|
24
|
+
if (patch.fontFamily !== undefined) {
|
|
25
|
+
writeFontValue(font, "name", patch.fontFamily);
|
|
26
|
+
writeFontValue(font, "scheme", null);
|
|
27
|
+
}
|
|
28
|
+
if (patch.fontSize !== undefined) writeFontValue(font, "sz", String(patch.fontSize));
|
|
29
|
+
for (const [name, flag] of [["b", patch.bold], ["i", patch.italic], ["u", patch.underline], ["strike", patch.strike]] as const) {
|
|
30
|
+
if (flag !== undefined) writeFontValue(font, name, flag ? name === "u" ? "single" : "1" : null);
|
|
31
|
+
}
|
|
32
|
+
if (patch.color !== undefined) {
|
|
33
|
+
const previous = child(font, "color");
|
|
34
|
+
if (previous) font.removeChild(previous);
|
|
35
|
+
const color = font.ownerDocument!.createElementNS(spreadNs, "color");
|
|
36
|
+
if (patch.color === null) color.setAttribute("auto", "1");
|
|
37
|
+
else color.setAttribute("rgb", argb(patch.color));
|
|
38
|
+
font.appendChild(color);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param document - Styles part.
|
|
44
|
+
* @param rgb - Fill color or explicit absence.
|
|
45
|
+
* @returns New fill record.
|
|
46
|
+
*/
|
|
47
|
+
export function xlsxFill(document: Document, rgb: string | null): Element {
|
|
48
|
+
const fill = document.createElementNS(spreadNs, "fill");
|
|
49
|
+
const pattern = document.createElementNS(spreadNs, "patternFill");
|
|
50
|
+
pattern.setAttribute("patternType", rgb === null ? "none" : "solid");
|
|
51
|
+
if (rgb !== null) {
|
|
52
|
+
const foreground = document.createElementNS(spreadNs, "fgColor");
|
|
53
|
+
foreground.setAttribute("rgb", argb(rgb));
|
|
54
|
+
pattern.appendChild(foreground);
|
|
55
|
+
}
|
|
56
|
+
fill.appendChild(pattern);
|
|
57
|
+
return fill;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param border - Clone of the active border record.
|
|
62
|
+
* @param edges - Only the edges to replace, or null to clear the four cell edges.
|
|
63
|
+
*/
|
|
64
|
+
export function writeXlsxBorder(border: Element, edges: NonNullable<WorkbookCellFormat["borders"]> | null): void {
|
|
65
|
+
const document = border.ownerDocument!;
|
|
66
|
+
const orderedSides = ["start", "end", "left", "right", "top", "bottom", "diagonal", "vertical", "horizontal"];
|
|
67
|
+
for (const side of workbookBorderSides) {
|
|
68
|
+
const stroke = edges === null ? null : edges[side];
|
|
69
|
+
if (stroke === undefined) continue;
|
|
70
|
+
const edge = document.createElementNS(spreadNs, side);
|
|
71
|
+
if (stroke) {
|
|
72
|
+
edge.setAttribute("style", stroke.stroke);
|
|
73
|
+
const color = document.createElementNS(spreadNs, "color");
|
|
74
|
+
color.setAttribute("rgb", argb(stroke.color));
|
|
75
|
+
edge.appendChild(color);
|
|
76
|
+
}
|
|
77
|
+
const previous = child(border, side);
|
|
78
|
+
if (previous) border.replaceChild(edge, previous);
|
|
79
|
+
else {
|
|
80
|
+
const next = children(border).find(item => orderedSides.indexOf(item.localName ?? "") > orderedSides.indexOf(side));
|
|
81
|
+
border.insertBefore(edge, next ?? null);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param xf - Independent cell format record.
|
|
88
|
+
* @param inherited - The named style's alignment.
|
|
89
|
+
* @param patch - Alignment and text fitting changes.
|
|
90
|
+
*/
|
|
91
|
+
export function writeXlsxAlignment(xf: Element, inherited: Element | undefined, patch: WorkbookCellFormat): void {
|
|
92
|
+
const document = xf.ownerDocument!;
|
|
93
|
+
const previous = child(xf, "alignment");
|
|
94
|
+
const own = ["0", "false"].includes(xf.getAttribute("applyAlignment") ?? "") ? undefined : previous;
|
|
95
|
+
const alignment = (own ?? inherited)?.cloneNode(true) as Element | undefined ?? document.createElementNS(spreadNs, "alignment");
|
|
96
|
+
if (patch.horizontal !== undefined) alignment.setAttribute("horizontal", patch.horizontal ?? "general");
|
|
97
|
+
if (patch.vertical !== undefined) alignment.setAttribute("vertical", patch.vertical);
|
|
98
|
+
if (patch.indent !== undefined) {
|
|
99
|
+
alignment.setAttribute("indent", String(patch.indent));
|
|
100
|
+
if (patch.indent > 0 && ["", "general"].includes(alignment.getAttribute("horizontal") ?? "")) alignment.setAttribute("horizontal", "left");
|
|
101
|
+
}
|
|
102
|
+
if (patch.wrap !== undefined) alignment.setAttribute("wrapText", patch.wrap ? "1" : "0");
|
|
103
|
+
if (patch.shrinkToFit !== undefined) alignment.setAttribute("shrinkToFit", patch.shrinkToFit ? "1" : "0");
|
|
104
|
+
if (previous) xf.replaceChild(alignment, previous);
|
|
105
|
+
else xf.insertBefore(alignment, child(xf, "protection") ?? child(xf, "extLst") ?? null);
|
|
106
|
+
xf.setAttribute("applyAlignment", "1");
|
|
107
|
+
}
|