@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,76 @@
|
|
|
1
|
+
import {
|
|
2
|
+
maximumWorkbookChartSeries, workbookChartTickFormats, type WorkbookChartGrouping, type WorkbookChartType,
|
|
3
|
+
type WorkbookChartValueAxis, type WorkbookChartValueScale
|
|
4
|
+
} from "@quirekit/workbook";
|
|
5
|
+
import type { Element } from "@xmldom/xmldom";
|
|
6
|
+
import { children, isTrue, readIndex } from "../../opc/xml";
|
|
7
|
+
import { chartNamespace } from "../namespaces";
|
|
8
|
+
import { readCartesianAxisPairs } from "../presentation/cartesian/axes";
|
|
9
|
+
import { chartChild, chartValue } from "../presentation/properties";
|
|
10
|
+
import type { XlsxChartDefinitionPlots, XlsxChartDefinitionSeries } from "./types";
|
|
11
|
+
|
|
12
|
+
const families = { lineChart: "line", areaChart: "area", pieChart: "pie", doughnutChart: "doughnut" } as const;
|
|
13
|
+
|
|
14
|
+
/** @param plot - Source drawing group. @returns Its regular chart family, when supported. */
|
|
15
|
+
function chartType(plot: Element): Exclude<WorkbookChartType, "combination"> | undefined {
|
|
16
|
+
const family = plot.localName;
|
|
17
|
+
if (family === "barChart") return chartValue(plot, "barDir") === "bar" ? "bar" : "column";
|
|
18
|
+
return family && Object.hasOwn(families, family) ? families[family as keyof typeof families] : undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @param plot - Source drawing group. @returns Workbook arrangement. */
|
|
22
|
+
function chartGrouping(plot: Element): WorkbookChartGrouping {
|
|
23
|
+
const grouping = chartValue(plot, "grouping");
|
|
24
|
+
return grouping === "stacked" || grouping === "percentStacked" ? grouping : "standard";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @param axis - Linked value axis from the original part. @returns Source-format scale settings understood by the model. */
|
|
28
|
+
function chartValueScale(axis: Element): WorkbookChartValueScale {
|
|
29
|
+
const scaling = chartChild(axis, "scaling");
|
|
30
|
+
const minimum = chartValue(scaling, "min"), maximum = chartValue(scaling, "max");
|
|
31
|
+
const format = workbookChartTickFormats.find(code => code === chartChild(axis, "numFmt")?.getAttribute("formatCode"));
|
|
32
|
+
return { ...(minimum && { min: Number(minimum) }), ...(maximum && { max: Number(maximum) }), ...(format && { format }) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @param area - Source plot area. @returns Families, scale settings and source references in their global order. */
|
|
36
|
+
export function readChartPlots(area: Element): XlsxChartDefinitionPlots | undefined {
|
|
37
|
+
const plots = children(area).filter(node => node.namespaceURI === chartNamespace && node.localName?.endsWith("Chart"));
|
|
38
|
+
const first = plots[0];
|
|
39
|
+
if (!first || plots.length > maximumWorkbookChartSeries) return;
|
|
40
|
+
const grouping = chartGrouping(first);
|
|
41
|
+
const dataLabels = isTrue(chartValue(chartChild(first, "dLbls"), "showVal"));
|
|
42
|
+
if (plots.length === 1) {
|
|
43
|
+
const type = chartType(first);
|
|
44
|
+
if (type) return { type, grouping, dataLabels, series: children(first, "ser").map(node => ({ node })) };
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const pairs = readCartesianAxisPairs(area, plots);
|
|
49
|
+
if (!pairs) return;
|
|
50
|
+
const byOrder: (XlsxChartDefinitionSeries | undefined)[] = [];
|
|
51
|
+
const scales: Partial<Record<WorkbookChartValueAxis, WorkbookChartValueScale>> = {};
|
|
52
|
+
const bound = new Map<WorkbookChartValueAxis, Element>();
|
|
53
|
+
|
|
54
|
+
// Axis IDs are local to the chart. Series order is global, even when the file stores groups separately.
|
|
55
|
+
for (const [index, plot] of plots.entries()) {
|
|
56
|
+
const type = chartType(plot);
|
|
57
|
+
const valueAxis = pairs[index]?.value;
|
|
58
|
+
const position = chartValue(valueAxis, "axPos");
|
|
59
|
+
const axis = position === "l" ? "primary" : position === "r" ? "secondary" : undefined;
|
|
60
|
+
if (!valueAxis || !axis || (type !== "column" && type !== "line" && type !== "area")
|
|
61
|
+
|| chartGrouping(plot) !== grouping || (bound.has(axis) && bound.get(axis) !== valueAxis)) return;
|
|
62
|
+
bound.set(axis, valueAxis);
|
|
63
|
+
scales[axis] = chartValueScale(valueAxis);
|
|
64
|
+
const nativeSeries = children(plot, "ser");
|
|
65
|
+
if (!nativeSeries.length) return;
|
|
66
|
+
for (const node of nativeSeries) {
|
|
67
|
+
const order = readIndex(chartChild(node, "order"), "val");
|
|
68
|
+
if (order === undefined || order >= maximumWorkbookChartSeries || byOrder[order]) return;
|
|
69
|
+
byOrder[order] = { node, plot: { type, axis } };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// An array hole would make a series' identity ambiguous when authoring again.
|
|
73
|
+
const series = byOrder.filter((entry): entry is XlsxChartDefinitionSeries => entry !== undefined);
|
|
74
|
+
if (series.length !== byOrder.length) return;
|
|
75
|
+
return { type: "combination", grouping, dataLabels, series, valueAxes: scales };
|
|
76
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { copyWorkbookChart, workbookRangeSize, type WorkbookChart, type WorkbookDrawingAnchor } from "@quirekit/workbook";
|
|
3
|
+
import { child, isTrue, parsePart, rootOf } from "../../opc/xml";
|
|
4
|
+
import { chartTitleText } from "../chart";
|
|
5
|
+
import { chartNamespace } from "../namespaces";
|
|
6
|
+
import { readChartRange } from "../sources";
|
|
7
|
+
import { writeChartSpace } from "../write/chart";
|
|
8
|
+
import { readChartPlots } from "./plots";
|
|
9
|
+
import { chartShape } from "./shape";
|
|
10
|
+
|
|
11
|
+
const legends = { l: "left", t: "top", r: "right", b: "bottom" } as const;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reads a re-authorable chart. The structural comparison keeps richer native
|
|
15
|
+
* layouts in the retained file when rebuilding them would remove information.
|
|
16
|
+
* @param space - Native ChartML part.
|
|
17
|
+
* @param id - Portable drawing identity.
|
|
18
|
+
* @param anchor - Parsed worksheet placement.
|
|
19
|
+
* @param sheets - Worksheet identities by lowercase names.
|
|
20
|
+
* @returns Chart definition when its native structure maps to the writable model.
|
|
21
|
+
*/
|
|
22
|
+
export function readXlsxChartDefinition(space: Element, id: string, anchor: WorkbookDrawingAnchor, sheets: ReadonlyMap<string, string>): WorkbookChart | undefined {
|
|
23
|
+
const chart = child(space, "chart");
|
|
24
|
+
const area = chart && child(chart, "plotArea");
|
|
25
|
+
if (!chart || !area) return;
|
|
26
|
+
const plots = readChartPlots(area);
|
|
27
|
+
if (!plots) return;
|
|
28
|
+
const { type, grouping, valueAxes, dataLabels } = plots;
|
|
29
|
+
const blank = child(chart, "dispBlanksAs")?.getAttribute("val");
|
|
30
|
+
const legend = legends[child(child(chart, "legend") ?? chart, "legendPos")?.getAttribute("val") as keyof typeof legends] ?? "none";
|
|
31
|
+
|
|
32
|
+
/** @param holder - Native category, value or name holder. @param kind - Expected range formula wrapper. @returns Resolved local range if present. */
|
|
33
|
+
function reference(holder: Element | undefined, kind: "numRef" | "strRef") {
|
|
34
|
+
const ref = holder && child(holder, kind);
|
|
35
|
+
const formula = ref && child(ref, "f")?.textContent;
|
|
36
|
+
return formula ? readChartRange(formula, anchor.sheetId, sheets) ?? undefined : undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const first = plots.series[0]?.node;
|
|
40
|
+
const categories = reference(first && child(first, "cat"), "strRef");
|
|
41
|
+
const series = [];
|
|
42
|
+
for (const { node: member, plot } of plots.series) {
|
|
43
|
+
if (JSON.stringify(reference(child(member, "cat"), "strRef")) !== JSON.stringify(categories)) return;
|
|
44
|
+
const values = reference(child(member, "val"), "numRef");
|
|
45
|
+
const tx = child(member, "tx");
|
|
46
|
+
const label = reference(tx, "strRef");
|
|
47
|
+
if (!values || (label && workbookRangeSize(label) !== 1)) return;
|
|
48
|
+
const name = tx && child(tx, "v")?.textContent;
|
|
49
|
+
series.push({ values, ...(plot && { plot }), ...(label && { nameCell: { sheetId: label.sheetId, row: label.startRow, column: label.startColumn } }),
|
|
50
|
+
...(name !== null && name !== undefined && { name }) });
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const value = copyWorkbookChart({ id, anchor, type, grouping,
|
|
54
|
+
title: chartTitleText(child(chart, "title")), legend,
|
|
55
|
+
dataLabels, ...(valueAxes && { valueAxes }),
|
|
56
|
+
blankPolicy: blank === "zero" || blank === "span" ? blank : "gap",
|
|
57
|
+
showHidden: !isTrue(child(chart, "plotVisOnly")?.getAttribute("val")),
|
|
58
|
+
binding: { categories, series }
|
|
59
|
+
});
|
|
60
|
+
const names = new Map([...sheets].map(([name, sheetId]) => [sheetId, name]));
|
|
61
|
+
const data = { categories: [], hasFormula: false, series: series.map(item => ({ name: item.name ?? "", values: [] })) };
|
|
62
|
+
const serialized = rootOf(parsePart(writeChartSpace(value, data, names), "chart.xml"), chartNamespace, "chartSpace");
|
|
63
|
+
if (JSON.stringify(chartShape(serialized)) === JSON.stringify(chartShape(space))) return value;
|
|
64
|
+
} catch { /* The original part remains available without replacing unmodeled fields. */ }
|
|
65
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { hasKnownAttributes, hasKnownChildren } from "../../opc/shape";
|
|
3
|
+
import { child, children, readIndex } from "../../opc/xml";
|
|
4
|
+
import { chartNamespace } from "../namespaces";
|
|
5
|
+
|
|
6
|
+
/** @param node - Standard string or numeric cache. @param numeric - Whether point values must be numbers.
|
|
7
|
+
* @returns Whether refreshing the cache would replace only derived data. */
|
|
8
|
+
function isDerivedCache(node: Element, numeric: boolean): boolean {
|
|
9
|
+
const count = child(node, "ptCount"), format = child(node, "formatCode");
|
|
10
|
+
const size = readIndex(count, "val");
|
|
11
|
+
const indexes = new Set<number>();
|
|
12
|
+
if (size === undefined || !count || !hasKnownAttributes(node, [])
|
|
13
|
+
|| !hasKnownAttributes(count, ["val"]) || !hasKnownChildren(count, [])
|
|
14
|
+
|| (format && (!numeric || format.textContent !== "General" || !hasKnownAttributes(format, []) || !hasKnownChildren(format, [])))) return false;
|
|
15
|
+
return children(node).every(member => {
|
|
16
|
+
if (member.namespaceURI !== chartNamespace) return false;
|
|
17
|
+
if (member === count || member === format) return true;
|
|
18
|
+
const index = readIndex(member, "idx"), value = child(member, "v");
|
|
19
|
+
if (member.localName !== "pt" || !value || index === undefined || index >= size || indexes.has(index)
|
|
20
|
+
|| !hasKnownAttributes(member, ["idx"]) || !hasKnownChildren(member, ["v"])
|
|
21
|
+
|| !hasKnownAttributes(value, []) || !hasKnownChildren(value, [])) return false;
|
|
22
|
+
indexes.add(index);
|
|
23
|
+
return !numeric || (Boolean(value.textContent?.trim()) && Number.isFinite(Number(value.textContent)));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @param node - Native chart XML element. @returns Its modeled structure independent of prefixes and derived caches. */
|
|
28
|
+
export function chartShape(node: Element): unknown {
|
|
29
|
+
const name = node.localName ?? "";
|
|
30
|
+
const attrs = Array.from(node.attributes).filter(attr => attr.namespaceURI !== "http://www.w3.org/2000/xmlns/")
|
|
31
|
+
.map(attr => [attr.namespaceURI ?? "", attr.localName, attr.value]).sort(([a, b], [c, d]) => `${a}:${b}`.localeCompare(`${c}:${d}`));
|
|
32
|
+
let items = children(node);
|
|
33
|
+
if (node.namespaceURI === chartNamespace && (name === "strRef" || name === "numRef")) {
|
|
34
|
+
const numeric = name === "numRef";
|
|
35
|
+
const caches = items.filter(part => part.namespaceURI === chartNamespace && part.localName === (numeric ? "numCache" : "strCache"));
|
|
36
|
+
if (caches.length === 1 && isDerivedCache(caches[0]!, numeric)) items = items.filter(part => part !== caches[0]);
|
|
37
|
+
}
|
|
38
|
+
const text = items.length || (node.namespaceURI === chartNamespace && ["f", "strRef", "numRef"].includes(name)) ? "" : node.textContent ?? "";
|
|
39
|
+
return [node.namespaceURI, name, attrs, text, items.map(chartShape)];
|
|
40
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { WorkbookChartGrouping, WorkbookChartSeriesPlot, WorkbookChartType, WorkbookChartValueAxes } from "@quirekit/workbook";
|
|
2
|
+
import type { Element } from "@xmldom/xmldom";
|
|
3
|
+
|
|
4
|
+
/** Native series in authoring order. */
|
|
5
|
+
export interface XlsxChartDefinitionSeries {
|
|
6
|
+
/** Series element with its title, data references and caches. */
|
|
7
|
+
readonly node: Element;
|
|
8
|
+
/** Chart type and value axis for a combination series. */
|
|
9
|
+
readonly plot?: WorkbookChartSeriesPlot;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Plot data admitted as a candidate for the structural write-back comparison. */
|
|
13
|
+
export interface XlsxChartDefinitionPlots {
|
|
14
|
+
/** User-facing chart family. */
|
|
15
|
+
readonly type: WorkbookChartType;
|
|
16
|
+
/** Arrangement supported consistently by the native groups. */
|
|
17
|
+
readonly grouping: WorkbookChartGrouping;
|
|
18
|
+
/** Common numeric label visibility. */
|
|
19
|
+
readonly dataLabels: boolean;
|
|
20
|
+
/** Exact global series sequence (which can differ from XML group order). */
|
|
21
|
+
readonly series: readonly XlsxChartDefinitionSeries[];
|
|
22
|
+
/** Authored formats and scaling for combination charts. */
|
|
23
|
+
readonly valueAxes?: WorkbookChartValueAxes;
|
|
24
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { copyWorkbookFloatingPicture } from "@quirekit/workbook";
|
|
2
|
+
import type { Element } from "@xmldom/xmldom";
|
|
3
|
+
import { v4 as uuidv4 } from "uuid";
|
|
4
|
+
import type { XlsxImageAssetReader } from "../image/types";
|
|
5
|
+
import { hasKnownAttributes, hasKnownChildren } from "../opc/shape";
|
|
6
|
+
import type { PackageRelationship } from "../opc/types";
|
|
7
|
+
import { child, children, readIndex, relationshipNs } from "../opc/xml";
|
|
8
|
+
import { drawingCreationId } from "./identity";
|
|
9
|
+
import { drawingMainNamespace, drawingNamespace } from "./namespaces";
|
|
10
|
+
import { readDrawingPicture } from "./picture";
|
|
11
|
+
import type { XlsxDrawingLayout } from "./placement/types";
|
|
12
|
+
import type { XlsxPicturePart } from "./types";
|
|
13
|
+
|
|
14
|
+
/** @param node - Candidate element. @param attributes - Modeled attributes.
|
|
15
|
+
* @param members - Modeled children. @param namespace - Child namespace override.
|
|
16
|
+
* @returns Whether the available element is entirely represented by the picture writer. */
|
|
17
|
+
function known(node: Element | undefined, attributes: readonly string[], members: readonly string[] = [], namespace?: string): node is Element {
|
|
18
|
+
return Boolean(node && hasKnownAttributes(node, attributes) && hasKnownChildren(node, members, namespace));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @param pic - Native picture. @param fill - Direct bitmap fill. @returns Its local embed relationship ID, when no extra fill data would be lost. */
|
|
22
|
+
function pictureRelationship(pic: Element, fill: Element | undefined): string | undefined {
|
|
23
|
+
if (!known(pic, [], ["nvPicPr", "blipFill", "spPr"]) || !known(fill, [], ["blip", "srcRect", "stretch"], drawingMainNamespace)) return;
|
|
24
|
+
const blip = child(fill, "blip"), stretch = child(fill, "stretch"), source = child(fill, "srcRect");
|
|
25
|
+
if (!blip || !hasKnownChildren(blip, []) || Array.from(blip.attributes).some(attr =>
|
|
26
|
+
attr.namespaceURI !== "http://www.w3.org/2000/xmlns/" && (attr.namespaceURI !== relationshipNs || attr.localName !== "embed"))) return;
|
|
27
|
+
if (source && !known(source, ["l", "t", "r", "b"])) return;
|
|
28
|
+
if (!known(stretch, [], ["fillRect"]) || !known(child(stretch, "fillRect"), [])) return;
|
|
29
|
+
return blip.getAttributeNS(relationshipNs, "embed") || undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @param shape - Native properties. @returns Whether the two anchor markers fully describe the rectangular frame. */
|
|
33
|
+
function knownShape(shape: Element | undefined): boolean {
|
|
34
|
+
if (!known(shape, [], ["xfrm", "prstGeom"], drawingMainNamespace)) return false;
|
|
35
|
+
const transform = child(shape, "xfrm"), geometry = child(shape, "prstGeom");
|
|
36
|
+
if (transform && (!known(transform, ["flipH", "flipV", "rot"], ["off", "ext"])
|
|
37
|
+
|| ["flipH", "flipV"].some(attr => transform.hasAttribute(attr) && !["0", "1", "true", "false"].includes(transform.getAttribute(attr)!))
|
|
38
|
+
|| children(transform).some(part => !known(part, part.localName === "off" ? ["x", "y"] : ["cx", "cy"])
|
|
39
|
+
|| Array.from(part.attributes).filter(attr => !attr.namespaceURI).some(attr => !/^-?\d+$/u.test(attr.value))))) return false;
|
|
40
|
+
if (!known(geometry, ["prst"], ["avLst"]) || geometry.getAttribute("prst") !== "rect") return false;
|
|
41
|
+
const adjustments = child(geometry, "avLst");
|
|
42
|
+
return !adjustments || known(adjustments, []);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param node - Strict two-cell anchor checked by the parent graph.
|
|
47
|
+
* @param pic - Its native picture element.
|
|
48
|
+
* @param sheetId - Worksheet owner.
|
|
49
|
+
* @param order - Native stacking position.
|
|
50
|
+
* @param relations - Local drawing relationships.
|
|
51
|
+
* @param image - Raster reader with existing byte and pixel limits.
|
|
52
|
+
* @param register - Associates media bytes with a workbook resource ID by package path.
|
|
53
|
+
* @param layout - Native worksheet metrics.
|
|
54
|
+
* @returns A simple native rectangular picture with fully mapped properties.
|
|
55
|
+
*/
|
|
56
|
+
export function readEditableXlsxPicture(node: Element, pic: Element, sheetId: string, order: number,
|
|
57
|
+
relations: ReadonlyMap<string, PackageRelationship>, image: XlsxImageAssetReader,
|
|
58
|
+
register: (path: string, asset: NonNullable<ReturnType<XlsxImageAssetReader>>) => string, layout: XlsxDrawingLayout): XlsxPicturePart | undefined {
|
|
59
|
+
const relationshipId = pictureRelationship(pic, child(pic, "blipFill"));
|
|
60
|
+
const relation = relationshipId && relations.get(relationshipId);
|
|
61
|
+
if (!relationshipId || !relation || !relation.path || relation.type !== relationshipNs + "/image"
|
|
62
|
+
|| !hasKnownAttributes(relation.element, ["Id", "Type", "Target", "TargetMode"]) || !hasKnownChildren(relation.element, [])
|
|
63
|
+
|| !["", "Internal"].includes(relation.element.getAttribute("TargetMode") ?? "")) return;
|
|
64
|
+
const nonvisual = child(pic, "nvPicPr");
|
|
65
|
+
const props = nonvisual && child(nonvisual, "cNvPr"), locking = nonvisual && child(nonvisual, "cNvPicPr");
|
|
66
|
+
if (!known(nonvisual, [], ["cNvPr", "cNvPicPr"], drawingNamespace) || !props || !known(locking, [], ["picLocks"], drawingMainNamespace)
|
|
67
|
+
|| !knownShape(child(pic, "spPr"))) return;
|
|
68
|
+
const lock = child(locking, "picLocks");
|
|
69
|
+
if (lock && (!known(lock, ["noChangeAspect"]) || !["1", "true"].includes(lock.getAttribute("noChangeAspect") ?? ""))) return;
|
|
70
|
+
const id = drawingCreationId(props, ["id", "name", "descr"]);
|
|
71
|
+
const frameId = readIndex(props, "id");
|
|
72
|
+
const native = readDrawingPicture(node, pic, sheetId, order, relations, image, layout);
|
|
73
|
+
if (!frameId || id === null || !native) return;
|
|
74
|
+
const { asset, ...appearance } = native.picture;
|
|
75
|
+
const picture = copyWorkbookFloatingPicture({
|
|
76
|
+
...appearance, anchor: native.anchor, order, id: id ?? uuidv4(),
|
|
77
|
+
name: props.getAttribute("name") ?? "", assetId: register(relation.path, asset)
|
|
78
|
+
});
|
|
79
|
+
return { picture, path: relation.path, relationshipId, frameId };
|
|
80
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { assertWorkbookShapeConnectionGraph, type WorkbookDrawingPoint } from "@quirekit/workbook";
|
|
3
|
+
import { v4 as uuidv4 } from "uuid";
|
|
4
|
+
import type { XlsxImageAssetReader } from "../image/types";
|
|
5
|
+
import type { PackageRelationship } from "../opc/types";
|
|
6
|
+
import { child, children, parsePart, readIndex, relationshipNs, rootOf } from "../opc/xml";
|
|
7
|
+
import { readXlsxChartDefinition } from "./definition/read";
|
|
8
|
+
import { chartNamespace, drawingMainNamespace, drawingNamespace } from "./namespaces";
|
|
9
|
+
import { readDrawingMarker } from "./placement/marker";
|
|
10
|
+
import { drawingPartRelationships } from "./read";
|
|
11
|
+
import { hasKnownAttributes, hasKnownChildren } from "../opc/shape";
|
|
12
|
+
import { readEditableXlsxPicture } from "./editable-picture";
|
|
13
|
+
import { readWorksheetShape } from "./shapes/read";
|
|
14
|
+
import type { XlsxWorksheetShapeTheme } from "./shapes/types";
|
|
15
|
+
import { drawingCreationId } from "./identity";
|
|
16
|
+
import type { XlsxDrawingLayout } from "./placement/types";
|
|
17
|
+
import type { XlsxDrawingGraph, XlsxChartPart, XlsxPicturePart, XlsxShapePart } from "./types";
|
|
18
|
+
|
|
19
|
+
/** @param node - Native cell corner. @returns Coordinates if all marker data can be preserved. */
|
|
20
|
+
function chartMarker(node: Element | undefined): WorkbookDrawingPoint | null {
|
|
21
|
+
if (!node || !hasKnownAttributes(node, []) || !hasKnownChildren(node, ["col", "colOff", "row", "rowOff"])
|
|
22
|
+
|| children(node).some(part => !hasKnownAttributes(part, []) || !hasKnownChildren(part, []))) return null;
|
|
23
|
+
return readDrawingMarker(node);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param node - Drawing or chart reference. @param links - Its OPC relationships. @param kind - Native relationship type.
|
|
27
|
+
* @returns Identity and path of a known internal target. */
|
|
28
|
+
function chartLink(node: Element, links: ReadonlyMap<string, PackageRelationship>, kind: "drawing" | "chart") {
|
|
29
|
+
const link = links.get(node.getAttributeNS(relationshipNs, "id") ?? "");
|
|
30
|
+
return link?.path && link.type === `${relationshipNs}/${kind}` && hasKnownAttributes(node, [], true) && hasKnownChildren(node, [])
|
|
31
|
+
&& hasKnownAttributes(link.element, ["Id", "Type", "Target", "TargetMode"]) && hasKnownChildren(link.element, [])
|
|
32
|
+
&& ["", "Internal"].includes(link.element.getAttribute("TargetMode") ?? "")
|
|
33
|
+
? { id: link.id, path: link.path }
|
|
34
|
+
: undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @param node - Frame transform. @returns Whether rectangular placement is fully expressed by its anchors. */
|
|
38
|
+
function isAnchorTransform(node: Element): boolean {
|
|
39
|
+
if (!hasKnownAttributes(node, []) || !hasKnownChildren(node, ["off", "ext"], drawingMainNamespace)) return false;
|
|
40
|
+
return children(node).every(part => hasKnownChildren(part, [])
|
|
41
|
+
&& hasKnownAttributes(part, part.localName === "off" ? ["x", "y"] : ["cx", "cy"])
|
|
42
|
+
&& Array.from(part.attributes).filter(attr => !attr.namespaceURI).every(attr => attr.value === "0"));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param parts - Local OPC archive.
|
|
47
|
+
* @param path - Worksheet part path.
|
|
48
|
+
* @param worksheet - Parsed worksheet.
|
|
49
|
+
* @param sheetId - Workbook sheet identity.
|
|
50
|
+
* @param sheets - Workbook worksheet names by lowercase spelling.
|
|
51
|
+
* @param hyperlinkRelations - Independently managed external cell links in the same worksheet.
|
|
52
|
+
* @param image - Bounded raster resolver.
|
|
53
|
+
* @param theme - Workbook shape style references.
|
|
54
|
+
* @param register - Associates validated picture bytes with reusable workbook media.
|
|
55
|
+
* @param layout - Native worksheet metrics.
|
|
56
|
+
* @returns Exclusive native drawing graph if every drawing is safe to re-author.
|
|
57
|
+
*/
|
|
58
|
+
export function readXlsxDrawingGraph(parts: Readonly<Record<string, Uint8Array>>, path: string, worksheet: Element,
|
|
59
|
+
sheetId: string, sheets: ReadonlyMap<string, string>, hyperlinkRelations: ReadonlySet<string> | undefined,
|
|
60
|
+
image: XlsxImageAssetReader, theme: XlsxWorksheetShapeTheme, register: Parameters<typeof readEditableXlsxPicture>[6],
|
|
61
|
+
layout: XlsxDrawingLayout): XlsxDrawingGraph | undefined {
|
|
62
|
+
try {
|
|
63
|
+
const reference = child(worksheet, "drawing");
|
|
64
|
+
if (!reference || reference.namespaceURI !== worksheet.namespaceURI || children(worksheet, "drawing").length !== 1) return;
|
|
65
|
+
const links = drawingPartRelationships(parts, path);
|
|
66
|
+
const drawingLink = chartLink(reference, links, "drawing");
|
|
67
|
+
if (!drawingLink || links.size - (hyperlinkRelations?.size ?? 0) !== 1) return;
|
|
68
|
+
const drawingPath = drawingLink.path;
|
|
69
|
+
const drawing = rootOf(parsePart(parts[drawingPath], drawingPath), drawingNamespace, "wsDr");
|
|
70
|
+
const relations = drawingPartRelationships(parts, drawingPath);
|
|
71
|
+
const anchors = children(drawing);
|
|
72
|
+
if (!anchors.length || anchors.length > 512 || !hasKnownAttributes(drawing, [])) return;
|
|
73
|
+
|
|
74
|
+
const result: XlsxChartPart[] = [];
|
|
75
|
+
const pictures: XlsxPicturePart[] = [];
|
|
76
|
+
const shapes: XlsxShapePart[] = [];
|
|
77
|
+
const connectors: { readonly node: Element; readonly object: Element; readonly order: number }[] = [];
|
|
78
|
+
const unique = new Set<string | number>();
|
|
79
|
+
const usedRelations = new Set<string>();
|
|
80
|
+
const mixed = anchors.some(node => child(node, "pic") || child(node, "sp") || child(node, "cxnSp"));
|
|
81
|
+
/** @param part - Shape candidate in the native part. @returns Whether its identity is safe to edit. */
|
|
82
|
+
function addShape(part: XlsxShapePart | undefined): boolean {
|
|
83
|
+
if (!part || unique.has(part.frameId) || unique.has(part.shape.id)) return false;
|
|
84
|
+
unique.add(part.shape.id);
|
|
85
|
+
unique.add(part.frameId);
|
|
86
|
+
shapes.push(part);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
for (const [order, node] of anchors.entries()) {
|
|
90
|
+
if (node.localName !== "twoCellAnchor" || node.namespaceURI !== drawingNamespace || !hasKnownAttributes(node, ["editAs"])
|
|
91
|
+
|| !["", "twoCell"].includes(node.getAttribute("editAs") ?? "") || !hasKnownChildren(node, ["from", "to", "graphicFrame", "pic", "sp", "cxnSp", "clientData"])) return;
|
|
92
|
+
const from = chartMarker(child(node, "from")), to = chartMarker(child(node, "to"));
|
|
93
|
+
const client = child(node, "clientData");
|
|
94
|
+
if (!from || !to || !client || !hasKnownAttributes(client, []) || !hasKnownChildren(client, [])) return;
|
|
95
|
+
const pic = child(node, "pic"), shape = child(node, "sp"), connector = child(node, "cxnSp"), chartFrame = child(node, "graphicFrame");
|
|
96
|
+
if ([pic, shape, connector, chartFrame].filter(Boolean).length !== 1) return;
|
|
97
|
+
if (connector) {
|
|
98
|
+
connectors.push({ node, object: connector, order });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (shape) {
|
|
102
|
+
if (!addShape(readWorksheetShape(node, shape, sheetId, order, theme, layout))) return;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (pic) {
|
|
106
|
+
const part = readEditableXlsxPicture(node, pic, sheetId, order, relations, image, register, layout);
|
|
107
|
+
if (!part || unique.has(part.frameId) || unique.has(part.picture.id)) return;
|
|
108
|
+
unique.add(part.frameId);
|
|
109
|
+
unique.add(part.picture.id);
|
|
110
|
+
usedRelations.add(part.relationshipId);
|
|
111
|
+
pictures.push(part);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const frame = child(node, "graphicFrame"), nonvisual = frame && child(frame, "nvGraphicFramePr"), props = nonvisual && child(nonvisual, "cNvPr");
|
|
115
|
+
const xfrm = frame && child(frame, "xfrm");
|
|
116
|
+
const frameProps = nonvisual && child(nonvisual, "cNvGraphicFramePr");
|
|
117
|
+
const graphic = frame && child(frame, "graphic"), data = graphic && child(graphic, "graphicData"), target = data && child(data, "chart");
|
|
118
|
+
if (!frame || !nonvisual || !props || !xfrm || !frameProps || !graphic || !data || !target
|
|
119
|
+
|| !hasKnownAttributes(frame, ["macro"]) || frame.getAttribute("macro")
|
|
120
|
+
|| !hasKnownChildren(frame, ["nvGraphicFramePr", "xfrm", "graphic"], drawingNamespace, { graphic: drawingMainNamespace })
|
|
121
|
+
|| !hasKnownAttributes(nonvisual, []) || !hasKnownChildren(nonvisual, ["cNvPr", "cNvGraphicFramePr"])
|
|
122
|
+
|| !hasKnownAttributes(frameProps, []) || !hasKnownChildren(frameProps, []) || !isAnchorTransform(xfrm)
|
|
123
|
+
|| !hasKnownAttributes(graphic, []) || !hasKnownChildren(graphic, ["graphicData"])
|
|
124
|
+
|| data.getAttribute("uri") !== chartNamespace || !hasKnownAttributes(data, ["uri"]) || !hasKnownChildren(data, ["chart"], chartNamespace)) return;
|
|
125
|
+
const frameId = readIndex(props, "id"), frameName = props.getAttribute("name") ?? "";
|
|
126
|
+
const id = drawingCreationId(props, ["id", "name"]), link = chartLink(target, relations, "chart");
|
|
127
|
+
if (!frameId || id === null || !link || unique.has(link.path) || unique.has(frameId)
|
|
128
|
+
|| (id !== undefined && unique.has(id)) || drawingPartRelationships(parts, link.path).size) return;
|
|
129
|
+
const space = rootOf(parsePart(parts[link.path], link.path), chartNamespace, "chartSpace");
|
|
130
|
+
const chart = readXlsxChartDefinition(space, id ?? uuidv4(), { sheetId, from, to }, sheets);
|
|
131
|
+
if (!chart) return;
|
|
132
|
+
unique.add(link.path);
|
|
133
|
+
unique.add(frameId);
|
|
134
|
+
unique.add(chart.id);
|
|
135
|
+
usedRelations.add(link.id);
|
|
136
|
+
result.push({ chart: mixed ? { ...chart, order } : chart, path: link.path, frameId, frameName, relationshipId: link.id });
|
|
137
|
+
}
|
|
138
|
+
if (relations.size !== usedRelations.size) return;
|
|
139
|
+
// Native references address the whole drawing part, including shapes painted after the line.
|
|
140
|
+
const targets = new Map(shapes.map(({ shape, frameId }) => [frameId, shape.id]));
|
|
141
|
+
for (const { node, object, order } of connectors) {
|
|
142
|
+
if (!addShape(readWorksheetShape(node, object, sheetId, order, theme, layout, false, targets))) return;
|
|
143
|
+
}
|
|
144
|
+
assertWorkbookShapeConnectionGraph(new Map(shapes.map(({ shape }) => [shape.id, shape])));
|
|
145
|
+
shapes.sort((a, b) => a.shape.order - b.shape.order);
|
|
146
|
+
return { worksheetPath: path, drawingPath, relationshipId: drawingLink.id, parts: result, pictures, shapes };
|
|
147
|
+
} catch { /* Preserve unknown and unreadable relationships in the downloaded original. */ }
|
|
148
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { validate as validateUuid } from "uuid";
|
|
3
|
+
import { hasKnownAttributes, hasKnownChildren } from "../opc/shape";
|
|
4
|
+
import { child } from "../opc/xml";
|
|
5
|
+
import { creationIdExtension, creationIdNamespace, drawingMainNamespace } from "./namespaces";
|
|
6
|
+
|
|
7
|
+
/** @param props - Nonvisual object metadata. @param attributes - Attributes represented by the workbook.
|
|
8
|
+
* @returns Its portable creation ID, undefined for native objects without one, or null if data would be lost. */
|
|
9
|
+
export function drawingCreationId(props: Element, attributes: readonly string[]): string | undefined | null {
|
|
10
|
+
if (!hasKnownAttributes(props, attributes) || !hasKnownChildren(props, ["extLst"], drawingMainNamespace)) return null;
|
|
11
|
+
const list = child(props, "extLst");
|
|
12
|
+
if (!list) return undefined;
|
|
13
|
+
const ext = child(list, "ext");
|
|
14
|
+
const value = ext && child(ext, "creationId");
|
|
15
|
+
const text = value?.getAttribute("id")?.replace(/^\{|\}$/gu, "");
|
|
16
|
+
if (!ext || !value || ext.getAttribute("uri") !== creationIdExtension
|
|
17
|
+
|| !hasKnownChildren(list, ["ext"]) || !hasKnownAttributes(list, [])
|
|
18
|
+
|| !hasKnownChildren(ext, ["creationId"], creationIdNamespace) || !hasKnownAttributes(ext, ["uri"])
|
|
19
|
+
|| !hasKnownChildren(value, []) || !hasKnownAttributes(value, ["id"])
|
|
20
|
+
|| !text || !validateUuid(text)) return null;
|
|
21
|
+
return text.toLowerCase();
|
|
22
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { Element } from "@xmldom/xmldom";
|
|
2
|
+
import { createWorkbookImageAsset, maximumWorkbookCharts, maximumWorkbookPictures, maximumWorkbookShapes, type WorkbookFloatingShape, type WorkbookCellImageAsset, type WorkbookRasterAsset, type WorkbookSheetAppearance } from "@quirekit/workbook";
|
|
3
|
+
import { createXlsxImageAssetReader } from "../image/raster";
|
|
4
|
+
import { readRelationships } from "../opc/relationships";
|
|
5
|
+
import { child, children, contentTypeNs, parsePart, parseWorksheetPart, rootOf } from "../opc/xml";
|
|
6
|
+
import type { XlsxChart, XlsxFloatingPicture } from "../types";
|
|
7
|
+
import { readXlsxDrawingGraph } from "./graph";
|
|
8
|
+
import { drawingContentTypes } from "./namespaces";
|
|
9
|
+
import { createXlsxDrawingLayout } from "./placement/axes";
|
|
10
|
+
import { readSheetDrawings } from "./read";
|
|
11
|
+
import type { XlsxWorksheetShapeTheme } from "./shapes/types";
|
|
12
|
+
import type { XlsxDrawingLayout } from "./placement/types";
|
|
13
|
+
import type { XlsxDrawingGraph } from "./types";
|
|
14
|
+
|
|
15
|
+
/** @param archive - Original package. @param graphs - Potentially writable graphs. @returns Whether every part has one owner. */
|
|
16
|
+
function exclusivelyOwned(archive: Readonly<Record<string, Uint8Array>>, graphs: ReadonlyMap<string, XlsxDrawingGraph>): boolean {
|
|
17
|
+
const owners = new Map<string, string>();
|
|
18
|
+
const chartIds = new Set<string>();
|
|
19
|
+
const pictureIds = new Set<string>();
|
|
20
|
+
const shapeIds = new Set<string>();
|
|
21
|
+
const drawingIds = new Set<string>();
|
|
22
|
+
const paths = new Set<string>();
|
|
23
|
+
try {
|
|
24
|
+
for (const graph of graphs.values()) {
|
|
25
|
+
if (owners.has(graph.drawingPath)) return false;
|
|
26
|
+
owners.set(graph.drawingPath, graph.worksheetPath);
|
|
27
|
+
for (const { picture } of graph.pictures) {
|
|
28
|
+
if (drawingIds.has(picture.id)) return false;
|
|
29
|
+
pictureIds.add(picture.id);
|
|
30
|
+
drawingIds.add(picture.id);
|
|
31
|
+
}
|
|
32
|
+
for (const { shape } of graph.shapes) {
|
|
33
|
+
if (drawingIds.has(shape.id)) return false;
|
|
34
|
+
drawingIds.add(shape.id);
|
|
35
|
+
shapeIds.add(shape.id);
|
|
36
|
+
}
|
|
37
|
+
for (const { chart, path } of graph.parts) {
|
|
38
|
+
if (owners.has(path) || drawingIds.has(chart.id)) return false;
|
|
39
|
+
drawingIds.add(chart.id);
|
|
40
|
+
owners.set(path, graph.drawingPath);
|
|
41
|
+
chartIds.add(chart.id);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!owners.size) return true;
|
|
45
|
+
if (chartIds.size > maximumWorkbookCharts || pictureIds.size > maximumWorkbookPictures || shapeIds.size > maximumWorkbookShapes) return false;
|
|
46
|
+
const manifest = rootOf(parsePart(archive["[Content_Types].xml"], "[Content_Types].xml"), contentTypeNs, "Types");
|
|
47
|
+
const types = new Map(children(manifest, "Override").map(node => [node.getAttribute("PartName"), node.getAttribute("ContentType")]));
|
|
48
|
+
for (const graph of graphs.values()) {
|
|
49
|
+
if (types.get("/" + graph.drawingPath) !== drawingContentTypes.drawing
|
|
50
|
+
|| graph.parts.some(part => types.get("/" + part.path) !== drawingContentTypes.chart)) return false;
|
|
51
|
+
}
|
|
52
|
+
for (const [part, bytes] of Object.entries(archive)) {
|
|
53
|
+
const link = /^(.*)_rels\/([^/]*)\.rels$/u.exec(part);
|
|
54
|
+
if (!link) continue;
|
|
55
|
+
const owner = link[1]! + link[2]!;
|
|
56
|
+
for (const relationship of readRelationships(parsePart(bytes, part), owner).values()) {
|
|
57
|
+
if (!relationship.path || !owners.has(relationship.path)) continue;
|
|
58
|
+
if (owners.get(relationship.path) !== owner || paths.has(relationship.path)) return false;
|
|
59
|
+
paths.add(relationship.path);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return paths.size === owners.size;
|
|
63
|
+
} catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @param archive - Retained XLSX members. @param theme - Workbook palette. @param sheets - IDs by lowercased native name.
|
|
69
|
+
* @param shapeTheme - The original drawing format scheme.
|
|
70
|
+
* @returns Separation between editable graphs and cached drawings kept in the original. */
|
|
71
|
+
export function createXlsxDrawingImport(
|
|
72
|
+
archive: Readonly<Record<string, Uint8Array>>, theme: readonly string[], sheets: ReadonlyMap<string, string>, shapeTheme: XlsxWorksheetShapeTheme
|
|
73
|
+
) {
|
|
74
|
+
const drawingGraphs = new Map<string, XlsxDrawingGraph>();
|
|
75
|
+
const layouts = new Map<string, XlsxDrawingLayout>();
|
|
76
|
+
const previews: XlsxChart[] = [];
|
|
77
|
+
const pictures: XlsxFloatingPicture[] = [];
|
|
78
|
+
const shapes: WorkbookFloatingShape[] = [];
|
|
79
|
+
const image = createXlsxImageAssetReader(archive);
|
|
80
|
+
const assets = new Map<string, WorkbookRasterAsset>();
|
|
81
|
+
|
|
82
|
+
/** @param path - Internal media part. @param raster - Validated raster. @returns Resource ID for all matching objects. */
|
|
83
|
+
function register(path: string, raster: WorkbookCellImageAsset): string {
|
|
84
|
+
let asset = assets.get(path);
|
|
85
|
+
if (!asset) {
|
|
86
|
+
// Each room participant reads the same document in worksheet order before applying shared edits.
|
|
87
|
+
asset = createWorkbookImageAsset("xlsx-drawing-image-" + (assets.size + 1), raster.bytes());
|
|
88
|
+
assets.set(path, asset);
|
|
89
|
+
}
|
|
90
|
+
return asset.id;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @param path - Worksheet part. @param worksheet - Sheet XML. @param sheetId - Workbook sheet identity. */
|
|
94
|
+
function addPreviews(path: string, worksheet: Element, sheetId: string): void {
|
|
95
|
+
const source = readSheetDrawings(archive, path, worksheet, sheetId, theme, sheets, image, shapeTheme, layouts.get(sheetId)!);
|
|
96
|
+
for (const chart of source.charts) previews.push(chart);
|
|
97
|
+
for (const picture of source.pictures) pictures.push(picture);
|
|
98
|
+
for (const shape of source.shapes) shapes.push(shape);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
/** @param path - Native sheet part. @param worksheet - Parsed worksheet. @param sheetId - Stable session ID.
|
|
103
|
+
* @param appearance - Native row and column layout. @param hyperlinkRelations - Native text links managed by the worksheet codec.
|
|
104
|
+
* @returns Whether the drawing participates in live workbook editing. */
|
|
105
|
+
add(path: string, worksheet: Element, sheetId: string, appearance: WorkbookSheetAppearance, hyperlinkRelations?: ReadonlySet<string>): boolean {
|
|
106
|
+
if (!child(worksheet, "drawing")) return false;
|
|
107
|
+
const layout = createXlsxDrawingLayout(appearance);
|
|
108
|
+
layouts.set(sheetId, layout);
|
|
109
|
+
const graph = readXlsxDrawingGraph(archive, path, worksheet, sheetId, sheets, hyperlinkRelations, image, shapeTheme, register, layout);
|
|
110
|
+
if (graph) drawingGraphs.set(sheetId, graph);
|
|
111
|
+
else addPreviews(path, worksheet, sheetId);
|
|
112
|
+
return Boolean(graph);
|
|
113
|
+
},
|
|
114
|
+
/** @param restrictions - Reasons the workbook must keep its original parts. @returns Final drawing import state. */
|
|
115
|
+
finish(restrictions: Set<string>) {
|
|
116
|
+
if (!exclusivelyOwned(archive, drawingGraphs)) {
|
|
117
|
+
restrictions.add("绘图关系共用部件或对象数量超限");
|
|
118
|
+
for (const [sheetId, graph] of drawingGraphs) {
|
|
119
|
+
const path = graph.worksheetPath;
|
|
120
|
+
const worksheet = parseWorksheetPart(archive[path], path);
|
|
121
|
+
addPreviews(path, worksheet, sheetId);
|
|
122
|
+
}
|
|
123
|
+
drawingGraphs.clear();
|
|
124
|
+
}
|
|
125
|
+
const mapped = [...drawingGraphs.values()];
|
|
126
|
+
const editable = mapped.flatMap(graph => graph.pictures);
|
|
127
|
+
return { charts: mapped.flatMap(graph => graph.parts.map(part => part.chart)), drawingGraphs, previews, pictures, shapes,
|
|
128
|
+
editableShapes: mapped.flatMap(graph => graph.shapes.map(part => part.shape)),
|
|
129
|
+
editablePictures: editable.map(part => part.picture),
|
|
130
|
+
pictureAssets: [...new Set(editable.map(part => part.path))].map(path => assets.get(path)!) };
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** DrawingML namespaces and units shared by workbook import and export. */
|
|
2
|
+
export const drawingNamespace = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing";
|
|
3
|
+
export const drawingMainNamespace = "http://schemas.openxmlformats.org/drawingml/2006/main";
|
|
4
|
+
export const chartNamespace = "http://schemas.openxmlformats.org/drawingml/2006/chart";
|
|
5
|
+
export const creationIdNamespace = "http://schemas.microsoft.com/office/drawing/2014/main";
|
|
6
|
+
export const creationIdExtension = "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}";
|
|
7
|
+
export const emuPerCssPixel = 9525;
|
|
8
|
+
export const drawingContentTypes = {
|
|
9
|
+
drawing: "application/vnd.openxmlformats-officedocument.drawing+xml",
|
|
10
|
+
chart: "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
11
|
+
} as const;
|