@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.
Files changed (336) hide show
  1. package/LICENSE.md +21 -0
  2. package/THIRD_PARTY_NOTICES.md +150 -0
  3. package/dist/index.d.mts +122 -0
  4. package/dist/index.d.mts.map +1 -0
  5. package/dist/index.mjs +19167 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/licenses/saxes.LICENSE +64 -0
  8. package/licenses/xmlchars.LICENSE +18 -0
  9. package/package.json +52 -3
  10. package/src/bundle.ts +17 -0
  11. package/src/comments/legacy/read.ts +68 -0
  12. package/src/comments/legacy/write.ts +64 -0
  13. package/src/comments/package/ranges.ts +78 -0
  14. package/src/comments/package/read.ts +95 -0
  15. package/src/comments/package/roundtrip.test.ts +106 -0
  16. package/src/comments/package/write.ts +130 -0
  17. package/src/comments/thread/read.ts +69 -0
  18. package/src/comments/thread/write.ts +61 -0
  19. package/src/comments/types.ts +43 -0
  20. package/src/comments/xml.ts +50 -0
  21. package/src/create.ts +13 -0
  22. package/src/drawing/cache.ts +48 -0
  23. package/src/drawing/chart.test.ts +96 -0
  24. package/src/drawing/chart.ts +87 -0
  25. package/src/drawing/definition/plots.ts +76 -0
  26. package/src/drawing/definition/read.ts +65 -0
  27. package/src/drawing/definition/shape.ts +40 -0
  28. package/src/drawing/definition/types.ts +24 -0
  29. package/src/drawing/editable-picture.ts +80 -0
  30. package/src/drawing/graph.ts +148 -0
  31. package/src/drawing/identity.ts +22 -0
  32. package/src/drawing/import.ts +133 -0
  33. package/src/drawing/namespaces.ts +11 -0
  34. package/src/drawing/picture.test.ts +98 -0
  35. package/src/drawing/picture.ts +54 -0
  36. package/src/drawing/placement/anchor.ts +71 -0
  37. package/src/drawing/placement/axes.ts +113 -0
  38. package/src/drawing/placement/marker.ts +16 -0
  39. package/src/drawing/placement/transform.ts +132 -0
  40. package/src/drawing/placement/types.ts +20 -0
  41. package/src/drawing/presentation/cartesian/axes.ts +103 -0
  42. package/src/drawing/presentation/cartesian/plot.ts +109 -0
  43. package/src/drawing/presentation/cartesian/types.ts +9 -0
  44. package/src/drawing/presentation/layout.ts +29 -0
  45. package/src/drawing/presentation/paint.ts +56 -0
  46. package/src/drawing/presentation/properties.ts +31 -0
  47. package/src/drawing/read.ts +85 -0
  48. package/src/drawing/shapes/connector.ts +55 -0
  49. package/src/drawing/shapes/paint.ts +108 -0
  50. package/src/drawing/shapes/read.ts +89 -0
  51. package/src/drawing/shapes/text.ts +89 -0
  52. package/src/drawing/shapes/types.ts +13 -0
  53. package/src/drawing/sources.test.ts +27 -0
  54. package/src/drawing/sources.ts +43 -0
  55. package/src/drawing/types.ts +54 -0
  56. package/src/drawing/worksheet-connector.test.ts +135 -0
  57. package/src/drawing/worksheet-shape.test.ts +61 -0
  58. package/src/drawing/write/anchor.ts +17 -0
  59. package/src/drawing/write/axes.ts +23 -0
  60. package/src/drawing/write/chart.ts +56 -0
  61. package/src/drawing/write/drawing.ts +61 -0
  62. package/src/drawing/write/pictures.ts +68 -0
  63. package/src/drawing/write/series.ts +43 -0
  64. package/src/drawing/write/shapes.ts +113 -0
  65. package/src/drawing/write/text.ts +26 -0
  66. package/src/drawing/write/workbook.test.ts +199 -0
  67. package/src/drawing/write/workbook.ts +217 -0
  68. package/src/external-links/formulas.ts +21 -0
  69. package/src/external-links/read.test.ts +93 -0
  70. package/src/external-links/read.ts +104 -0
  71. package/src/external-links/types.ts +9 -0
  72. package/src/external-links/write.ts +120 -0
  73. package/src/image/excel.ts +122 -0
  74. package/src/image/native.ts +21 -0
  75. package/src/image/ownership.ts +77 -0
  76. package/src/image/parts.ts +45 -0
  77. package/src/image/raster.ts +25 -0
  78. package/src/image/read.test.ts +95 -0
  79. package/src/image/read.ts +66 -0
  80. package/src/image/relationships.ts +26 -0
  81. package/src/image/types.ts +48 -0
  82. package/src/image/wps.ts +43 -0
  83. package/src/image/write.test.ts +53 -0
  84. package/src/image/write.ts +118 -0
  85. package/src/index.ts +6 -0
  86. package/src/metadata/dynamic-array.ts +105 -0
  87. package/src/metadata/package.ts +75 -0
  88. package/src/metadata/types.ts +7 -0
  89. package/src/opc/archive.ts +26 -0
  90. package/src/opc/ownership.ts +40 -0
  91. package/src/opc/relationships.ts +98 -0
  92. package/src/opc/revision.ts +28 -0
  93. package/src/opc/shape.ts +23 -0
  94. package/src/opc/types.ts +13 -0
  95. package/src/opc/xml.test.ts +49 -0
  96. package/src/opc/xml.ts +128 -0
  97. package/src/optimization/blanks.ts +53 -0
  98. package/src/optimization/optimize.test.ts +117 -0
  99. package/src/optimization/optimize.ts +41 -0
  100. package/src/optimization/package.ts +65 -0
  101. package/src/optimization/rules.ts +46 -0
  102. package/src/optimization/styles.ts +76 -0
  103. package/src/optimization/types.ts +33 -0
  104. package/src/package/assembly.test.ts +117 -0
  105. package/src/package/calculation-metadata.test.ts +85 -0
  106. package/src/package/calculation-metadata.ts +88 -0
  107. package/src/package/checkboxes.ts +37 -0
  108. package/src/package/cleanup.test.ts +61 -0
  109. package/src/package/dashboard.test.ts +53 -0
  110. package/src/package/dashboard.ts +84 -0
  111. package/src/package/extensions.ts +16 -0
  112. package/src/package/merge-recipes.ts +84 -0
  113. package/src/package/names/conditional.ts +64 -0
  114. package/src/package/names/defined.test.ts +52 -0
  115. package/src/package/names/defined.ts +75 -0
  116. package/src/package/names/filter.ts +68 -0
  117. package/src/package/names/print-layout.ts +62 -0
  118. package/src/package/names/print.ts +62 -0
  119. package/src/package/new.ts +46 -0
  120. package/src/package/open/appearance.ts +46 -0
  121. package/src/package/open/directory.ts +61 -0
  122. package/src/package/open/restrictions.ts +27 -0
  123. package/src/package/open/snapshot.test.ts +48 -0
  124. package/src/package/open/snapshot.ts +34 -0
  125. package/src/package/open/source-write.ts +130 -0
  126. package/src/package/open/types.ts +73 -0
  127. package/src/package/open/worksheets.ts +149 -0
  128. package/src/package/positions.ts +40 -0
  129. package/src/package/read.ts +143 -0
  130. package/src/package/save/types.ts +57 -0
  131. package/src/package/save/worksheets.ts +126 -0
  132. package/src/package/selection-copy.test.ts +70 -0
  133. package/src/package/styles.ts +39 -0
  134. package/src/package/types.ts +52 -0
  135. package/src/package/workbook-views.ts +34 -0
  136. package/src/package/worksheets.test.ts +190 -0
  137. package/src/package/worksheets.ts +162 -0
  138. package/src/package/write.ts +175 -0
  139. package/src/pivot/cache/calculated.ts +34 -0
  140. package/src/pivot/cache/dates.ts +124 -0
  141. package/src/pivot/cache/groups.ts +11 -0
  142. package/src/pivot/cache/members.ts +99 -0
  143. package/src/pivot/cache/numbers.ts +63 -0
  144. package/src/pivot/cache/read.ts +131 -0
  145. package/src/pivot/cache/time.ts +20 -0
  146. package/src/pivot/cache/values.ts +43 -0
  147. package/src/pivot/cache/write.ts +117 -0
  148. package/src/pivot/definition/axes.ts +35 -0
  149. package/src/pivot/definition/dates.ts +87 -0
  150. package/src/pivot/definition/display.ts +86 -0
  151. package/src/pivot/definition/extension.ts +57 -0
  152. package/src/pivot/definition/field-settings/caption.ts +38 -0
  153. package/src/pivot/definition/field-settings/comparison.ts +47 -0
  154. package/src/pivot/definition/field-settings/field.ts +66 -0
  155. package/src/pivot/definition/field-settings/filters.ts +86 -0
  156. package/src/pivot/definition/field-settings/measure.ts +79 -0
  157. package/src/pivot/definition/field-settings/types.ts +19 -0
  158. package/src/pivot/definition/functions.ts +21 -0
  159. package/src/pivot/definition/items.ts +144 -0
  160. package/src/pivot/definition/members.ts +98 -0
  161. package/src/pivot/definition/numbers.ts +19 -0
  162. package/src/pivot/definition/presentation.ts +52 -0
  163. package/src/pivot/definition/read.ts +143 -0
  164. package/src/pivot/definition/report-layout.ts +39 -0
  165. package/src/pivot/definition/write.ts +157 -0
  166. package/src/pivot/package/calculated.test.ts +103 -0
  167. package/src/pivot/package/dates.test.ts +266 -0
  168. package/src/pivot/package/display.test.ts +133 -0
  169. package/src/pivot/package/field-settings.test.ts +59 -0
  170. package/src/pivot/package/layout.test.ts +148 -0
  171. package/src/pivot/package/measure-settings.test.ts +85 -0
  172. package/src/pivot/package/members.test.ts +167 -0
  173. package/src/pivot/package/numbers.test.ts +120 -0
  174. package/src/pivot/package/read.ts +93 -0
  175. package/src/pivot/package/roundtrip.test.ts +244 -0
  176. package/src/pivot/package/write.ts +103 -0
  177. package/src/pivot/types.ts +36 -0
  178. package/src/pivot/xml.ts +50 -0
  179. package/src/presentation.test.ts +186 -0
  180. package/src/roundtrip.test.ts +72 -0
  181. package/src/styles/checkbox/bags.ts +85 -0
  182. package/src/styles/checkbox/style.ts +45 -0
  183. package/src/styles/checkbox/types.ts +7 -0
  184. package/src/styles/color.ts +64 -0
  185. package/src/styles/differential-preview.ts +60 -0
  186. package/src/styles/differential.ts +119 -0
  187. package/src/styles/numbers.ts +9 -0
  188. package/src/styles/parts.ts +107 -0
  189. package/src/styles/read.ts +95 -0
  190. package/src/styles/reference.ts +14 -0
  191. package/src/styles/types.ts +9 -0
  192. package/src/styles/write.test.ts +88 -0
  193. package/src/styles/write.ts +162 -0
  194. package/src/table/conversion.test.ts +73 -0
  195. package/src/table/definition.ts +89 -0
  196. package/src/table/filter.ts +42 -0
  197. package/src/table/ownership.ts +112 -0
  198. package/src/table/read.test.ts +274 -0
  199. package/src/table/read.ts +117 -0
  200. package/src/table/remove.ts +55 -0
  201. package/src/table/sort.test.ts +79 -0
  202. package/src/table/styles/builtin.ts +40 -0
  203. package/src/table/styles/differential.ts +55 -0
  204. package/src/table/styles/preset-data.ts +17 -0
  205. package/src/table/styles/presets/dark.ts +104 -0
  206. package/src/table/styles/presets/light-grid.ts +68 -0
  207. package/src/table/styles/presets/light.ts +145 -0
  208. package/src/table/styles/presets/medium-edges.ts +131 -0
  209. package/src/table/styles/presets/medium.ts +131 -0
  210. package/src/table/styles/read.ts +70 -0
  211. package/src/table/styles/types.ts +27 -0
  212. package/src/table/totals.test.ts +77 -0
  213. package/src/table/totals.ts +79 -0
  214. package/src/table/types.ts +21 -0
  215. package/src/table/write.ts +189 -0
  216. package/src/types.ts +74 -0
  217. package/src/value/date.ts +21 -0
  218. package/src/worksheet/appearance.ts +55 -0
  219. package/src/worksheet/axis.test.ts +185 -0
  220. package/src/worksheet/axis.ts +131 -0
  221. package/src/worksheet/background/read.ts +41 -0
  222. package/src/worksheet/background/roundtrip.test.ts +77 -0
  223. package/src/worksheet/background/write.ts +96 -0
  224. package/src/worksheet/cell-shift.ts +44 -0
  225. package/src/worksheet/cells.ts +75 -0
  226. package/src/worksheet/checkbox/formats.ts +53 -0
  227. package/src/worksheet/checkbox/ranges.ts +112 -0
  228. package/src/worksheet/checkbox/write.test.ts +62 -0
  229. package/src/worksheet/conditional/color-scale.ts +61 -0
  230. package/src/worksheet/conditional/data-bar.ts +76 -0
  231. package/src/worksheet/conditional/extensions.ts +95 -0
  232. package/src/worksheet/conditional/icon-reference.ts +69 -0
  233. package/src/worksheet/conditional/icon-set.test.ts +60 -0
  234. package/src/worksheet/conditional/icon-set.ts +79 -0
  235. package/src/worksheet/conditional/preview.ts +44 -0
  236. package/src/worksheet/conditional/read.ts +185 -0
  237. package/src/worksheet/conditional/text.test.ts +71 -0
  238. package/src/worksheet/conditional/text.ts +122 -0
  239. package/src/worksheet/conditional/threshold.ts +47 -0
  240. package/src/worksheet/conditional/types.ts +20 -0
  241. package/src/worksheet/conditional/write-bar.ts +66 -0
  242. package/src/worksheet/conditional/write.test.ts +218 -0
  243. package/src/worksheet/conditional/write.ts +103 -0
  244. package/src/worksheet/copied-axis.ts +70 -0
  245. package/src/worksheet/data/rows.ts +59 -0
  246. package/src/worksheet/data/source.test.ts +68 -0
  247. package/src/worksheet/data/source.ts +31 -0
  248. package/src/worksheet/data/types.ts +9 -0
  249. package/src/worksheet/data.test.ts +314 -0
  250. package/src/worksheet/dimensions/columns.ts +87 -0
  251. package/src/worksheet/dimensions/write.test.ts +110 -0
  252. package/src/worksheet/dimensions/write.ts +36 -0
  253. package/src/worksheet/filters/advanced.test.ts +57 -0
  254. package/src/worksheet/filters/color.ts +105 -0
  255. package/src/worksheet/filters/icon.test.ts +83 -0
  256. package/src/worksheet/filters/icon.ts +39 -0
  257. package/src/worksheet/filters/read.ts +114 -0
  258. package/src/worksheet/filters/relative-dates.ts +47 -0
  259. package/src/worksheet/filters/restore-icons.ts +33 -0
  260. package/src/worksheet/filters/restore.ts +169 -0
  261. package/src/worksheet/filters/rules.ts +23 -0
  262. package/src/worksheet/filters/types.ts +23 -0
  263. package/src/worksheet/filters/visibility.ts +188 -0
  264. package/src/worksheet/filters/write.ts +121 -0
  265. package/src/worksheet/formula/cache.ts +29 -0
  266. package/src/worksheet/formula/external-results.test.ts +39 -0
  267. package/src/worksheet/formula/functions.ts +29 -0
  268. package/src/worksheet/formula/prefix.ts +27 -0
  269. package/src/worksheet/formula/shared/read.test.ts +121 -0
  270. package/src/worksheet/formula/shared/read.ts +94 -0
  271. package/src/worksheet/formula/shared/references.ts +100 -0
  272. package/src/worksheet/formula/shared/types.ts +21 -0
  273. package/src/worksheet/formula/spill-read.ts +54 -0
  274. package/src/worksheet/formula/spill-write.ts +32 -0
  275. package/src/worksheet/formula/spill.test.ts +101 -0
  276. package/src/worksheet/formula/write.test.ts +26 -0
  277. package/src/worksheet/hyperlinks/location.ts +59 -0
  278. package/src/worksheet/hyperlinks/model.ts +22 -0
  279. package/src/worksheet/hyperlinks/read.ts +70 -0
  280. package/src/worksheet/hyperlinks/types.ts +21 -0
  281. package/src/worksheet/hyperlinks/write.test.ts +203 -0
  282. package/src/worksheet/hyperlinks/write.ts +51 -0
  283. package/src/worksheet/merge.test.ts +58 -0
  284. package/src/worksheet/merge.ts +61 -0
  285. package/src/worksheet/outline/columns.ts +46 -0
  286. package/src/worksheet/outline/metadata.ts +68 -0
  287. package/src/worksheet/outline/native.ts +81 -0
  288. package/src/worksheet/outline/read.ts +50 -0
  289. package/src/worksheet/outline/roundtrip.test.ts +73 -0
  290. package/src/worksheet/outline/types.ts +37 -0
  291. package/src/worksheet/outline/write.ts +110 -0
  292. package/src/worksheet/positions.test.ts +46 -0
  293. package/src/worksheet/positions.ts +48 -0
  294. package/src/worksheet/print/breaks.ts +72 -0
  295. package/src/worksheet/print/elements.ts +16 -0
  296. package/src/worksheet/print/page-text.ts +139 -0
  297. package/src/worksheet/print/setup.test.ts +141 -0
  298. package/src/worksheet/print/setup.ts +116 -0
  299. package/src/worksheet/progress/read.ts +73 -0
  300. package/src/worksheet/progress/solid.ts +26 -0
  301. package/src/worksheet/progress/types.ts +11 -0
  302. package/src/worksheet/progress/write.test.ts +63 -0
  303. package/src/worksheet/progress/write.ts +46 -0
  304. package/src/worksheet/read.ts +197 -0
  305. package/src/worksheet/rich-text.test.ts +41 -0
  306. package/src/worksheet/rich-text.ts +76 -0
  307. package/src/worksheet/sort/read.ts +115 -0
  308. package/src/worksheet/sort/relocate.ts +62 -0
  309. package/src/worksheet/sort/roundtrip.test.ts +116 -0
  310. package/src/worksheet/sort/types.ts +11 -0
  311. package/src/worksheet/sort/write.ts +77 -0
  312. package/src/worksheet/template.ts +9 -0
  313. package/src/worksheet/text.ts +33 -0
  314. package/src/worksheet/validation/extended.ts +36 -0
  315. package/src/worksheet/validation/image.ts +7 -0
  316. package/src/worksheet/validation/list-options/portable.ts +90 -0
  317. package/src/worksheet/validation/list-options/types.ts +14 -0
  318. package/src/worksheet/validation/list-options/wps.ts +51 -0
  319. package/src/worksheet/validation/read.ts +162 -0
  320. package/src/worksheet/validation/text.ts +13 -0
  321. package/src/worksheet/validation/write.test.ts +259 -0
  322. package/src/worksheet/validation/write.ts +99 -0
  323. package/src/worksheet/view/coordinates.ts +39 -0
  324. package/src/worksheet/view/named/read.ts +116 -0
  325. package/src/worksheet/view/named/roundtrip.test.ts +136 -0
  326. package/src/worksheet/view/named/sort.ts +57 -0
  327. package/src/worksheet/view/named/types.ts +15 -0
  328. package/src/worksheet/view/named/write.ts +107 -0
  329. package/src/worksheet/view/named/xml.ts +33 -0
  330. package/src/worksheet/view/panes.ts +75 -0
  331. package/src/worksheet/view/read.ts +61 -0
  332. package/src/worksheet/view/relocate.ts +57 -0
  333. package/src/worksheet/view/write.test.ts +91 -0
  334. package/src/worksheet/view/write.ts +57 -0
  335. package/src/worksheet/write.ts +86 -0
  336. package/README.md +0 -3
