@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,259 @@
|
|
|
1
|
+
import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
|
|
2
|
+
import { openWorkbook, parseWorkbookRange, type WorkbookSnapshot } from "@quirekit/workbook";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { createXlsx, openXlsx } from "../../index";
|
|
5
|
+
|
|
6
|
+
const seed: WorkbookSnapshot = {
|
|
7
|
+
id: "file", name: "Validation", version: 1, revision: 0, dateSystem: "1904",
|
|
8
|
+
sheets: [{ id: "one", name: "Rules", cells: [
|
|
9
|
+
{ row: 0, column: 2, input: { kind: "value", value: "001" } },
|
|
10
|
+
{ row: 1, column: 2, input: { kind: "value", value: "Paid" } }
|
|
11
|
+
] }, { id: "two", name: "Other", cells: [] }]
|
|
12
|
+
};
|
|
13
|
+
const sheetPart = "xl/worksheets/sheet1.xml";
|
|
14
|
+
const otherPart = "xl/worksheets/sheet2.xml";
|
|
15
|
+
|
|
16
|
+
it("writes native validation with editable dropdowns, numeric conditions and cell prompts", async () => {
|
|
17
|
+
const original = createXlsx(seed);
|
|
18
|
+
const source = openXlsx("Validation", original);
|
|
19
|
+
const book = await openWorkbook(source.snapshot);
|
|
20
|
+
try {
|
|
21
|
+
const sheetId = book.worksheets()[0]!.id;
|
|
22
|
+
const range = (a1: string) => parseWorkbookRange(sheetId, a1);
|
|
23
|
+
const edit = book.prepare({ operationId: "set-rules", baseRevision: 0, commands: [
|
|
24
|
+
{ name: "setDataValidation", target: range("A2:A11"), params: { type: "list", source: { kind: "range", a1: "C1:C2" },
|
|
25
|
+
allowBlank: true, prompt: { title: "Status", message: "Pick a status." }, alert: { level: "stop" } } },
|
|
26
|
+
{ name: "setDataValidation", target: range("B2:B11"), params: { type: "date", operator: "between", first: 10, second: 40,
|
|
27
|
+
alert: { level: "information", title: "Date", enabled: false } } },
|
|
28
|
+
{ name: "setDataValidation", target: range("E2"), params: { type: "list", source: { kind: "options", values: ["A & B", "001"] },
|
|
29
|
+
dropdown: false } }
|
|
30
|
+
] });
|
|
31
|
+
await book.commit(edit.id);
|
|
32
|
+
const written = source.write(book.snapshot());
|
|
33
|
+
const xml = unzipSync(written);
|
|
34
|
+
expect(strFromU8(xml[sheetPart]!)).toContain("<formula1>$C$1:$C$2</formula1>");
|
|
35
|
+
expect(strFromU8(xml[sheetPart]!)).toContain('showDropDown="1"');
|
|
36
|
+
expect(strFromU8(xml[sheetPart]!)).toContain('<formula1>"A & B,001"</formula1>');
|
|
37
|
+
expect(xml[otherPart]).toEqual(unzipSync(original)[otherPart]);
|
|
38
|
+
const reopened = openXlsx("Validation", written);
|
|
39
|
+
expect(reopened.restrictions).toEqual([]);
|
|
40
|
+
expect(reopened.axisRestrictions.has(sheetId)).toBe(false);
|
|
41
|
+
expect(reopened.snapshot.dateSystem).toBe("1904");
|
|
42
|
+
expect(reopened.snapshot.sheets[0]!.validations).toEqual(book.dataValidations(sheetId));
|
|
43
|
+
expect(reopened.write(reopened.snapshot)).toEqual(written);
|
|
44
|
+
await book.undo("undo-rules");
|
|
45
|
+
expect(source.write(book.snapshot())).toEqual(original);
|
|
46
|
+
} finally { await book.close(); }
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("keeps a mixed worksheet readable and preserves the file when a validation formula is unsupported", () => {
|
|
50
|
+
const archive = unzipSync(createXlsx(seed));
|
|
51
|
+
const xml = strFromU8(archive[sheetPart]!);
|
|
52
|
+
archive[sheetPart] = strToU8(xml.replace("</worksheet>", '<dataValidations count="2"><dataValidation type="list" sqref="A1 A3:A4"><formula1>"One,Two"</formula1></dataValidation><dataValidation type="custom" sqref="B1"><formula1>ISNUMBER([1]Other!A1)</formula1></dataValidation></dataValidations></worksheet>'));
|
|
53
|
+
const original = zipSync(archive);
|
|
54
|
+
const file = openXlsx("Mixed", original);
|
|
55
|
+
expect(file.restrictions).toContain("数据有效性包含暂未支持的规则");
|
|
56
|
+
expect(file.snapshot.sheets[0]!.validations).toHaveLength(2);
|
|
57
|
+
expect(file.write(file.snapshot)).toEqual(original);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("rebases imported custom validation in disjoint areas and saves the standard rule", async () => {
|
|
61
|
+
const archive = unzipSync(createXlsx(seed)), source = strFromU8(archive[sheetPart]!);
|
|
62
|
+
const native = '<dataValidations count="1"><dataValidation type="custom" showErrorMessage="1" sqref="E3:E4 C2">'
|
|
63
|
+
+ '<formula1>AND(E3>$C3,COUNTIF($E$3:$E$10,E3)=1)</formula1></dataValidation></dataValidations>';
|
|
64
|
+
const office2010 = '<extLst><ext uri="{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}"><x14:dataValidations xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main" count="1">'
|
|
65
|
+
+ '<x14:dataValidation type="custom" allowBlank="1" showErrorMessage="1"><x14:formula1><xm:f>F3>=E3</xm:f></x14:formula1><xm:sqref>F3:F9</xm:sqref></x14:dataValidation></x14:dataValidations></ext></extLst>';
|
|
66
|
+
archive[sheetPart] = strToU8(source.replace("</worksheet>", native + office2010 + "</worksheet>"));
|
|
67
|
+
const bytes = zipSync(archive), file = openXlsx("Custom", bytes);
|
|
68
|
+
expect(file.restrictions).toEqual([]);
|
|
69
|
+
const book = await openWorkbook(file.snapshot);
|
|
70
|
+
const sheetId = book.worksheets()[0]!.id;
|
|
71
|
+
try {
|
|
72
|
+
expect(book.validationAt({ sheetId, row: 1, column: 2 })?.rule.definition).toMatchObject({
|
|
73
|
+
type: "custom", formula: "=AND(C2>$C2,COUNTIF($E$3:$E$10,C2)=1)"
|
|
74
|
+
});
|
|
75
|
+
expect(book.validationAt({ sheetId, row: 2, column: 5 })?.rule.definition).toMatchObject({
|
|
76
|
+
type: "custom", formula: "=F3>=E3", allowBlank: true
|
|
77
|
+
});
|
|
78
|
+
await book.commit(book.prepare({ operationId: "clear-custom", baseRevision: book.revision, commands: [
|
|
79
|
+
{ name: "clearDataValidation", target: parseWorkbookRange(sheetId, "E3"), params: {} }
|
|
80
|
+
] }).id);
|
|
81
|
+
const saved = file.write(book.snapshot()), xml = strFromU8(unzipSync(saved)[sheetPart]!);
|
|
82
|
+
expect(xml).not.toContain("x14:dataValidation");
|
|
83
|
+
expect(xml).toContain('<dataValidation type="custom" sqref="E4"');
|
|
84
|
+
expect(xml).toContain("<formula1>AND(E4>$C4,COUNTIF($E$3:$E$10,E4)=1)</formula1>");
|
|
85
|
+
const reopened = openXlsx("Custom", saved);
|
|
86
|
+
expect(reopened.restrictions).toEqual([]);
|
|
87
|
+
expect(reopened.snapshot.sheets[0]?.validations).toEqual(book.dataValidations(sheetId));
|
|
88
|
+
} finally { await book.close(); }
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("round-trips a native relative list and rebases disjoint targets when writing split rules", async () => {
|
|
92
|
+
const archive = unzipSync(createXlsx(seed)), sourceXml = strFromU8(archive[sheetPart]!);
|
|
93
|
+
const native = '<dataValidations count="1"><dataValidation type="list" showErrorMessage="1" sqref="E1:E2 G2">'
|
|
94
|
+
+ '<formula1>$C1</formula1></dataValidation></dataValidations>';
|
|
95
|
+
archive[sheetPart] = strToU8(sourceXml.replace("</worksheet>", native + "</worksheet>"));
|
|
96
|
+
const original = zipSync(archive), source = openXlsx("Relative", original);
|
|
97
|
+
expect(source.restrictions).toEqual([]);
|
|
98
|
+
expect(source.write(source.snapshot)).toEqual(original);
|
|
99
|
+
const book = await openWorkbook(source.snapshot);
|
|
100
|
+
const sheetId = book.worksheets()[0]!.id;
|
|
101
|
+
try {
|
|
102
|
+
expect(book.validationAt({ sheetId, row: 0, column: 4 })?.options).toEqual(["001"]);
|
|
103
|
+
expect(book.validationAt({ sheetId, row: 1, column: 4 })?.options).toEqual(["Paid"]);
|
|
104
|
+
expect(book.validationAt({ sheetId, row: 1, column: 6 })?.rule.definition).toMatchObject({
|
|
105
|
+
type: "list", source: { kind: "range", relative: true, a1: "$C2" }
|
|
106
|
+
});
|
|
107
|
+
await book.commit(book.prepare({ operationId: "clear-first-relative-cell", baseRevision: book.revision, commands: [
|
|
108
|
+
{ name: "clearDataValidation", target: parseWorkbookRange(sheetId, "E1"), params: {} }
|
|
109
|
+
] }).id);
|
|
110
|
+
const written = source.write(book.snapshot()), xml = strFromU8(unzipSync(written)[sheetPart]!);
|
|
111
|
+
expect(xml).toContain('sqref="E2"');
|
|
112
|
+
expect(xml.match(/<formula1>\$C2<\/formula1>/gu)).toHaveLength(2);
|
|
113
|
+
expect(openXlsx("Relative", written).snapshot.sheets[0]?.validations).toEqual(book.dataValidations(sheetId));
|
|
114
|
+
} finally { await book.close(); }
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("writes cross-sheet list formulas with the current sheet name and retains named scope", async () => {
|
|
118
|
+
const original = createXlsx({ ...seed, sheets: [
|
|
119
|
+
{ ...seed.sheets[0]!, validations: [
|
|
120
|
+
{ range: parseWorkbookRange("one", "B2"), definition: { type: "list", source: { kind: "range", sheetId: "two", a1: "C1:C2" } } },
|
|
121
|
+
{ range: parseWorkbookRange("one", "B3"), definition: { type: "list", source: { kind: "name", name: "Status", scopeSheetId: "two" } } }
|
|
122
|
+
] },
|
|
123
|
+
{ ...seed.sheets[1]!, name: "O'Brien Values", cells: seed.sheets[0]!.cells }
|
|
124
|
+
], definedNames: [{ id: "status", name: "Status", scopeSheetId: "two", target: { kind: "range", range: parseWorkbookRange("two", "C1:C2") } }] });
|
|
125
|
+
const archive = unzipSync(original), workbookPart = "xl/workbook.xml";
|
|
126
|
+
archive[workbookPart] = strToU8(strFromU8(archive[workbookPart]!).replace('<definedName name="Status"', '<definedName function="false" vbProcedure="false" name="Status"'));
|
|
127
|
+
const input = openXlsx("Validation", zipSync(archive));
|
|
128
|
+
expect(input.restrictions).toEqual([]);
|
|
129
|
+
const book = await openWorkbook(input.snapshot);
|
|
130
|
+
const [first, second] = book.worksheets();
|
|
131
|
+
const read = (row: number) => book.validationAt({ sheetId: first!.id, row, column: 1 })?.options;
|
|
132
|
+
try {
|
|
133
|
+
expect(read(1)).toEqual(["001", "Paid"]);
|
|
134
|
+
expect(read(2)).toEqual(["001", "Paid"]);
|
|
135
|
+
await book.commit(book.prepare({ operationId: "rename-source", baseRevision: book.revision, commands: [
|
|
136
|
+
{ name: "renameWorksheet", target: parseWorkbookRange(second!.id, "A1"), params: { name: "Options" } }
|
|
137
|
+
] }).id);
|
|
138
|
+
const written = input.write(book.snapshot());
|
|
139
|
+
const xml = strFromU8(unzipSync(written)[sheetPart]!);
|
|
140
|
+
expect(xml).toContain("<formula1>Options!$C$1:$C$2</formula1>");
|
|
141
|
+
expect(xml).toContain("<formula1>Options!Status</formula1>");
|
|
142
|
+
const reopened = openXlsx("Validation", written);
|
|
143
|
+
expect(reopened.restrictions).toEqual([]);
|
|
144
|
+
expect(reopened.write(reopened.snapshot)).toEqual(written);
|
|
145
|
+
} finally { await book.close(); }
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("imports an Office 2010 list alongside ordinary rules and preserves its original until edited", async () => {
|
|
149
|
+
const archive = unzipSync(createXlsx({ ...seed, sheets: [seed.sheets[0]!, { ...seed.sheets[1]!, cells: seed.sheets[0]!.cells }] }));
|
|
150
|
+
const sourceXml = strFromU8(archive[sheetPart]!);
|
|
151
|
+
const ordinary = '<dataValidations count="1"><dataValidation type="list" sqref="A1"><formula1>"One,Two"</formula1></dataValidation></dataValidations>';
|
|
152
|
+
const extended = '<extLst><ext uri="{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}"><x14:dataValidations xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main" count="1">'
|
|
153
|
+
+ '<x14:dataValidation type="list" allowBlank="1" showErrorMessage="1"><x14:formula1><xm:f>Other!$C$1:$C$2</xm:f></x14:formula1><xm:sqref>B2:B3</xm:sqref></x14:dataValidation></x14:dataValidations></ext></extLst>';
|
|
154
|
+
archive[sheetPart] = strToU8(sourceXml.replace("</worksheet>", ordinary + extended + "</worksheet>"));
|
|
155
|
+
const original = zipSync(archive);
|
|
156
|
+
const input = openXlsx("Extended", original);
|
|
157
|
+
expect(input.restrictions).toEqual([]);
|
|
158
|
+
expect(input.write(input.snapshot)).toEqual(original);
|
|
159
|
+
const book = await openWorkbook(input.snapshot);
|
|
160
|
+
const sheetId = book.worksheets()[0]!.id;
|
|
161
|
+
try {
|
|
162
|
+
expect(book.validationAt({ sheetId, row: 2, column: 1 })?.options).toEqual(["001", "Paid"]);
|
|
163
|
+
await book.commit(book.prepare({ operationId: "clear-extended-list", baseRevision: book.revision, commands: [
|
|
164
|
+
{ name: "clearDataValidation", target: parseWorkbookRange(sheetId, "B3"), params: {} }
|
|
165
|
+
] }).id);
|
|
166
|
+
const bytes = input.write(book.snapshot());
|
|
167
|
+
const xml = strFromU8(unzipSync(bytes)[sheetPart]!);
|
|
168
|
+
expect(xml).not.toContain("x14:dataValidation");
|
|
169
|
+
expect(xml).toContain("<formula1>Other!$C$1:$C$2</formula1>");
|
|
170
|
+
const reopened = openXlsx("Extended", bytes);
|
|
171
|
+
expect(reopened.restrictions).toEqual([]);
|
|
172
|
+
expect(reopened.snapshot.sheets[0]?.validations?.map(rule => rule.range)).toEqual([parseWorkbookRange(sheetId, "A1"), parseWorkbookRange(sheetId, "B2")]);
|
|
173
|
+
} finally { await book.close(); }
|
|
174
|
+
archive[sheetPart] = strToU8(sourceXml.replace("</worksheet>", ordinary + extended.replace("Other!$C$1:$C$2", "[1]Other!$C$1:$C$2") + "</worksheet>"));
|
|
175
|
+
const unsupported = zipSync(archive), retained = openXlsx("External source", unsupported);
|
|
176
|
+
expect(retained.restrictions.length).toBeGreaterThan(0);
|
|
177
|
+
expect(retained.snapshot.sheets[0]?.validations).toHaveLength(1);
|
|
178
|
+
expect(retained.write(retained.snapshot)).toEqual(unsupported);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("writes relocated validation targets and a deleted list source through an ordinary worksheet edit", async () => {
|
|
182
|
+
const scope = parseWorkbookRange("one", "E2:E4");
|
|
183
|
+
const input = createXlsx({ ...seed, sheets: [{ ...seed.sheets[0]!, validations: [
|
|
184
|
+
{ range: scope, definition: { type: "list", source: { kind: "range", a1: "C1:C2" } } }
|
|
185
|
+
] }, seed.sheets[1]!] });
|
|
186
|
+
const original = openXlsx("Validation", input);
|
|
187
|
+
const book = await openWorkbook(original.snapshot, { axisRestrictions: original.axisRestrictions });
|
|
188
|
+
try {
|
|
189
|
+
const sheetId = book.worksheets()[0]!.id;
|
|
190
|
+
const edit = book.prepare({ operationId: "delete-source", baseRevision: 0, commands: [
|
|
191
|
+
{ name: "deleteColumns", target: parseWorkbookRange(sheetId, "C1"), params: {} }
|
|
192
|
+
] });
|
|
193
|
+
await book.commit(edit.id);
|
|
194
|
+
const written = original.write(book.snapshot());
|
|
195
|
+
const rebuilt = openXlsx("Validation", written);
|
|
196
|
+
const parsed = rebuilt.snapshot.sheets[0]!.validations;
|
|
197
|
+
expect(parsed).toEqual([{ range: parseWorkbookRange(sheetId, "D2:D4"),
|
|
198
|
+
definition: { type: "list", source: { kind: "broken" } } }]);
|
|
199
|
+
expect(rebuilt.restrictions).toEqual([]);
|
|
200
|
+
await book.undo("restore-source");
|
|
201
|
+
expect(original.write(book.snapshot())).toEqual(input);
|
|
202
|
+
} finally { await book.close(); }
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("keeps standard lists and matches multi-selection metadata by range and source", async () => {
|
|
206
|
+
const standard = openXlsx("Lists", createXlsx(seed));
|
|
207
|
+
const book = await openWorkbook(standard.snapshot);
|
|
208
|
+
try {
|
|
209
|
+
const sheet = book.worksheets()[0]!.id;
|
|
210
|
+
const target = parseWorkbookRange(sheet, "E2:E3");
|
|
211
|
+
const source = { kind: "options", values: ["Pending & short", "Paid"] };
|
|
212
|
+
await book.commit(book.prepare({ operationId: "list-options", baseRevision: book.revision, commands: [
|
|
213
|
+
{ name: "setDataValidation", target, params: { type: "list", source, multiple: true, optionColors: [null, "#196747"], alert: { level: "stop" } } },
|
|
214
|
+
{ name: "setCells", target: parseWorkbookRange(sheet, "E2"), params: { cells: [[{ kind: "value", value: "Paid,Pending & short" }]] } }
|
|
215
|
+
] }).id);
|
|
216
|
+
const file = standard.write(book.snapshot());
|
|
217
|
+
const reopened = openXlsx("Lists", file);
|
|
218
|
+
expect(reopened.restrictions).toEqual([]);
|
|
219
|
+
expect(reopened.snapshot.sheets[0]!.validations).toEqual(book.dataValidations(sheet));
|
|
220
|
+
expect(reopened.snapshot.sheets[0]!.cells.find(cell => cell.column === 4)?.input).toEqual({ kind: "value", value: "Paid,Pending & short" });
|
|
221
|
+
const parts = unzipSync(file);
|
|
222
|
+
const native = strFromU8(parts[sheetPart]!);
|
|
223
|
+
expect(native).toContain('<dataValidation type="list" sqref="E2:E3"');
|
|
224
|
+
expect(native).toContain('<formula1>"Pending & short,Paid"</formula1>');
|
|
225
|
+
// A producer changed the list source while retaining a previous extension.
|
|
226
|
+
parts[sheetPart] = strToU8(native.replace('<formula1>"Pending & short,Paid"</formula1>', '<formula1>"Open,Closed"</formula1>'));
|
|
227
|
+
const stale = zipSync(parts);
|
|
228
|
+
const mismatch = openXlsx("Stale extension", stale);
|
|
229
|
+
expect(mismatch.restrictions).toContain("数据有效性包含暂未支持的规则");
|
|
230
|
+
expect(mismatch.snapshot.sheets[0]!.validations?.[0]?.definition).toMatchObject({ source: { kind: "options", values: ["Open", "Closed"] } });
|
|
231
|
+
expect(mismatch.snapshot.sheets[0]!.validations?.[0]?.definition).not.toHaveProperty("multiple");
|
|
232
|
+
expect(mismatch.write(mismatch.snapshot)).toEqual(stale);
|
|
233
|
+
|
|
234
|
+
await book.commit(book.prepare({ operationId: "clear-list-options", baseRevision: book.revision, commands: [
|
|
235
|
+
{ name: "clearDataValidation", target, params: {} }
|
|
236
|
+
] }).id);
|
|
237
|
+
const cleared = openXlsx("Cleared", reopened.write(book.snapshot()));
|
|
238
|
+
expect(cleared.snapshot.sheets[0]!.validations ?? []).toEqual([]);
|
|
239
|
+
expect(cleared.snapshot.sheets[0]!.cells.find(cell => cell.column === 4)?.input).toMatchObject({ value: "Paid,Pending & short" });
|
|
240
|
+
} finally { await book.close(); }
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("reads only the matched WPS vendor list settings and preserves its original package", () => {
|
|
244
|
+
const parts = unzipSync(createXlsx(seed));
|
|
245
|
+
const id = "{512497dc-6bce-4be0-b9b3-4b5314b44ca6}";
|
|
246
|
+
const input = '<dataValidations count="1"><dataValidation type="list" id="' + id
|
|
247
|
+
+ '" sqref="E2" showErrorMessage="1"><formula1>"Alpha,Beta"</formula1></dataValidation></dataValidations>';
|
|
248
|
+
parts[sheetPart] = strToU8(strFromU8(parts[sheetPart]!).replace("</worksheet>", input + "</worksheet>"));
|
|
249
|
+
const relPath = "xl/_rels/workbook.xml.rels";
|
|
250
|
+
parts[relPath] = strToU8(strFromU8(parts[relPath]!).replace("</Relationships>",
|
|
251
|
+
'<Relationship Id="vendorLists" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/dropdown.xml"/></Relationships>'));
|
|
252
|
+
parts["customXml/dropdown.xml"] = strToU8('<dvExts xmlns="http://www.wps.cn/et/2023/dataValidationExt"'
|
|
253
|
+
+ ' xmlns:s="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><dvExt uuid="' + id
|
|
254
|
+
+ '"><list multiSupport="1" colorSupport="1"><item index="0"><color s:rgb="FF5081C2"/></item></list></dvExt></dvExts>');
|
|
255
|
+
const bytes = zipSync(parts), xlsx = openXlsx("WPS Dropdown", bytes);
|
|
256
|
+
expect(xlsx.snapshot.sheets[0]!.validations?.[0]?.definition).toMatchObject({ type: "list", multiple: true, optionColors: ["#5081C2"] });
|
|
257
|
+
expect(xlsx.restrictions).toEqual(["专用工作簿部件"]);
|
|
258
|
+
expect(xlsx.write(xlsx.snapshot)).toEqual(bytes);
|
|
259
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { copyWorkbookValidations, formatWorkbookRange, formatWorkbookSpillReferences, formatWorkbookValidationReference, isWorkbookTextValidationType,
|
|
3
|
+
type WorkbookRange, type WorkbookValidationDefinition, type WorkbookValidationMessage, type WorkbookValidationRule } from "@quirekit/workbook";
|
|
4
|
+
import { child, children, spreadNs } from "../../opc/xml";
|
|
5
|
+
import { encodeXlsxText } from "../text";
|
|
6
|
+
import { xlsxImageValidationFormula } from "./image";
|
|
7
|
+
import { xlsxTextValidationFormula } from "./text";
|
|
8
|
+
import { findXlsxValidationExtensions } from "./extended";
|
|
9
|
+
import { writeXlsxPortableListOptions } from "./list-options/portable";
|
|
10
|
+
import type { XlsxListOptionsRecord } from "./list-options/types";
|
|
11
|
+
|
|
12
|
+
const afterValidations = new Set([
|
|
13
|
+
"hyperlinks", "printOptions", "pageMargins", "pageSetup", "headerFooter", "rowBreaks", "colBreaks",
|
|
14
|
+
"customProperties", "cellWatches", "ignoredErrors", "smartTags", "drawing", "legacyDrawing", "legacyDrawingHF",
|
|
15
|
+
"picture", "oleObjects", "controls", "webPublishItems", "tableParts", "extLst"
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** @param entry - New native rule. @param prefix - Native message role. @param value - Authored message state. */
|
|
19
|
+
function applyMessage(entry: Element, prefix: "prompt" | "error", value?: WorkbookValidationMessage): void {
|
|
20
|
+
if (!value) return;
|
|
21
|
+
entry.setAttribute(prefix === "prompt" ? "showInputMessage" : "showErrorMessage", value.enabled === false ? "0" : "1");
|
|
22
|
+
if (value.title) entry.setAttribute(prefix + "Title", encodeXlsxText(value.title));
|
|
23
|
+
if (value.message) entry.setAttribute(prefix, encodeXlsxText(value.message));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param definition - Portable validation definition. @param range - Target supplying relative references.
|
|
27
|
+
* @param sheetName - Final name for a stored worksheet identity.
|
|
28
|
+
* @returns Native formula text in schema order. */
|
|
29
|
+
function formulas(definition: WorkbookValidationDefinition, range: WorkbookRange, sheetName: (id: string) => string): readonly (string | number)[] {
|
|
30
|
+
if (definition.type === "any") return [];
|
|
31
|
+
if (definition.type === "custom") return [formatWorkbookSpillReferences(definition.formula, "function").formula.slice(1)];
|
|
32
|
+
if (definition.type === "progress") return [0, 1];
|
|
33
|
+
if (definition.type === "image") return [xlsxImageValidationFormula(range)];
|
|
34
|
+
if (definition.type === "list") {
|
|
35
|
+
const { source } = definition;
|
|
36
|
+
if (source.kind === "broken") return ["#REF!"];
|
|
37
|
+
if (source.kind === "options") return ['"' + source.values.join(",") + '"'];
|
|
38
|
+
const reference = formatWorkbookValidationReference(source, sheetName, true);
|
|
39
|
+
return [formatWorkbookSpillReferences(reference, "function").formula];
|
|
40
|
+
}
|
|
41
|
+
if ("operator" in definition) return definition.second === undefined ? [definition.first] : [definition.first, definition.second];
|
|
42
|
+
return [xlsxTextValidationFormula(definition.type)];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param worksheet - Sheet whose native rules have changed. @param sheetId - Canonical owner.
|
|
46
|
+
* @param source - Final rules in this sheet's coordinates. @param names - Final worksheet names by identity. */
|
|
47
|
+
export function writeXlsxValidations(worksheet: Element, sheetId: string, source: readonly WorkbookValidationRule[] | undefined,
|
|
48
|
+
names: ReadonlyMap<string, string>): void {
|
|
49
|
+
const original = child(worksheet, "dataValidations");
|
|
50
|
+
const rules = copyWorkbookValidations(sheetId, source ?? []);
|
|
51
|
+
// The package admits this write only after all rules in the extension are decoded.
|
|
52
|
+
for (const extension of findXlsxValidationExtensions(worksheet)) {
|
|
53
|
+
const group = extension.parentNode as Element;
|
|
54
|
+
group.removeChild(extension);
|
|
55
|
+
if (!children(group).length) worksheet.removeChild(group);
|
|
56
|
+
}
|
|
57
|
+
if (!rules.length) {
|
|
58
|
+
if (original) worksheet.removeChild(original);
|
|
59
|
+
writeXlsxPortableListOptions(worksheet, []);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const document = worksheet.ownerDocument!;
|
|
63
|
+
const container = document.createElementNS(spreadNs, "dataValidations");
|
|
64
|
+
const records: XlsxListOptionsRecord[] = [];
|
|
65
|
+
container.setAttribute("count", String(rules.length));
|
|
66
|
+
/** @param id - Explicit source sheet. @returns Its current file name. */
|
|
67
|
+
function sheetName(id: string): string {
|
|
68
|
+
const name = names.get(id);
|
|
69
|
+
if (!name) throw new Error("数据有效性的来源工作表不在此工作簿内。");
|
|
70
|
+
return name;
|
|
71
|
+
}
|
|
72
|
+
for (const { range, definition } of rules) {
|
|
73
|
+
const entry = document.createElementNS(spreadNs, "dataValidation");
|
|
74
|
+
entry.setAttribute("type", definition.type === "any" ? "none" : definition.type === "progress" ? "decimal"
|
|
75
|
+
: definition.type === "image" || isWorkbookTextValidationType(definition.type) ? "custom" : definition.type);
|
|
76
|
+
const targets = formatWorkbookRange(range);
|
|
77
|
+
entry.setAttribute("sqref", targets);
|
|
78
|
+
if (definition.allowBlank) entry.setAttribute("allowBlank", "1");
|
|
79
|
+
if ("operator" in definition) entry.setAttribute("operator", definition.operator);
|
|
80
|
+
if (definition.type === "progress") entry.setAttribute("operator", "between");
|
|
81
|
+
if (definition.type === "list" && definition.dropdown === false) entry.setAttribute("showDropDown", "1");
|
|
82
|
+
applyMessage(entry, "prompt", definition.prompt);
|
|
83
|
+
applyMessage(entry, "error", definition.alert);
|
|
84
|
+
if (definition.alert) entry.setAttribute("errorStyle", definition.alert.level);
|
|
85
|
+
const values = formulas(definition, range, sheetName);
|
|
86
|
+
if (definition.type === "list" && (definition.multiple || definition.optionColors)) {
|
|
87
|
+
records.push({ targets, formula: String(values[0]), settings: definition });
|
|
88
|
+
}
|
|
89
|
+
for (const [index, value] of values.entries()) {
|
|
90
|
+
const formula = document.createElementNS(spreadNs, "formula" + (index + 1));
|
|
91
|
+
formula.appendChild(document.createTextNode(String(value)));
|
|
92
|
+
entry.appendChild(formula);
|
|
93
|
+
}
|
|
94
|
+
container.appendChild(entry);
|
|
95
|
+
}
|
|
96
|
+
if (original) worksheet.replaceChild(container, original);
|
|
97
|
+
else worksheet.insertBefore(container, children(worksheet).find(node => afterValidations.has(node.localName ?? "")) ?? null);
|
|
98
|
+
writeXlsxPortableListOptions(worksheet, records);
|
|
99
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { parseWorkbookRange, type WorkbookRange, type WorkbookViewAnchor } from "@quirekit/workbook";
|
|
3
|
+
import { child, children } from "../../opc/xml";
|
|
4
|
+
|
|
5
|
+
/** @param root - Native worksheet. @returns All saved views in their source order. */
|
|
6
|
+
export function xlsxWorksheetViews(root: Element): Element[] {
|
|
7
|
+
const container = child(root, "sheetViews");
|
|
8
|
+
return container ? children(container, "sheetView") : [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** @param root - Native worksheet. @returns The primary workbook window's view, when present. */
|
|
12
|
+
export function primaryXlsxWorksheetView(root: Element): Element | undefined {
|
|
13
|
+
const views = xlsxWorksheetViews(root);
|
|
14
|
+
return views.find(view => view.getAttribute("workbookViewId") === "0") ?? views[0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @param view - Native worksheet view. @returns Whether it is the normal spreadsheet view. */
|
|
18
|
+
export function isNormalXlsxView(view: Element): boolean {
|
|
19
|
+
const mode = view.getAttribute("view");
|
|
20
|
+
return !mode || mode === "normal";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param node - View or selection record. @param attribute - Reference attribute. @param sheetId - Worksheet identity.
|
|
24
|
+
* @returns Ordered rectangles described by the coordinate attribute. */
|
|
25
|
+
export function xlsxViewReferences(node: Element, attribute: string, sheetId: string): WorkbookRange[] {
|
|
26
|
+
const ref = (node.getAttribute(attribute) ?? "").trim();
|
|
27
|
+
return ref ? ref.split(/\s+/u).map(range => parseWorkbookRange(sheetId, range)) : [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param node - Native view, pane or selection. @param attribute - Attribute containing an optional single cell.
|
|
31
|
+
* @returns One logical coordinate without an absolute or relative formula interpretation. */
|
|
32
|
+
export function xlsxViewCell(node: Element, attribute: string): WorkbookViewAnchor | undefined {
|
|
33
|
+
const [range, extra] = xlsxViewReferences(node, attribute, "");
|
|
34
|
+
if (!range) return;
|
|
35
|
+
if (extra || range.startRow !== range.endRow || range.startColumn !== range.endColumn) {
|
|
36
|
+
throw new Error("工作表视图的位置必须为单个单元格。");
|
|
37
|
+
}
|
|
38
|
+
return { row: range.startRow, column: range.startColumn };
|
|
39
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { copyWorkbookNamedViews, formatWorkbookRange, maximumWorkbookNamedViews, type WorkbookAutoFilter,
|
|
3
|
+
type WorkbookNamedSheetView, type WorkbookNamedViewFilter } from "@quirekit/workbook";
|
|
4
|
+
import { relationshipsPath } from "../../../opc/relationships";
|
|
5
|
+
import { readXlsxRevisionGuid, xlsxFilterUidNs, xlsxTableColumnUidNs } from "../../../opc/revision";
|
|
6
|
+
import { hasKnownAttributes } from "../../../opc/shape";
|
|
7
|
+
import type { PackageRelationship } from "../../../opc/types";
|
|
8
|
+
import { child, children, parsePart, readIndex, rootOf, spreadNs } from "../../../opc/xml";
|
|
9
|
+
import { readXlsxDifferential } from "../../../styles/differential";
|
|
10
|
+
import type { XlsxSourceTable } from "../../../table/types";
|
|
11
|
+
import { readXlsxAutoFilter } from "../../filters/read";
|
|
12
|
+
import type { XlsxFilterReadContext } from "../../filters/types";
|
|
13
|
+
import type { XlsxNamedViewFilterOwner } from "./types";
|
|
14
|
+
import { readXlsxNamedViewSort } from "./sort";
|
|
15
|
+
import { readXlsxNamedViewColumn, unsupportedXlsxNamedView as unsupported, xlsxNamedViewNs, xlsxNamedViewRelationship } from "./xml";
|
|
16
|
+
|
|
17
|
+
/** @param root - Native filter owner. @returns Its optional revision identity. */
|
|
18
|
+
function filterUid(root: Element): string | undefined {
|
|
19
|
+
return readXlsxRevisionGuid(child(root, "autoFilter")?.getAttributeNS(xlsxFilterUidNs, "uid"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @param parts - Source workbook. @param root - Worksheet XML. @param filter - Decoded worksheet filter.
|
|
23
|
+
* @param tables - Native table definitions owned by the same worksheet. @returns Candidates for saved filter reconciliation. */
|
|
24
|
+
function filterOwners(parts: Readonly<Record<string, Uint8Array>>, root: Element, filter: WorkbookAutoFilter | undefined,
|
|
25
|
+
tables: readonly XlsxSourceTable[]): readonly XlsxNamedViewFilterOwner[] {
|
|
26
|
+
const owners: XlsxNamedViewFilterOwner[] = [];
|
|
27
|
+
if (filter && !filter.advanced) owners.push({ serial: 0, uid: filterUid(root), range: filter.range });
|
|
28
|
+
for (const { id, path, layout } of tables) {
|
|
29
|
+
if (!layout.filter || !layout.id) continue;
|
|
30
|
+
const table = rootOf(parsePart(parts[path], path), spreadNs, "table");
|
|
31
|
+
const fields = child(table, "tableColumns");
|
|
32
|
+
owners.push({ serial: id, tableId: layout.id, uid: filterUid(table), range: layout.filter.value.range,
|
|
33
|
+
columnUids: fields && children(fields, "tableColumn").map(node => readXlsxRevisionGuid(node.getAttributeNS(xlsxTableColumnUidNs, "uid"))) });
|
|
34
|
+
}
|
|
35
|
+
return owners;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @param node - Named view scope XML. @param owners - Local workbook filter owners.
|
|
39
|
+
* @param context - Source style and date semantics. @returns The complete override for the reconciled source filter. */
|
|
40
|
+
function readFilter(node: Element, owners: readonly XlsxNamedViewFilterOwner[], context: XlsxFilterReadContext): WorkbookNamedViewFilter {
|
|
41
|
+
const uid = readXlsxRevisionGuid(node.getAttribute("filterId"));
|
|
42
|
+
const serial = readIndex(node, "tableId") ?? 0;
|
|
43
|
+
const matched = owners.filter(owner => owner.uid === uid && uid !== undefined);
|
|
44
|
+
const owner = matched.length === 1 ? matched[0] : matched.length === 0 ? owners.find(owner => owner.serial === serial) : undefined;
|
|
45
|
+
if (!uid || !owner || !hasKnownAttributes(node, ["filterId", "ref", "tableId"])
|
|
46
|
+
|| node.hasAttribute("tableId") && readIndex(node, "tableId") === undefined) throw unsupported();
|
|
47
|
+
const { range, tableId } = owner;
|
|
48
|
+
const document = node.ownerDocument!;
|
|
49
|
+
const parent = document.createElementNS(spreadNs, "worksheet");
|
|
50
|
+
const native = document.createElementNS(spreadNs, "autoFilter");
|
|
51
|
+
native.setAttribute("ref", formatWorkbookRange(range));
|
|
52
|
+
parent.appendChild(native);
|
|
53
|
+
const differentials: Array<ReturnType<typeof readXlsxDifferential>> = [];
|
|
54
|
+
const entries = children(node);
|
|
55
|
+
const sorts = entries.filter(entry => entry.namespaceURI === xlsxNamedViewNs && entry.localName === "sortRules");
|
|
56
|
+
if (sorts.length > 1 || sorts[0] && sorts[0] !== entries.at(-1)) throw unsupported();
|
|
57
|
+
for (const entry of entries) {
|
|
58
|
+
if (entry === sorts[0]) continue;
|
|
59
|
+
if (entry.namespaceURI !== xlsxNamedViewNs || entry.localName !== "columnFilter"
|
|
60
|
+
|| !hasKnownAttributes(entry, ["colId", "id"])) throw unsupported();
|
|
61
|
+
const index = readXlsxNamedViewColumn(entry, owner);
|
|
62
|
+
const records = children(entry);
|
|
63
|
+
const criteria = records.filter(member => member.namespaceURI === xlsxNamedViewNs && member.localName === "filter");
|
|
64
|
+
const dxfs = records.filter(member => member.namespaceURI === xlsxNamedViewNs && member.localName === "dxf");
|
|
65
|
+
if (criteria.length > 1 || dxfs.length > 1 || records.length !== criteria.length + dxfs.length) throw unsupported();
|
|
66
|
+
const source = criteria[0];
|
|
67
|
+
if (!source) {
|
|
68
|
+
if (dxfs.length) throw unsupported();
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (!hasKnownAttributes(source, ["colId", "hiddenButton", "showButton"])
|
|
72
|
+
|| source.hasAttribute("hiddenButton") || source.hasAttribute("showButton")) throw unsupported();
|
|
73
|
+
const column = document.createElementNS(spreadNs, "filterColumn");
|
|
74
|
+
column.setAttribute("colId", String(index));
|
|
75
|
+
for (const element of children(source)) column.appendChild(element.cloneNode(true));
|
|
76
|
+
const color = child(column, "colorFilter");
|
|
77
|
+
const dxf = dxfs[0];
|
|
78
|
+
if (Boolean(color) !== Boolean(dxf)) throw unsupported();
|
|
79
|
+
if (color && dxf) {
|
|
80
|
+
const style = readXlsxDifferential(dxf, context.resolveColor, xlsxNamedViewNs);
|
|
81
|
+
if (!style) throw unsupported();
|
|
82
|
+
color.setAttribute("dxfId", String(differentials.push(style) - 1));
|
|
83
|
+
}
|
|
84
|
+
native.appendChild(column);
|
|
85
|
+
}
|
|
86
|
+
const { columns } = readXlsxAutoFilter(parent, range.sheetId, false, { ...context, differentials })!;
|
|
87
|
+
const sort = sorts[0] && readXlsxNamedViewSort(sorts[0], owner, context);
|
|
88
|
+
return { tableId, range, columns, ...(sort && { sort }) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @param parts - Source package. @param relations - Implicit worksheet relationships.
|
|
92
|
+
* @param worksheet - Main worksheet part. @param ordinary - Parsed worksheet filter. @param tables - Native tables of the sheet.
|
|
93
|
+
* @param context - Source styles and calendar. @returns Owned definition part and validated named views. */
|
|
94
|
+
export function readXlsxNamedViews(parts: Readonly<Record<string, Uint8Array>>, relations: ReadonlyMap<string, PackageRelationship> | undefined,
|
|
95
|
+
worksheet: Element, ordinary: WorkbookAutoFilter | undefined, tables: readonly XlsxSourceTable[], context: XlsxFilterReadContext):
|
|
96
|
+
{ readonly relationshipId: string; readonly path: string; readonly views: readonly WorkbookNamedSheetView[] } | undefined {
|
|
97
|
+
const related = [...(relations?.values() ?? [])].filter(relation => relation.type === xlsxNamedViewRelationship);
|
|
98
|
+
if (!related.length) return;
|
|
99
|
+
const [{ path, id: relationshipId }] = related as [PackageRelationship, ...PackageRelationship[]];
|
|
100
|
+
if (related.length !== 1 || !path || !parts[path] || parts[relationshipsPath(path)]) throw unsupported();
|
|
101
|
+
const root = rootOf(parsePart(parts[path], path), xlsxNamedViewNs, "namedSheetViews");
|
|
102
|
+
const nodes = children(root);
|
|
103
|
+
if (!nodes.length || nodes.length > maximumWorkbookNamedViews || !hasKnownAttributes(root, [])) throw unsupported();
|
|
104
|
+
const owners = filterOwners(parts, worksheet, ordinary, tables);
|
|
105
|
+
const views = copyWorkbookNamedViews(nodes.map(view => {
|
|
106
|
+
if (view.namespaceURI !== xlsxNamedViewNs || view.localName !== "namedSheetView" || !hasKnownAttributes(view, ["name", "id"])) throw unsupported();
|
|
107
|
+
const id = readXlsxRevisionGuid(view.getAttribute("id"));
|
|
108
|
+
if (!id) throw unsupported();
|
|
109
|
+
const filters = children(view).map(scope => {
|
|
110
|
+
if (scope.namespaceURI !== xlsxNamedViewNs || scope.localName !== "nsvFilter") throw unsupported();
|
|
111
|
+
return readFilter(scope, owners, context);
|
|
112
|
+
});
|
|
113
|
+
return { id, name: view.getAttribute("name") ?? "", filters };
|
|
114
|
+
}))!;
|
|
115
|
+
return { relationshipId, path, views };
|
|
116
|
+
}
|