@@ -0,0 +1,77 @@
1
+ import type { Document, Element } from "@xmldom/xmldom";
2
+ import { copyWorkbookWorksheetSorts, formatWorkbookRange, sameWorkbookSortState, workbookSortKeyRange,
3
+ type WorkbookRange, type WorkbookSheet, type WorkbookSortState } from "@quirekit/workbook";
4
+ import { child, children, rootOf, spreadNs } from "../../opc/xml";
5
+ import { xlsxColorDifferential } from "../../styles/differential";
6
+ import type { XlsxDifferentialStyle } from "../../styles/types";
7
+ import { makeXlsxIconReference } from "../conditional/icon-reference";
8
+ import { worksheetAfterAutoFilter } from "../filters/write";
9
+ import { encodeXlsxText } from "../text";
10
+
11
+ /** @param document - Current worksheet XML. @param previous - Original sheet that owned this part.
12
+ * @param sheet - Successor checkpoint. @param filterRebuilt - Whether this write replaced the ordinary AutoFilter.
13
+ * @param addStyle - Registers the color used by a sort key. */
14
+ export function writeXlsxWorksheetSorts(document: Document, previous: WorkbookSheet | undefined, sheet: WorkbookSheet,
15
+ filterRebuilt: boolean, addStyle: (style: XlsxDifferentialStyle) => number): void {
16
+ if (!previous?.sorts && !sheet.sorts) return;
17
+ const worksheet = rootOf(document, spreadNs, "worksheet");
18
+ const next = copyWorkbookWorksheetSorts(sheet.sorts, sheet.id, sheet.autoFilter?.range);
19
+ const earlier = previous?.sorts;
20
+ if (!sameWorkbookSortState(earlier?.worksheet, next?.worksheet)) {
21
+ const anchor = children(worksheet).find(node => node.localName !== "sortState" && worksheetAfterAutoFilter.has(node.localName ?? "")) ?? null;
22
+ writeXlsxOwnerSort(document, worksheet, earlier?.worksheet, next?.worksheet, anchor, addStyle);
23
+ }
24
+ const filter = child(worksheet, "autoFilter");
25
+ if (filter && (filterRebuilt || !sameWorkbookSortState(earlier?.filter, next?.filter))) {
26
+ writeXlsxOwnerSort(document, filter, earlier?.filter, next?.filter, child(filter, "extLst") ?? null, addStyle);
27
+ }
28
+ }
29
+
30
+ /** @param document - Mutable SpreadsheetML part. @param owner - Worksheet, table or filter.
31
+ * @param previous - Known original definition. @param next - Criteria to write, if any.
32
+ * @param anchor - Following child in the SpreadsheetML sequence. @param addStyle - Shared differential palette. */
33
+ export function writeXlsxOwnerSort(document: Document, owner: Element, previous: WorkbookSortState | undefined, next: WorkbookSortState | undefined,
34
+ anchor: Element | null, addStyle: (style: XlsxDifferentialStyle) => number): void {
35
+ if (!previous && !next) return;
36
+ const existing = child(owner, "sortState");
37
+ if (existing && !previous) throw new Error("无法替换尚未支持的原生排序条件。");
38
+ if (!next) {
39
+ if (existing) owner.removeChild(existing);
40
+ return;
41
+ }
42
+ const state = document.createElementNS(spreadNs, "sortState");
43
+ state.setAttribute("ref", formatWorkbookRange(next.range));
44
+ if (next.axis === "column") state.setAttribute("columnSort", "1");
45
+ if (next.caseSensitive) state.setAttribute("caseSensitive", "1");
46
+ if (next.sortMethod) state.setAttribute("sortMethod", next.sortMethod);
47
+ for (const key of next.keys) {
48
+ const native = "icon" in key ? makeXlsxIconReference(document, "sortCondition", key.icon) : undefined;
49
+ const condition = native?.leaf ?? document.createElementNS(spreadNs, "sortCondition");
50
+ writeXlsxSortCondition(condition, next.range, key, addStyle);
51
+ state.appendChild(native?.content ?? condition);
52
+ }
53
+ if (existing) owner.replaceChild(state, existing);
54
+ else owner.insertBefore(state, anchor);
55
+ }
56
+
57
+ /** @param condition - Native sort condition in the owner's namespace. @param range - Sortable records.
58
+ * @param key - Value, color or icon priority. @param addStyle - Color style registration for this owner. */
59
+ export function writeXlsxSortCondition(condition: Element, range: WorkbookRange, key: WorkbookSortState["keys"][number],
60
+ addStyle: (style: XlsxDifferentialStyle) => number): void {
61
+ condition.setAttribute("ref", formatWorkbookRange(workbookSortKeyRange(range, key)));
62
+ if ("icon" in key) {
63
+ condition.setAttribute("sortBy", "icon");
64
+ if (key.icon) {
65
+ condition.setAttribute("iconSet", key.icon.set);
66
+ condition.setAttribute("iconId", String(key.icon.index));
67
+ }
68
+ if (key.position === "last") condition.setAttribute("descending", "1");
69
+ } else if ("source" in key) {
70
+ condition.setAttribute("sortBy", key.source === "fill" ? "cellColor" : "fontColor");
71
+ condition.setAttribute("dxfId", String(addStyle(xlsxColorDifferential(key.source, key.color))));
72
+ if (key.position === "last") condition.setAttribute("descending", "1");
73
+ } else {
74
+ if (key.direction === "desc") condition.setAttribute("descending", "1");
75
+ if (key.customList) condition.setAttribute("customList", encodeXlsxText(key.customList.join(",")));
76
+ }
77
+ }
@@ -0,0 +1,9 @@
1
+ import { defaultWorkbookDimensions } from "@quirekit/workbook";
2
+ import { strToU8 } from "fflate";
3
+ import { spreadNs } from "../opc/xml";
4
+
5
+ /** @returns An empty worksheet part with the same default dimensions as the workbench. */
6
+ export function createXlsxWorksheetPart(): Uint8Array {
7
+ const { column, row } = defaultWorkbookDimensions;
8
+ return strToU8(`<worksheet xmlns="${spreadNs}"><sheetFormatPr defaultColWidth="${column}" defaultRowHeight="${row}"/><sheetData/></worksheet>`);
9
+ }
@@ -0,0 +1,33 @@
1
+ import type { Document, Element } from "@xmldom/xmldom";
2
+ import { children, spreadNs } from "../opc/xml";
3
+
4
+ /**
5
+ * @param value - SpreadsheetML escaped string.
6
+ * @returns Workbook text with control characters restored.
7
+ */
8
+ export function decodeXlsxText(value: string): string {
9
+ return value.replace(/_x([0-9a-f]{4})_/gi, (_match, digits: string) => String.fromCharCode(Number.parseInt(digits, 16)));
10
+ }
11
+
12
+ /**
13
+ * @param value - Authored cell text.
14
+ * @returns Text for an XML string node, with Excel escape sequences kept distinct from literal text.
15
+ */
16
+ export function encodeXlsxText(value: string): string {
17
+ return value.replace(/_x[0-9a-f]{4}_/gi, match => "_x005F_" + match.slice(1)).replace(
18
+ /[\u0000-\u0008\u000b-\u001f\ufffe\uffff\p{Surrogate}]/gu,
19
+ character => `_x${character.charCodeAt(0).toString(16).padStart(4, "0").toUpperCase()}_`
20
+ );
21
+ }
22
+
23
+ /**
24
+ * @param document - Owning worksheet. @param parent - Element receiving text.
25
+ * @param name - Standard SpreadsheetML element. @param value - Already escaped body text.
26
+ */
27
+ export function insertXlsxText(document: Document, parent: Element, name: string, value: string): void {
28
+ const element = document.createElementNS(spreadNs, name);
29
+ element.appendChild(document.createTextNode(value));
30
+ if (name === "t" && /^\s|\s$/u.test(value)) element.setAttribute("xml:space", "preserve");
31
+ const extension = children(parent).find(item => item.localName === "extLst");
32
+ parent.insertBefore(element, extension ?? null);
33
+ }
@@ -0,0 +1,36 @@
1
+ import type { Element } from "@xmldom/xmldom";
2
+ import { hasKnownAttributes, hasKnownChildren } from "../../opc/shape";
3
+ import { child, children, spreadNs } from "../../opc/xml";
4
+
5
+ const validationUri = "{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}";
6
+ const spreadsheet2010 = "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main";
7
+ const spreadsheetFormula = "http://schemas.microsoft.com/office/excel/2006/main";
8
+
9
+ /** @param node - Worksheet extension. @returns Whether it identifies Office 2010 validation. */
10
+ export function isXlsxValidationExtension(node: Element): boolean {
11
+ return node.namespaceURI === spreadNs && node.localName === "ext" && node.getAttribute("uri")?.toUpperCase() === validationUri;
12
+ }
13
+
14
+ /** @param worksheet - Worksheet part. @returns Its Office 2010 validation extensions. */
15
+ export function findXlsxValidationExtensions(worksheet: Element): Element[] {
16
+ return children(worksheet, "extLst").flatMap(group => children(group).filter(isXlsxValidationExtension));
17
+ }
18
+
19
+ /** @param extension - Office 2010 validation extension. @returns Its validation container when the wrapper is understood. */
20
+ export function readXlsxValidationExtension(extension: Element): Element | undefined {
21
+ if (!hasKnownAttributes(extension, ["uri"]) || !hasKnownChildren(extension, ["dataValidations"], spreadsheet2010)) return;
22
+ return child(extension, "dataValidations");
23
+ }
24
+
25
+ /** @param entry - Candidate rule. @param attributes - Known unqualified rule attributes.
26
+ * @returns Formula and targets from an understood Office 2010 rule. */
27
+ export function readXlsxExtendedValidation(entry: Element, attributes: readonly string[]): { first: string; second: string; targets: string } | undefined {
28
+ if (entry.namespaceURI !== spreadsheet2010 || entry.localName !== "dataValidation" || !["list", "custom"].includes(entry.getAttribute("type") ?? "")
29
+ || !hasKnownAttributes(entry, attributes) || !hasKnownChildren(entry, ["formula1", "sqref"], spreadsheet2010, { sqref: spreadsheetFormula })) return;
30
+ const formula = child(entry, "formula1"), positions = child(entry, "sqref");
31
+ if (!formula || !positions || !hasKnownAttributes(formula, []) || !hasKnownChildren(formula, ["f"], spreadsheetFormula)) return;
32
+ const expression = child(formula, "f");
33
+ if (!expression || !hasKnownAttributes(expression, []) || !hasKnownChildren(expression, [])
34
+ || !hasKnownAttributes(positions, []) || !hasKnownChildren(positions, [])) return;
35
+ return { first: expression.textContent ?? "", second: "", targets: positions.textContent ?? "" };
36
+ }
@@ -0,0 +1,7 @@
1
+ import { formatWorkbookColumn, type WorkbookRange } from "@quirekit/workbook";
2
+
3
+ /** @param range - Region controlled by a local image column rule. @returns Native custom validation with the WPS image marker. */
4
+ export function xlsxImageValidationFormula(range: WorkbookRange): string {
5
+ const anchor = formatWorkbookColumn(range.startColumn) + (range.startRow + 1);
6
+ return 'IF(TRUE,LEFT(' + anchor + ',9)="=DISPIMG(","DispImg")';
7
+ }
@@ -0,0 +1,90 @@
1
+ import type { Element } from "@xmldom/xmldom";
2
+ import { hasKnownAttributes } from "../../../opc/shape";
3
+ import { child, children, readIndex, spreadNs } from "../../../opc/xml";
4
+ import type { XlsxListOptions, XlsxListOptionsRecord } from "./types";
5
+
6
+ const namespace = "urn:quirekit:spreadsheet:list-options:1";
7
+
8
+ /** @param element - Worksheet extension. @returns Whether this codec owns its envelope. */
9
+ export function isXlsxListOptionsExtension(element: Element): boolean {
10
+ return element.namespaceURI === spreadNs && element.localName === "ext" && element.getAttribute("uri") === namespace;
11
+ }
12
+
13
+ /** @param targets - Native rule target. @param formula - Native list source.
14
+ * @returns Key that ties settings to both the target and the rule's list source. */
15
+ export function xlsxListOptionsKey(targets: string, formula: string): string {
16
+ return JSON.stringify([targets.trim().replace(/\s+/gu, " "), formula.trim().replace(/^=/u, "")]);
17
+ }
18
+
19
+ /** @param worksheet - Native rule owner. @returns Extra settings validated before association with native rules. */
20
+ export function readXlsxPortableListOptions(worksheet: Element): Map<string, XlsxListOptions> {
21
+ const result = new Map<string, XlsxListOptions>();
22
+ const group = child(worksheet, "extLst");
23
+ const extensions = group ? children(group).filter(isXlsxListOptionsExtension) : [];
24
+ if (!extensions.length) return result;
25
+ const extension = extensions[0]!;
26
+ const [lists, ...unrecognized] = children(extension);
27
+ if (extensions.length !== 1 || !hasKnownAttributes(extension, ["uri"]) || lists?.namespaceURI !== namespace
28
+ || lists.localName !== "lists" || !hasKnownAttributes(lists, []) || unrecognized.length) {
29
+ throw new Error("工作表包含无法识别的下拉选项配置。");
30
+ }
31
+ for (const list of children(lists)) {
32
+ const targets = list.getAttribute("sqref"), formula = list.getAttribute("formula");
33
+ const multiple = list.getAttribute("multiple"), colored = list.getAttribute("colored");
34
+ const key = xlsxListOptionsKey(targets ?? "", formula ?? "");
35
+ if (list.namespaceURI !== namespace || list.localName !== "list"
36
+ || !hasKnownAttributes(list, ["sqref", "formula", "multiple", "colored"])
37
+ || !targets || !formula || result.has(key) || multiple !== null && multiple !== "1" || colored !== null && colored !== "1"
38
+ || !multiple && !colored) throw new Error("无效的下拉选项配置。");
39
+ const colors: (string | null)[] = [];
40
+ for (const item of children(list)) {
41
+ const index = readIndex(item, "index"), rgb = item.getAttribute("rgb");
42
+ if (!colored || item.namespaceURI !== namespace || item.localName !== "color"
43
+ || !hasKnownAttributes(item, ["index", "rgb"]) || children(item).length
44
+ || index === undefined || index >= 255 || colors[index] || !rgb || !/^#[\da-f]{6}$/iu.test(rgb)) {
45
+ throw new Error("无效的下拉选项颜色。");
46
+ }
47
+ while (colors.length <= index) colors.push(null);
48
+ colors[index] = rgb;
49
+ }
50
+ result.set(key, { ...(multiple && { multiple: true }), ...(colored && { optionColors: colors }) });
51
+ }
52
+ return result;
53
+ }
54
+
55
+ /** @param worksheet - Staged worksheet. @param records - Settings authored alongside its current native list rules. */
56
+ export function writeXlsxPortableListOptions(worksheet: Element, records: readonly XlsxListOptionsRecord[]): void {
57
+ let group = child(worksheet, "extLst");
58
+ if (group) for (const extension of children(group).filter(isXlsxListOptionsExtension)) group.removeChild(extension);
59
+ if (!records.length) {
60
+ if (group && !children(group).length) worksheet.removeChild(group);
61
+ return;
62
+ }
63
+ const document = worksheet.ownerDocument!;
64
+ if (!group) {
65
+ group = document.createElementNS(spreadNs, "extLst");
66
+ worksheet.appendChild(group);
67
+ }
68
+ const extension = document.createElementNS(spreadNs, "ext");
69
+ extension.setAttribute("uri", namespace);
70
+ const lists = document.createElementNS(namespace, "qkl:lists");
71
+ for (const { targets, formula, settings } of records) {
72
+ const list = document.createElementNS(namespace, "qkl:list");
73
+ list.setAttribute("sqref", targets);
74
+ list.setAttribute("formula", formula);
75
+ if (settings.multiple) list.setAttribute("multiple", "1");
76
+ if (settings.optionColors) {
77
+ list.setAttribute("colored", "1");
78
+ settings.optionColors.forEach((color, index) => {
79
+ if (!color) return;
80
+ const item = document.createElementNS(namespace, "qkl:color");
81
+ item.setAttribute("index", String(index));
82
+ item.setAttribute("rgb", color);
83
+ list.appendChild(item);
84
+ });
85
+ }
86
+ lists.appendChild(list);
87
+ }
88
+ extension.appendChild(lists);
89
+ group.appendChild(extension);
90
+ }
@@ -0,0 +1,14 @@
1
+ import type { WorkbookListValidationDefinition } from "@quirekit/workbook";
2
+
3
+ /** Optional settings recorded separately from the standard validation list. */
4
+ export type XlsxListOptions = Pick<WorkbookListValidationDefinition, "multiple" | "optionColors">;
5
+
6
+ /** Portable metadata and its association with a standard worksheet validation entry. */
7
+ export interface XlsxListOptionsRecord {
8
+ /** Native target addresses. */
9
+ readonly targets: string;
10
+ /** Native source expression. */
11
+ readonly formula: string;
12
+ /** Extra behaviors of the standard list. */
13
+ readonly settings: XlsxListOptions;
14
+ }
@@ -0,0 +1,51 @@
1
+ import type { PackageRelationship } from "../../../opc/types";
2
+ import { hasKnownAttributes } from "../../../opc/shape";
3
+ import { children, parsePart, readIndex, relationshipNs, spreadNs } from "../../../opc/xml";
4
+ import type { XlsxListOptions } from "./types";
5
+
6
+ const namespace = "http://www.wps.cn/et/2023/dataValidationExt";
7
+
8
+ /** @param parts - Retained package bytes. @param relationships - Workbook-owned parts.
9
+ * @returns Understood WPS list settings by the ID used on native validation entries. */
10
+ export function readXlsxWpsListOptions(parts: Readonly<Record<string, Uint8Array>>,
11
+ relationships: ReadonlyMap<string, PackageRelationship>): ReadonlyMap<string, XlsxListOptions> {
12
+ const options = new Map<string, XlsxListOptions>();
13
+ for (const { type, path } of relationships.values()) {
14
+ if (type !== relationshipNs + "/customXml" || !path) continue;
15
+ try {
16
+ const root = parsePart(parts[path], path).documentElement;
17
+ if (root?.namespaceURI !== namespace || root.localName !== "dvExts" || !hasKnownAttributes(root, [])) continue;
18
+ const records = new Map<string, XlsxListOptions>();
19
+ for (const record of children(root)) {
20
+ const id = record.getAttribute("uuid")?.toLowerCase();
21
+ const [list, ...extra] = children(record);
22
+ if (record.namespaceURI !== namespace || record.localName !== "dvExt" || !hasKnownAttributes(record, ["uuid"])
23
+ || !id || !/^\{[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}\}$/u.test(id) || list?.namespaceURI !== namespace
24
+ || list.localName !== "list" || extra.length || !hasKnownAttributes(list, ["multiSupport", "colorSupport"])) {
25
+ throw new Error("Unknown WPS list settings.");
26
+ }
27
+ if (options.has(id) || records.has(id)) return new Map();
28
+ const multiple = list.getAttribute("multiSupport"), colored = list.getAttribute("colorSupport");
29
+ if (![null, "0", "1"].includes(multiple) || ![null, "0", "1"].includes(colored)) throw new Error("Unknown WPS list flags.");
30
+ const colors: (string | null)[] = [];
31
+ for (const item of children(list)) {
32
+ const index = readIndex(item, "index");
33
+ const [color, ...other] = children(item);
34
+ const rgb = color?.getAttributeNS(spreadNs, "rgb");
35
+ if (colored !== "1" || item.namespaceURI !== namespace || item.localName !== "item" || !hasKnownAttributes(item, ["index"])
36
+ || index === undefined || index >= 255 || colors[index] || color?.namespaceURI !== namespace || color.localName !== "color"
37
+ || children(color).length || other.length || !rgb || !/^FF[\da-f]{6}$/iu.test(rgb)
38
+ || Array.from(color.attributes).some(attr => attr.namespaceURI !== "http://www.w3.org/2000/xmlns/"
39
+ && (attr.namespaceURI !== spreadNs || attr.localName !== "rgb"))) throw new Error("Unknown WPS list color.");
40
+ while (colors.length <= index) colors.push(null);
41
+ colors[index] = "#" + rgb.slice(2);
42
+ }
43
+ records.set(id, { ...(multiple === "1" && { multiple: true }), ...(colored === "1" && { optionColors: colors }) });
44
+ }
45
+ for (const [id, settings] of records) options.set(id, settings);
46
+ } catch {
47
+ // The package's original-part restriction preserves unrecognized vendor metadata.
48
+ }
49
+ }
50
+ return options;
51
+ }
@@ -0,0 +1,162 @@
1
+ import type { Element } from "@xmldom/xmldom";
2
+ import {
3
+ copyWorkbookValidationDefinition, copyWorkbookValidations, formatWorkbookColumn, formatWorkbookRange, formatWorkbookSpillReferences,
4
+ parseWorkbookRange, parseWorkbookValidationReference, workbookValidationComparisonTypes, workbookValidationDefinitionForRange,
5
+ workbookValidationLevels, workbookValidationOperators, type WorkbookValidationDefinition, type WorkbookValidationMessage,
6
+ type WorkbookRange, type WorkbookValidationRule, type WorkbookValidationSource, type WorkbookWorksheetIdentity
7
+ } from "@quirekit/workbook";
8
+ import { hasKnownAttributes, hasKnownChildren } from "../../opc/shape";
9
+ import { child, children, readIndex, spreadNs } from "../../opc/xml";
10
+ import { decodeXlsxText } from "../text";
11
+ import type { XlsxProgressPresentation } from "../progress/types";
12
+ import { xlsxImageValidationFormula } from "./image";
13
+ import { readXlsxTextValidation } from "./text";
14
+ import { findXlsxValidationExtensions, readXlsxExtendedValidation, readXlsxValidationExtension } from "./extended";
15
+ import { readXlsxPortableListOptions, xlsxListOptionsKey } from "./list-options/portable";
16
+ import type { XlsxListOptions } from "./list-options/types";
17
+
18
+ const attributes = ["type", "operator", "sqref", "allowBlank", "showDropDown", "showInputMessage", "showErrorMessage",
19
+ "promptTitle", "prompt", "errorTitle", "error", "errorStyle"];
20
+
21
+ /** @param node - Native flag owner. @param key - Boolean attribute name.
22
+ * @returns Its value or undefined when the attribute is omitted. */
23
+ function flag(node: Element, key: string): boolean | undefined {
24
+ if (!node.hasAttribute(key)) return;
25
+ const text = node.getAttribute(key);
26
+ if (text === "1" || text === "true") return true;
27
+ if (text === "0" || text === "false") return false;
28
+ throw new Error("无效的数据有效性开关。");
29
+ }
30
+
31
+ /** @param item - Native validation entry. @param field - Prompt or error message. @param enabled - Native display flag.
32
+ * @returns Authored message, preserving the presence of disabled text. */
33
+ function message(item: Element, field: "prompt" | "error", enabled: boolean): WorkbookValidationMessage | undefined {
34
+ const title = decodeXlsxText(item.getAttribute(field + "Title") ?? ""), text = decodeXlsxText(item.getAttribute(field) ?? "");
35
+ if (!enabled && !title && !text) return;
36
+ return { ...(title && { title }), ...(text && { message: text }), ...(!enabled && { enabled }) };
37
+ }
38
+
39
+ /** @param formula - Raw formula1 text. @param owner - Worksheet owning the list.
40
+ * @param directory - Worksheet names used by the formula. @returns A portable list source. */
41
+ function listSource(formula: string, owner: string, directory: readonly WorkbookWorksheetIdentity[]): WorkbookValidationSource {
42
+ const literal = /^"([^"]*)"$/u.exec(formula);
43
+ if (literal) return { kind: "options", values: literal[1]!.split(",") };
44
+ if (formula.toUpperCase() === "#REF!") return { kind: "broken" };
45
+ return parseWorkbookValidationReference(formula, owner, directory, true);
46
+ }
47
+
48
+ /** @param raw - Constant expression from the file. @returns A portable numeric bound. */
49
+ function number(raw: string): number {
50
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:E[+-]?\d+)?$/iu.test(raw) || !Number.isFinite(Number(raw))) {
51
+ throw new Error("数据有效性的比较使用了暂未支持的公式。");
52
+ }
53
+ return Number(raw);
54
+ }
55
+
56
+ /** @param item - One native rule. @param range - Its first target. @param directory - File worksheet names.
57
+ * @param first - First raw formula. @param second - Second raw formula. @param progress - Candidate matching progress presentation.
58
+ * @returns Portable definition when its value space is covered. */
59
+ function definition(item: Element, range: WorkbookRange, directory: readonly WorkbookWorksheetIdentity[],
60
+ first: string, second: string, progress?: XlsxProgressPresentation): WorkbookValidationDefinition {
61
+ const type = item.getAttribute("type") || "none";
62
+ const allowBlank = flag(item, "allowBlank");
63
+ const hidden = flag(item, "showDropDown");
64
+ const prompt = message(item, "prompt", flag(item, "showInputMessage") === true);
65
+ const errorFlag = flag(item, "showErrorMessage");
66
+ const error = message(item, "error", errorFlag === true);
67
+ const style = item.getAttribute("errorStyle");
68
+ const level = workbookValidationLevels.find(value => value === (style || "stop"));
69
+ if (!level) throw new Error("暂未支持的校验错误级别。");
70
+ const alert = error || style ? { level, enabled: errorFlag === true, ...error } : undefined;
71
+ const options = { allowBlank, prompt, alert };
72
+ const formula1 = first.trim().replace(/^=/u, ""), formula2 = second.trim().replace(/^=/u, "");
73
+ const operator = workbookValidationOperators.find(value => value === (item.getAttribute("operator") || "between"));
74
+ if (progress) {
75
+ const anchor = formatWorkbookColumn(range.startColumn) + (range.startRow + 1);
76
+ const knownWps = progress.wps && type === "custom" && !formula2 && !item.hasAttribute("operator")
77
+ && formula1 === `IF(TRUE,AND(${anchor}>=0,${anchor}<=1),"Complete")`;
78
+ const knownNative = !progress.wps && type === "decimal" && operator === "between" && number(formula1) === 0 && number(formula2) === 1;
79
+ if (knownWps || knownNative) return { type: "progress", color: progress.color, ...options,
80
+ allowBlank: allowBlank === true, alert: alert ?? { level, enabled: errorFlag === true } };
81
+ }
82
+ if (type === "custom" && !formula2 && !item.hasAttribute("operator")) {
83
+ const text = formula1 === xlsxImageValidationFormula(range) ? "image" : readXlsxTextValidation(formula1);
84
+ if (text) return { type: text, ...options, allowBlank: allowBlank === true, alert: alert ?? { level, enabled: errorFlag === true } };
85
+ }
86
+ if (type === "custom" && formula1 && !formula2 && operator) {
87
+ return { type, formula: formatWorkbookSpillReferences("=" + formula1, "postfix").formula, ...options };
88
+ }
89
+ if (type === "none" && !formula1 && !formula2) return { type: "any", ...options };
90
+ // Calc may write an unused zero as formula2 for list validation.
91
+ if (type === "list" && (!formula2 || formula2 === "0")) return { type, source: listSource(formula1, range.sheetId, directory), dropdown: !hidden, ...options };
92
+ const comparison = workbookValidationComparisonTypes.find(value => value === type);
93
+ if (!comparison || !operator) throw new Error("暂未支持的校验类型。");
94
+ const pair = operator === "between" || operator === "notBetween";
95
+ if (!pair && formula2) throw new Error("数据有效性的比较包含额外公式。");
96
+ return { type: comparison, operator, first: number(formula1), ...(pair && { second: number(formula2) }), ...options };
97
+ }
98
+
99
+ /** @param entry - Ordinary SpreadsheetML rule. @param wpsOptions - Recognized settings attached to its ID.
100
+ * @returns The raw formulas and targets. */
101
+ function readOrdinaryRule(entry: Element, wpsOptions?: XlsxListOptions): { first: string; second: string; targets: string } {
102
+ if (entry.namespaceURI !== spreadNs || entry.localName !== "dataValidation" || !hasKnownAttributes(entry, wpsOptions ? [...attributes, "id"] : attributes)
103
+ || !hasKnownChildren(entry, ["formula1", "formula2"])
104
+ || children(entry).some(formula => !hasKnownAttributes(formula, []) || !hasKnownChildren(formula, []))) {
105
+ throw new Error("暂未支持的数据有效性扩展。");
106
+ }
107
+ return { first: child(entry, "formula1")?.textContent ?? "", second: child(entry, "formula2")?.textContent ?? "",
108
+ targets: entry.getAttribute("sqref") ?? "" };
109
+ }
110
+
111
+ /** @param worksheet - Native worksheet. @param sheetId - Canonical worksheet identity. @param directory - File worksheet names.
112
+ * @param progress - Native progress presentations by range.
113
+ * @param wpsLists - Recognized WPS list options by native rule ID.
114
+ * @returns Decodable rules and any conditional formats owned by those rules. */
115
+ export function readXlsxValidations(worksheet: Element, sheetId: string, directory: readonly WorkbookWorksheetIdentity[],
116
+ progress?: ReadonlyMap<string, XlsxProgressPresentation>, wpsLists?: ReadonlyMap<string, XlsxListOptions>) {
117
+ const ordinary = children(worksheet, "dataValidations"), extensions = findXlsxValidationExtensions(worksheet);
118
+ const extended = extensions[0] && readXlsxValidationExtension(extensions[0]);
119
+ const entries = [ordinary[0], extended] as const;
120
+ let writable = ordinary.length <= 1 && extensions.length <= 1 && (!extensions.length || extended !== undefined);
121
+ let portable: Map<string, XlsxListOptions> | undefined;
122
+ try { portable = readXlsxPortableListOptions(worksheet); }
123
+ catch { writable = false; }
124
+ if (!ordinary.length && !extensions.length) return { writable: writable && !portable?.size };
125
+ const rules: WorkbookValidationRule[] = [];
126
+ const progressConditions = new Set<Element>();
127
+ for (const [index, container] of entries.entries()) {
128
+ if (!container) continue;
129
+ const items = children(container);
130
+ if (index === 0 && container.namespaceURI !== spreadNs || !hasKnownAttributes(container, ["count", "disablePrompts"])
131
+ || container.hasAttribute("count") && readIndex(container, "count") !== items.length) writable = false;
132
+ let suppressPrompts = false;
133
+ try { suppressPrompts = flag(container, "disablePrompts") === true; }
134
+ catch { writable = false; }
135
+ if (suppressPrompts) writable = false;
136
+ for (const entry of items) {
137
+ try {
138
+ const nativeId = entry.getAttribute("id")?.toLowerCase();
139
+ const wps = index === 0 && nativeId ? wpsLists?.get(nativeId) : undefined;
140
+ const raw = index === 0 ? readOrdinaryRule(entry, wps) : readXlsxExtendedValidation(entry, attributes.filter(name => name !== "sqref"));
141
+ if (!raw?.targets.trim()) throw new Error("数据有效性缺少目标范围或使用了未支持的扩展。");
142
+ const target = raw.targets.trim().split(/\s+/u).map(a1 => parseWorkbookRange(sheetId, a1));
143
+ const presentation = index === 0 && target.length === 1 ? progress?.get(formatWorkbookRange(target[0]!)) : undefined;
144
+ let parsed = definition(entry, target[0]!, directory, raw.first, raw.second, presentation);
145
+ if (parsed.type === "list" && index === 0) {
146
+ const key = xlsxListOptionsKey(raw.targets, raw.first);
147
+ const authored = portable?.get(key);
148
+ if (wps && authored) throw new Error("下拉列表包含两份选项配置。");
149
+ parsed = { ...parsed, ...wps, ...authored };
150
+ if (authored) portable?.delete(key);
151
+ } else if (wps) throw new Error("非序列规则包含下拉选项配置。");
152
+ const rule = copyWorkbookValidationDefinition(suppressPrompts && parsed.prompt
153
+ ? { ...parsed, prompt: { ...parsed.prompt, enabled: false } } : parsed, sheetId);
154
+ const anchored = { range: target[0]!, definition: rule };
155
+ rules.push(...target.map(range => ({ range, definition: workbookValidationDefinitionForRange(anchored, range) })));
156
+ if (rule.type === "progress") for (const node of presentation?.conditions ?? []) progressConditions.add(node);
157
+ } catch { writable = false; }
158
+ }
159
+ }
160
+ try { return { writable: writable && !portable?.size, rules: copyWorkbookValidations(sheetId, rules), progressConditions }; }
161
+ catch { return { writable: false }; }
162
+ }
@@ -0,0 +1,13 @@
1
+ import { workbookTextValidationTypes, type WorkbookTextValidationType } from "@quirekit/workbook";
2
+
3
+ const markers = { email: "Email", url: "Web", mobile: "Tel", citizenId: "ID" } satisfies Record<WorkbookTextValidationType, string>;
4
+
5
+ /** @param type - Text column kind. @returns The native producer marker in a SpreadsheetML custom validation formula. */
6
+ export function xlsxTextValidationFormula(type: WorkbookTextValidationType): string {
7
+ return 'IF(TRUE,TRUE,"' + markers[type] + '")';
8
+ }
9
+
10
+ /** @param formula - Parsed native formula text. @returns Known text column kind when the exact marker is present. */
11
+ export function readXlsxTextValidation(formula: string): WorkbookTextValidationType | undefined {
12
+ return workbookTextValidationTypes.find(type => formula === xlsxTextValidationFormula(type));
13
+ }