ooxml-core 1.3.0 → 1.4.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 (86) hide show
  1. package/README.md +2 -0
  2. package/dist/automation/index.cjs +5785 -5295
  3. package/dist/automation/index.mjs +5785 -5295
  4. package/dist/teams/index.cjs +72 -23
  5. package/dist/teams/index.mjs +72 -23
  6. package/dist/visio/edit-commands.d.ts +11 -2
  7. package/dist/visio/edit-commands.d.ts.map +1 -1
  8. package/dist/visio/edit-page-size.d.ts +5 -0
  9. package/dist/visio/edit-page-size.d.ts.map +1 -0
  10. package/dist/visio/edit-pages.d.ts.map +1 -1
  11. package/dist/visio/edit-text-range-commands.d.ts +21 -0
  12. package/dist/visio/edit-text-range-commands.d.ts.map +1 -0
  13. package/dist/visio/edit-text-range-plan.d.ts +7 -0
  14. package/dist/visio/edit-text-range-plan.d.ts.map +1 -0
  15. package/dist/visio/edit-text-ranges.d.ts +4 -0
  16. package/dist/visio/edit-text-ranges.d.ts.map +1 -0
  17. package/dist/visio/edit-text-target.d.ts +3 -0
  18. package/dist/visio/edit-text-target.d.ts.map +1 -0
  19. package/dist/visio/edit-text.d.ts.map +1 -1
  20. package/dist/visio/edit.d.ts +1 -1
  21. package/dist/visio/edit.d.ts.map +1 -1
  22. package/dist/visio/index.cjs +395 -27
  23. package/dist/visio/index.d.ts +1 -1
  24. package/dist/visio/index.d.ts.map +1 -1
  25. package/dist/visio/index.mjs +395 -27
  26. package/dist/visio/model.d.ts +3 -0
  27. package/dist/visio/model.d.ts.map +1 -1
  28. package/dist/visio/parser.d.ts.map +1 -1
  29. package/dist/visio/ui/edit-commands.d.ts.map +1 -1
  30. package/dist/visio/ui/index.cjs +134 -11
  31. package/dist/visio/ui/index.d.ts +1 -0
  32. package/dist/visio/ui/index.d.ts.map +1 -1
  33. package/dist/visio/ui/index.mjs +130 -12
  34. package/dist/visio/ui/page-size.d.ts +31 -0
  35. package/dist/visio/ui/page-size.d.ts.map +1 -0
  36. package/dist/visio/ui/scene-validation.d.ts.map +1 -1
  37. package/dist/visio/ui/snapshot-scene.d.ts.map +1 -1
  38. package/dist/visio/ui/text-replace-types.d.ts +6 -2
  39. package/dist/visio/ui/text-replace-types.d.ts.map +1 -1
  40. package/dist/visio/ui/text-replace.d.ts +3 -4
  41. package/dist/visio/ui/text-replace.d.ts.map +1 -1
  42. package/dist/visio/xml-validation.d.ts.map +1 -1
  43. package/dist/xlsx/collab/codec.d.ts +2 -0
  44. package/dist/xlsx/collab/codec.d.ts.map +1 -1
  45. package/dist/xlsx/collab/index.cjs +5 -0
  46. package/dist/xlsx/collab/index.mjs +5 -0
  47. package/dist/xlsx/display-text.d.ts +4 -0
  48. package/dist/xlsx/display-text.d.ts.map +1 -0
  49. package/dist/xlsx/edit/cell-values.d.ts +4 -3
  50. package/dist/xlsx/edit/cell-values.d.ts.map +1 -1
  51. package/dist/xlsx/edit/clipboard.d.ts.map +1 -1
  52. package/dist/xlsx/edit/context.d.ts +7 -5
  53. package/dist/xlsx/edit/context.d.ts.map +1 -1
  54. package/dist/xlsx/edit/deps.d.ts +1 -1
  55. package/dist/xlsx/edit/deps.d.ts.map +1 -1
  56. package/dist/xlsx/edit/dimensions.d.ts.map +1 -1
  57. package/dist/xlsx/edit/fill.d.ts.map +1 -1
  58. package/dist/xlsx/edit/filter.d.ts.map +1 -1
  59. package/dist/xlsx/edit/find.d.ts.map +1 -1
  60. package/dist/xlsx/edit/paste-selection.d.ts.map +1 -1
  61. package/dist/xlsx/edit/shift-formulas.d.ts +10 -4
  62. package/dist/xlsx/edit/shift-formulas.d.ts.map +1 -1
  63. package/dist/xlsx/edit/table-header-names.d.ts +3 -0
  64. package/dist/xlsx/edit/table-header-names.d.ts.map +1 -0
  65. package/dist/xlsx/edit/tables.d.ts +1 -2
  66. package/dist/xlsx/edit/tables.d.ts.map +1 -1
  67. package/dist/xlsx/filter-rows.d.ts +8 -0
  68. package/dist/xlsx/filter-rows.d.ts.map +1 -0
  69. package/dist/xlsx/formula/functions/math-sum.d.ts.map +1 -1
  70. package/dist/xlsx/formula/table-refs.d.ts +2 -0
  71. package/dist/xlsx/formula/table-refs.d.ts.map +1 -1
  72. package/dist/xlsx/index.cjs +445 -323
  73. package/dist/xlsx/index.mjs +445 -323
  74. package/dist/xlsx/load/index.cjs +7621 -7585
  75. package/dist/xlsx/load/index.mjs +7621 -7585
  76. package/dist/xlsx/model.d.ts +4 -0
  77. package/dist/xlsx/model.d.ts.map +1 -1
  78. package/dist/xlsx/read/load.d.ts.map +1 -1
  79. package/dist/xlsx/ui/index.cjs +443 -323
  80. package/dist/xlsx/ui/index.mjs +443 -323
  81. package/dist/xlsx/ui/locales/de/shell.d.ts.map +1 -1
  82. package/dist/xlsx/ui/locales/en/shell.d.ts.map +1 -1
  83. package/dist/xlsx/ui/locales/es/shell.d.ts.map +1 -1
  84. package/dist/xlsx/ui/locales/fr/shell.d.ts.map +1 -1
  85. package/dist/xlsx/ui/locales/zh-CN/shell.d.ts.map +1 -1
  86. package/package.json +1 -1
@@ -7809,6 +7809,39 @@ function liftCriteria(ctx, pairs, fn) {
7809
7809
  });
7810
7810
  }
7811
7811
 
7812
+ // xlsx/display-text.ts
7813
+ function displayText(workbook, cell) {
7814
+ if (!cell || cell.value === null) return "";
7815
+ const value = cell.value;
7816
+ if (isCellError(value)) return value.error;
7817
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
7818
+ if (cell.richText && typeof value === "string") return value;
7819
+ return formatValue(value, styleAt(workbook, cell.styleId).numFmt, {
7820
+ date1904: workbook.date1904
7821
+ }).text;
7822
+ }
7823
+
7824
+ // xlsx/filter-rows.ts
7825
+ function rowMatchesFilter(workbook, sheet, row2, filter2) {
7826
+ return (filter2.columns ?? []).every((column) => {
7827
+ if (!column.values && !column.blank) return true;
7828
+ const text3 = displayText(workbook, getCell(sheet, row2, filter2.range.start.col + column.offset));
7829
+ return text3 === "" ? !!column.blank : (column.values ?? []).includes(text3);
7830
+ });
7831
+ }
7832
+ function isFilteredRow(workbook, sheet, row2) {
7833
+ const info = sheet.rowInfo.get(row2);
7834
+ if (info?.filteredOut !== void 0) return info.filteredOut;
7835
+ const filter2 = sheet.autoFilter;
7836
+ return !!info?.hidden && !!filter2 && row2 > filter2.range.start.row && row2 <= filter2.range.end.row && !rowMatchesFilter(workbook, sheet, row2, filter2);
7837
+ }
7838
+ function readFilteredRows(workbook) {
7839
+ for (const sheet of workbook.sheets)
7840
+ for (const [row2, info] of sheet.rowInfo)
7841
+ if (sheet.autoFilter && row2 > sheet.autoFilter.range.start.row && row2 <= sheet.autoFilter.range.end.row && info.hidden)
7842
+ info.filteredOut = isFilteredRow(workbook, sheet, row2);
7843
+ }
7844
+
7812
7845
  // xlsx/formula/functions/math-sum.ts
7813
7846
  var C18 = "Math & Trig";
7814
7847
  var numbersOf = (values2) => values2.filter((v) => typeof v === "number");
@@ -7846,7 +7879,7 @@ function pairwise(ctx, args, fn) {
7846
7879
  return total2;
7847
7880
  }
7848
7881
  var SUBTOTAL_RE = /\b(?:_xlfn\.)?(?:SUBTOTAL|AGGREGATE)\s*\(/i;
7849
- function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true) {
7882
+ function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true, skipFiltered = false) {
7850
7883
  const out = [];
7851
7884
  const host = ctx.frame.host;
7852
7885
  for (const arg of args) {
@@ -7859,6 +7892,7 @@ function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true) {
7859
7892
  for (const area of arg.areas) {
7860
7893
  const sheet = ctx.workbook.sheets[area.sheet];
7861
7894
  host.forEachStored(area.sheet, area.range, (value, row2, col) => {
7895
+ if (skipFiltered && sheet && isFilteredRow(ctx.workbook, sheet, row2)) return;
7862
7896
  if (skipHidden && sheet?.rowInfo.get(row2)?.hidden) return;
7863
7897
  if (skipNested && SUBTOTAL_RE.test(host.cellFormula(area.sheet, row2, col) ?? "")) return;
7864
7898
  if (skipErrors && isError(value)) return;
@@ -8024,7 +8058,7 @@ var SUM_FUNCTIONS = [
8024
8058
  const code = int(args[0]);
8025
8059
  const base = code > 100 ? code - 100 : code;
8026
8060
  if (base < 1 || base > 11) fail(ERR.VALUE);
8027
- return aggregate(base, subtotalValues(ctx, args.slice(1), code > 100, false));
8061
+ return aggregate(base, subtotalValues(ctx, args.slice(1), code > 100, false, true, true));
8028
8062
  },
8029
8063
  ["value", "any"]
8030
8064
  ),
@@ -13562,6 +13596,31 @@ function renameTableInFormula(formula, oldName, newName) {
13562
13596
  }
13563
13597
  return changed ? joinTokens(tokens2) : formula;
13564
13598
  }
13599
+ function renameTableColumnInFormula(formula, tableName, oldName, newName, insideTable = false) {
13600
+ const tokens2 = safeTokens2(formula);
13601
+ if (!tokens2) return formula;
13602
+ const escaped = newName.replace(/['#@[\]]/g, "'$&");
13603
+ let changed = false;
13604
+ for (const [index2, token] of tokens2.entries()) {
13605
+ if (token.kind !== "structured" || token.prefix) continue;
13606
+ if (token.value === "" && tokens2[index2 - 1]?.prefix) continue;
13607
+ if (token.value === "" ? !insideTable : String(token.value).toLowerCase() !== tableName.toLowerCase())
13608
+ continue;
13609
+ const next = token.text.replace(
13610
+ /\[(@?)((?:[^\[\]']|'[\s\S])*)\]/g,
13611
+ (original, at, name) => {
13612
+ if (name.startsWith("#") || name.replace(/'(.)/g, "$1").toLowerCase() !== oldName.toLowerCase())
13613
+ return original;
13614
+ return `[${at}${escaped}]`;
13615
+ }
13616
+ );
13617
+ if (next !== token.text) {
13618
+ token.text = next;
13619
+ changed = true;
13620
+ }
13621
+ }
13622
+ return changed ? joinTokens(tokens2) : formula;
13623
+ }
13565
13624
 
13566
13625
  // xlsx/formula/structured-to-a1.ts
13567
13626
  var abs = (row2, col, rowAbs = true) => `$${columnLabel(col)}${rowAbs ? "$" : ""}${row2 + 1}`;
@@ -20318,6 +20377,7 @@ async function loadXlsx(input) {
20318
20377
  if (!sheets.length) throw new Error("XLSX workbook has no sheets");
20319
20378
  await resolveSmartArt(sheets, source2, warn);
20320
20379
  liftPrintAreas(workbook);
20380
+ readFilteredRows(workbook);
20321
20381
  return workbook;
20322
20382
  }
20323
20383
  function readProperties(core, app, custom, warn) {
@@ -24620,16 +24680,6 @@ function forEachPosition(sheet, range, visit2) {
24620
24680
  for (let row2 = r.start.row; row2 <= r.end.row; row2++)
24621
24681
  for (let col = r.start.col; col <= r.end.col; col++) visit2(row2, col);
24622
24682
  }
24623
- function displayText(workbook, cell) {
24624
- if (!cell) return "";
24625
- const value = cell.value;
24626
- if (value === null) return "";
24627
- if (isCellError(value)) return value.error;
24628
- if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
24629
- if (cell.richText && typeof value === "string") return value;
24630
- const format = styleAt(workbook, cell.styleId).numFmt;
24631
- return formatValue(value, format, { date1904: workbook.date1904 }).text;
24632
- }
24633
24683
  function rawText(value) {
24634
24684
  if (value === null) return "";
24635
24685
  if (isCellError(value)) return value.error;
@@ -24646,19 +24696,266 @@ function pruneCell(sheet, row2, col) {
24646
24696
  }
24647
24697
  }
24648
24698
 
24699
+ // xlsx/edit/shift-preserved.ts
24700
+ var XM3 = "http://schemas.microsoft.com/office/excel/2006/main";
24701
+ function descendants3(root, ns, local) {
24702
+ return Array.from(root.getElementsByTagNameNS(ns, local));
24703
+ }
24704
+ function editEntries(sheet, key, edit, quick) {
24705
+ const list = sheet.preserved.get(key);
24706
+ if (!list) return;
24707
+ const next = [];
24708
+ for (const xml of list) {
24709
+ if (!quick(xml)) {
24710
+ next.push(xml);
24711
+ continue;
24712
+ }
24713
+ let doc;
24714
+ try {
24715
+ doc = parseXml(xml);
24716
+ } catch {
24717
+ next.push(xml);
24718
+ continue;
24719
+ }
24720
+ const outcome = edit(doc.documentElement);
24721
+ if (outcome === "same") next.push(xml);
24722
+ else if (outcome === "changed") next.push(buildXml(doc));
24723
+ }
24724
+ if (next.length) sheet.preserved.set(key, next);
24725
+ else sheet.preserved.delete(key);
24726
+ }
24727
+ function shiftBreaks(root, shift2) {
24728
+ let changed = false;
24729
+ for (const brk of elements(root).filter((e) => e.localName === "brk")) {
24730
+ const id = Number(brk.getAttribute("id") ?? "0");
24731
+ const moved = shiftIndex(id, shift2);
24732
+ if (moved === id) continue;
24733
+ changed = true;
24734
+ if (moved === void 0 || moved === 0) root.removeChild(brk);
24735
+ else brk.setAttribute("id", String(moved));
24736
+ }
24737
+ if (!changed) return "same";
24738
+ const left2 = elements(root).filter((e) => e.localName === "brk");
24739
+ if (!left2.length) return "drop";
24740
+ if (root.hasAttribute("count")) root.setAttribute("count", String(left2.length));
24741
+ if (root.hasAttribute("manualBreakCount")) {
24742
+ const manual = left2.filter((b) => /^(1|true)$/.test(b.getAttribute("man") ?? "")).length;
24743
+ root.setAttribute("manualBreakCount", String(manual));
24744
+ }
24745
+ return "changed";
24746
+ }
24747
+ function removeUpwards(node, root) {
24748
+ let current = node;
24749
+ while (current && current !== root) {
24750
+ const parent = current.parentNode;
24751
+ parent?.removeChild(current);
24752
+ if (!parent) break;
24753
+ const emptied = elements(parent).length === 0 || parent.localName === "sparklineGroup" && !elements(parent).some((e) => e.localName === "sparklines");
24754
+ if (!emptied) break;
24755
+ current = parent;
24756
+ }
24757
+ }
24758
+ function shiftSqrefs(root, moveRange) {
24759
+ let changed = false;
24760
+ for (const sqref2 of descendants3(root, XM3, "sqref")) {
24761
+ const old = sqref2.textContent ?? "";
24762
+ const ranges = parseSqref(old).map(moveRange).filter((r) => !!r);
24763
+ const text3 = ranges.map(formatRange).join(" ");
24764
+ if (text3 === old.trim()) continue;
24765
+ changed = true;
24766
+ const owner = sqref2.parentNode;
24767
+ if (ranges.length) sqref2.textContent = text3;
24768
+ else if (owner) removeUpwards(owner, root);
24769
+ }
24770
+ if (!changed) return "same";
24771
+ for (const list of elements(root).flatMap((ext) => elements(ext)))
24772
+ if (list.localName === "dataValidations" && list.hasAttribute("count"))
24773
+ list.setAttribute("count", String(elements(list).length));
24774
+ return elements(root).length > 0 ? "changed" : "drop";
24775
+ }
24776
+ function shiftPreservedXml(sheet, shift2, moveRange, banded) {
24777
+ if (!banded)
24778
+ editEntries(
24779
+ sheet,
24780
+ shift2.axis === "row" ? "rowBreaks" : "colBreaks",
24781
+ (root) => shiftBreaks(root, shift2),
24782
+ (xml) => xml.includes("brk")
24783
+ );
24784
+ editEntries(
24785
+ sheet,
24786
+ "extLst",
24787
+ (root) => shiftSqrefs(root, moveRange),
24788
+ (xml) => xml.includes("sqref")
24789
+ );
24790
+ }
24791
+ function rewritePreservedFormulas(sheet, rewrite2) {
24792
+ editEntries(
24793
+ sheet,
24794
+ "extLst",
24795
+ (root) => {
24796
+ let changed = false;
24797
+ for (const f6 of descendants3(root, XM3, "f")) {
24798
+ const text3 = f6.textContent ?? "";
24799
+ let next = text3;
24800
+ try {
24801
+ next = rewrite2(text3, sheet.name);
24802
+ } catch {
24803
+ next = text3;
24804
+ }
24805
+ if (next === text3) continue;
24806
+ f6.textContent = next;
24807
+ changed = true;
24808
+ }
24809
+ return changed ? "changed" : "same";
24810
+ },
24811
+ (xml) => xml.includes(":f>")
24812
+ );
24813
+ }
24814
+
24815
+ // xlsx/edit/conditional-formulas.ts
24816
+ function rewriteConditionalRule(rule, rewrite2) {
24817
+ const safe2 = (formula) => {
24818
+ try {
24819
+ return rewrite2(formula);
24820
+ } catch {
24821
+ return formula;
24822
+ }
24823
+ };
24824
+ if (rule.type === "cellIs") rule.formulas = rule.formulas.map(safe2);
24825
+ else if (rule.type === "expression") rule.formula = safe2(rule.formula);
24826
+ if (rule.type === "colorScale" || rule.type === "iconSet") {
24827
+ for (const threshold of rule.thresholds)
24828
+ if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
24829
+ }
24830
+ if (rule.type === "dataBar") {
24831
+ for (const threshold of [rule.min, rule.max])
24832
+ if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
24833
+ }
24834
+ if (rule.type === "dataBar") rewriteConditionalExtension(rule, safe2);
24835
+ }
24836
+
24837
+ // xlsx/edit/shift-formulas.ts
24838
+ function safe(rewrite2, formula, formulaSheet, at) {
24839
+ try {
24840
+ return rewrite2(formula, formulaSheet, at);
24841
+ } catch {
24842
+ return formula;
24843
+ }
24844
+ }
24845
+ function rewriteFormulas(workbook, rewrite2, formulaCells) {
24846
+ const bySheet = formulaCells && /* @__PURE__ */ new Map();
24847
+ if (bySheet)
24848
+ for (const cell of formulaCells ?? []) {
24849
+ const positions2 = bySheet.get(cell.sheet);
24850
+ if (positions2) positions2.push(cell);
24851
+ else bySheet.set(cell.sheet, [cell]);
24852
+ }
24853
+ workbook.sheets.forEach(
24854
+ (sheet, index2) => rewriteSheetFormulas(sheet, rewrite2, bySheet ? bySheet.get(index2) ?? [] : void 0)
24855
+ );
24856
+ for (const definedName of workbook.definedNames) {
24857
+ const local = definedName.localSheet === void 0 ? void 0 : workbook.sheets[definedName.localSheet];
24858
+ definedName.formula = safe(rewrite2, definedName.formula, local?.name ?? "");
24859
+ }
24860
+ }
24861
+ function rewriteSheetFormulas(sheet, rewrite2, formulaCells) {
24862
+ rewritePreservedFormulas(sheet, rewrite2);
24863
+ {
24864
+ const name = sheet.name;
24865
+ for (const table2 of sheet.tables)
24866
+ for (const [index2, column] of table2.columns.entries())
24867
+ if (column.calculatedColumnFormula !== void 0)
24868
+ column.calculatedColumnFormula = safe(rewrite2, column.calculatedColumnFormula, name, {
24869
+ row: table2.range.start.row + (table2.headerRow ? 1 : 0),
24870
+ col: table2.range.start.col + index2
24871
+ });
24872
+ if (formulaCells) {
24873
+ for (const at of formulaCells) {
24874
+ const cell = getCell(sheet, at.row, at.col);
24875
+ if (cell?.formula !== void 0) cell.formula = safe(rewrite2, cell.formula, name, at);
24876
+ }
24877
+ } else {
24878
+ sheet.rows.forEach(
24879
+ (cells, row2) => cells.forEach((cell, col) => {
24880
+ if (cell.formula !== void 0)
24881
+ cell.formula = safe(rewrite2, cell.formula, name, { row: row2, col });
24882
+ })
24883
+ );
24884
+ }
24885
+ for (const cf of sheet.conditionalFormats)
24886
+ for (const rule of cf.rules)
24887
+ rewriteConditionalRule(rule, (f6) => rewrite2(f6, name, cf.ranges[0]?.start));
24888
+ for (const dv of sheet.dataValidations) {
24889
+ if (dv.formula1 !== void 0)
24890
+ dv.formula1 = safe(rewrite2, dv.formula1, name, dv.ranges[0]?.start);
24891
+ if (dv.formula2 !== void 0)
24892
+ dv.formula2 = safe(rewrite2, dv.formula2, name, dv.ranges[0]?.start);
24893
+ }
24894
+ for (const link of sheet.hyperlinks)
24895
+ if (link.location && /![A-Za-z$]/.test(link.location))
24896
+ link.location = safe(rewrite2, link.location, name);
24897
+ for (const drawing of sheet.drawings) {
24898
+ if (drawing.kind !== "chart") continue;
24899
+ for (const series of drawing.series) {
24900
+ if (series.nameRef) series.nameRef = safe(rewrite2, series.nameRef, name);
24901
+ if (series.categoriesRef) series.categoriesRef = safe(rewrite2, series.categoriesRef, name);
24902
+ if (series.valuesRef) series.valuesRef = safe(rewrite2, series.valuesRef, name);
24903
+ }
24904
+ }
24905
+ }
24906
+ }
24907
+
24908
+ // xlsx/edit/table-header-names.ts
24909
+ function uniqueHeaders(texts) {
24910
+ const used = /* @__PURE__ */ new Set();
24911
+ return texts.map((text3, i2) => {
24912
+ let base = text3.trim() || `Column${i2 + 1}`;
24913
+ if (!used.has(base.toLowerCase())) {
24914
+ used.add(base.toLowerCase());
24915
+ return base;
24916
+ }
24917
+ for (let n2 = 2; ; n2++) {
24918
+ const candidate = `${base}${n2}`;
24919
+ if (!used.has(candidate.toLowerCase())) {
24920
+ used.add(candidate.toLowerCase());
24921
+ base = candidate;
24922
+ return base;
24923
+ }
24924
+ }
24925
+ });
24926
+ }
24927
+
24649
24928
  // xlsx/edit/cell-values.ts
24650
24929
  function tableHeaderAt(sheet, row2, col) {
24651
24930
  return sheet.tables.find(
24652
24931
  (t) => t.headerRow && t.range.start.row === row2 && col >= t.range.start.col && col <= t.range.end.col
24653
24932
  );
24654
24933
  }
24655
- function syncTableHeader(ctx, sheet, row2, col) {
24934
+ function syncTableHeader(ctx, sheet, row2, col, formulaCells) {
24656
24935
  const table2 = tableHeaderAt(sheet, row2, col);
24657
24936
  if (!table2) return;
24658
24937
  const column = table2.columns[col - table2.range.start.col];
24659
24938
  if (!column) return;
24660
24939
  const text3 = displayText(ctx.workbook, getCell(sheet, row2, col)).trim();
24661
- if (text3) column.name = text3;
24940
+ if (!text3 || column.name === text3) return;
24941
+ const oldName = column.name;
24942
+ const others = table2.columns.filter((entry3) => entry3 !== column).map((entry3) => entry3.name);
24943
+ const newName = uniqueHeaders([...others, text3]).at(-1);
24944
+ column.name = newName;
24945
+ if (newName !== text3) writeValue(sheet, row2, col, newName);
24946
+ const index2 = ctx.workbook.sheets.indexOf(sheet);
24947
+ const cells = formulaCells && (formulaCells.some((cell) => cell.sheet === index2 && cell.row === row2 && cell.col === col) ? formulaCells : [...formulaCells, { sheet: index2, row: row2, col }]);
24948
+ rewriteFormulas(
24949
+ ctx.workbook,
24950
+ (formula, formulaSheet, at) => renameTableColumnInFormula(
24951
+ formula,
24952
+ table2.name,
24953
+ oldName,
24954
+ newName,
24955
+ formulaSheet === sheet.name && !!at && rangeContains(table2.range, at)
24956
+ ),
24957
+ cells
24958
+ );
24662
24959
  }
24663
24960
  function writeScopes(sheet, index2, range, parts = []) {
24664
24961
  const r = normalizeRange(range);
@@ -24668,9 +24965,9 @@ function writeScopes(sheet, index2, range, parts = []) {
24668
24965
  cellRange(t.range.start.row, t.range.start.col, t.range.start.row, t.range.end.col)
24669
24966
  )
24670
24967
  );
24671
- const all2 = touchesHeader && !parts.includes("tables") ? [...parts, "tables"] : parts;
24968
+ if (touchesHeader) return [{ kind: "refs" }, { kind: "cells", sheet: index2, ranges: [r] }];
24672
24969
  const scopes = [{ kind: "cells", sheet: index2, ranges: [r] }];
24673
- if (all2.length) scopes.push({ kind: "parts", sheet: index2, parts: all2 });
24970
+ if (parts.length) scopes.push({ kind: "parts", sheet: index2, parts });
24674
24971
  return scopes;
24675
24972
  }
24676
24973
  function writeInput(ctx, sheet, row2, col, text3) {
@@ -24721,9 +25018,9 @@ function setCellInput(ctx, s, row2, col, text3) {
24721
25018
  `Typing in ${formatAddress({ row: row2, col })}`,
24722
25019
  "cells",
24723
25020
  writeScopes(sheet, s, range),
24724
- () => {
25021
+ (formulaCells) => {
24725
25022
  writeInput(ctx, sheet, row2, col, text3);
24726
- syncTableHeader(ctx, sheet, row2, col);
25023
+ syncTableHeader(ctx, sheet, row2, col, formulaCells);
24727
25024
  },
24728
25025
  { sheet: s, ranges: [range] }
24729
25026
  );
@@ -24735,9 +25032,9 @@ function setCellValue(ctx, s, row2, col, value) {
24735
25032
  `Edit ${formatAddress({ row: row2, col })}`,
24736
25033
  "cells",
24737
25034
  writeScopes(sheet, s, range),
24738
- () => {
25035
+ (formulaCells) => {
24739
25036
  writeValue(sheet, row2, col, value);
24740
- syncTableHeader(ctx, sheet, row2, col);
25037
+ syncTableHeader(ctx, sheet, row2, col, formulaCells);
24741
25038
  },
24742
25039
  { sheet: s, ranges: [range] }
24743
25040
  );
@@ -25164,7 +25461,7 @@ function fillRange(ctx, s, source2, target, mode = "auto") {
25164
25461
  ctx.run(
25165
25462
  "Fill",
25166
25463
  "cells",
25167
- [{ kind: "cells", sheet: s, ranges: [all2] }],
25464
+ writeScopes(sheet, s, all2),
25168
25465
  () => {
25169
25466
  const laneCount = vertical ? src.end.col - src.start.col + 1 : src.end.row - src.start.row + 1;
25170
25467
  const length = vertical ? src.end.row - src.start.row + 1 : src.end.col - src.start.col + 1;
@@ -25187,6 +25484,7 @@ function fillRange(ctx, s, source2, target, mode = "auto") {
25187
25484
  const step2 = series?.(k);
25188
25485
  const cell = !step2 || step2.kind === "copy" ? copyCell(origin, row2 - srcRow, col - srcCol) : { value: step2.value, ...origin.styleId ? { styleId: origin.styleId } : {} };
25189
25486
  putCell(sheet, row2, col, cell);
25487
+ syncTableHeader(ctx, sheet, row2, col);
25190
25488
  }
25191
25489
  }
25192
25490
  },
@@ -25225,28 +25523,6 @@ function removeValidationArea(rules, area) {
25225
25523
  });
25226
25524
  }
25227
25525
 
25228
- // xlsx/edit/conditional-formulas.ts
25229
- function rewriteConditionalRule(rule, rewrite2) {
25230
- const safe2 = (formula) => {
25231
- try {
25232
- return rewrite2(formula);
25233
- } catch {
25234
- return formula;
25235
- }
25236
- };
25237
- if (rule.type === "cellIs") rule.formulas = rule.formulas.map(safe2);
25238
- else if (rule.type === "expression") rule.formula = safe2(rule.formula);
25239
- if (rule.type === "colorScale" || rule.type === "iconSet") {
25240
- for (const threshold of rule.thresholds)
25241
- if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
25242
- }
25243
- if (rule.type === "dataBar") {
25244
- for (const threshold of [rule.min, rule.max])
25245
- if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
25246
- }
25247
- if (rule.type === "dataBar") rewriteConditionalExtension(rule, safe2);
25248
- }
25249
-
25250
25526
  // xlsx/edit/conditional-formats.ts
25251
25527
  function formatAt(sheet, index2) {
25252
25528
  const format = sheet.conditionalFormats[index2];
@@ -25656,168 +25932,6 @@ function setBorders(ctx, s, range, preset, edge2 = { style: "thin" }) {
25656
25932
  );
25657
25933
  }
25658
25934
 
25659
- // xlsx/edit/shift-preserved.ts
25660
- var XM3 = "http://schemas.microsoft.com/office/excel/2006/main";
25661
- function descendants3(root, ns, local) {
25662
- return Array.from(root.getElementsByTagNameNS(ns, local));
25663
- }
25664
- function editEntries(sheet, key, edit, quick) {
25665
- const list = sheet.preserved.get(key);
25666
- if (!list) return;
25667
- const next = [];
25668
- for (const xml of list) {
25669
- if (!quick(xml)) {
25670
- next.push(xml);
25671
- continue;
25672
- }
25673
- let doc;
25674
- try {
25675
- doc = parseXml(xml);
25676
- } catch {
25677
- next.push(xml);
25678
- continue;
25679
- }
25680
- const outcome = edit(doc.documentElement);
25681
- if (outcome === "same") next.push(xml);
25682
- else if (outcome === "changed") next.push(buildXml(doc));
25683
- }
25684
- if (next.length) sheet.preserved.set(key, next);
25685
- else sheet.preserved.delete(key);
25686
- }
25687
- function shiftBreaks(root, shift2) {
25688
- let changed = false;
25689
- for (const brk of elements(root).filter((e) => e.localName === "brk")) {
25690
- const id = Number(brk.getAttribute("id") ?? "0");
25691
- const moved = shiftIndex(id, shift2);
25692
- if (moved === id) continue;
25693
- changed = true;
25694
- if (moved === void 0 || moved === 0) root.removeChild(brk);
25695
- else brk.setAttribute("id", String(moved));
25696
- }
25697
- if (!changed) return "same";
25698
- const left2 = elements(root).filter((e) => e.localName === "brk");
25699
- if (!left2.length) return "drop";
25700
- if (root.hasAttribute("count")) root.setAttribute("count", String(left2.length));
25701
- if (root.hasAttribute("manualBreakCount")) {
25702
- const manual = left2.filter((b) => /^(1|true)$/.test(b.getAttribute("man") ?? "")).length;
25703
- root.setAttribute("manualBreakCount", String(manual));
25704
- }
25705
- return "changed";
25706
- }
25707
- function removeUpwards(node, root) {
25708
- let current = node;
25709
- while (current && current !== root) {
25710
- const parent = current.parentNode;
25711
- parent?.removeChild(current);
25712
- if (!parent) break;
25713
- const emptied = elements(parent).length === 0 || parent.localName === "sparklineGroup" && !elements(parent).some((e) => e.localName === "sparklines");
25714
- if (!emptied) break;
25715
- current = parent;
25716
- }
25717
- }
25718
- function shiftSqrefs(root, moveRange) {
25719
- let changed = false;
25720
- for (const sqref2 of descendants3(root, XM3, "sqref")) {
25721
- const old = sqref2.textContent ?? "";
25722
- const ranges = parseSqref(old).map(moveRange).filter((r) => !!r);
25723
- const text3 = ranges.map(formatRange).join(" ");
25724
- if (text3 === old.trim()) continue;
25725
- changed = true;
25726
- const owner = sqref2.parentNode;
25727
- if (ranges.length) sqref2.textContent = text3;
25728
- else if (owner) removeUpwards(owner, root);
25729
- }
25730
- if (!changed) return "same";
25731
- for (const list of elements(root).flatMap((ext) => elements(ext)))
25732
- if (list.localName === "dataValidations" && list.hasAttribute("count"))
25733
- list.setAttribute("count", String(elements(list).length));
25734
- return elements(root).length > 0 ? "changed" : "drop";
25735
- }
25736
- function shiftPreservedXml(sheet, shift2, moveRange, banded) {
25737
- if (!banded)
25738
- editEntries(
25739
- sheet,
25740
- shift2.axis === "row" ? "rowBreaks" : "colBreaks",
25741
- (root) => shiftBreaks(root, shift2),
25742
- (xml) => xml.includes("brk")
25743
- );
25744
- editEntries(
25745
- sheet,
25746
- "extLst",
25747
- (root) => shiftSqrefs(root, moveRange),
25748
- (xml) => xml.includes("sqref")
25749
- );
25750
- }
25751
- function rewritePreservedFormulas(sheet, rewrite2) {
25752
- editEntries(
25753
- sheet,
25754
- "extLst",
25755
- (root) => {
25756
- let changed = false;
25757
- for (const f6 of descendants3(root, XM3, "f")) {
25758
- const text3 = f6.textContent ?? "";
25759
- let next = text3;
25760
- try {
25761
- next = rewrite2(text3, sheet.name);
25762
- } catch {
25763
- next = text3;
25764
- }
25765
- if (next === text3) continue;
25766
- f6.textContent = next;
25767
- changed = true;
25768
- }
25769
- return changed ? "changed" : "same";
25770
- },
25771
- (xml) => xml.includes(":f>")
25772
- );
25773
- }
25774
-
25775
- // xlsx/edit/shift-formulas.ts
25776
- function safe(rewrite2, formula, formulaSheet) {
25777
- try {
25778
- return rewrite2(formula, formulaSheet);
25779
- } catch {
25780
- return formula;
25781
- }
25782
- }
25783
- function rewriteFormulas(workbook, rewrite2) {
25784
- for (const sheet of workbook.sheets) rewriteSheetFormulas(sheet, rewrite2);
25785
- for (const definedName of workbook.definedNames) {
25786
- const local = definedName.localSheet === void 0 ? void 0 : workbook.sheets[definedName.localSheet];
25787
- definedName.formula = safe(rewrite2, definedName.formula, local?.name ?? "");
25788
- }
25789
- }
25790
- function rewriteSheetFormulas(sheet, rewrite2) {
25791
- rewritePreservedFormulas(sheet, rewrite2);
25792
- {
25793
- const name = sheet.name;
25794
- for (const table2 of sheet.tables)
25795
- for (const column of table2.columns)
25796
- if (column.calculatedColumnFormula !== void 0)
25797
- column.calculatedColumnFormula = safe(rewrite2, column.calculatedColumnFormula, name);
25798
- for (const cells of sheet.rows.values())
25799
- for (const cell of cells.values())
25800
- if (cell.formula !== void 0) cell.formula = safe(rewrite2, cell.formula, name);
25801
- for (const cf of sheet.conditionalFormats)
25802
- for (const rule of cf.rules) rewriteConditionalRule(rule, (f6) => rewrite2(f6, name));
25803
- for (const dv of sheet.dataValidations) {
25804
- if (dv.formula1 !== void 0) dv.formula1 = safe(rewrite2, dv.formula1, name);
25805
- if (dv.formula2 !== void 0) dv.formula2 = safe(rewrite2, dv.formula2, name);
25806
- }
25807
- for (const link of sheet.hyperlinks)
25808
- if (link.location && /![A-Za-z$]/.test(link.location))
25809
- link.location = safe(rewrite2, link.location, name);
25810
- for (const drawing of sheet.drawings) {
25811
- if (drawing.kind !== "chart") continue;
25812
- for (const series of drawing.series) {
25813
- if (series.nameRef) series.nameRef = safe(rewrite2, series.nameRef, name);
25814
- if (series.categoriesRef) series.categoriesRef = safe(rewrite2, series.categoriesRef, name);
25815
- if (series.valuesRef) series.valuesRef = safe(rewrite2, series.valuesRef, name);
25816
- }
25817
- }
25818
- }
25819
- }
25820
-
25821
25935
  // xlsx/edit/shift-sheet.ts
25822
25936
  function shiftSheetContent(sheet, shift2, band) {
25823
25937
  const moveRange = (range) => band ? shiftRangeInBand(range, shift2, band.lo, band.hi) : shiftRange(range, shift2);
@@ -26807,14 +26921,13 @@ function pasteAt(ctx, s, at, payload, request) {
26807
26921
  if (cut && operation !== "none")
26808
26922
  throw new RangeError("Paste operations are not available for cut cells.");
26809
26923
  const destCells = { kind: "cells", sheet: s, ranges: [dest] };
26810
- const scopes = cut ? [{ kind: "refs" }, { kind: "cells", sheet: cut.sheet, ranges: [cut.range] }, destCells] : [
26811
- destCells,
26812
- {
26813
- kind: "parts",
26814
- sheet: s,
26815
- parts: ["merges", "comments", "dataValidations", "hyperlinks", "conditionalFormats"]
26816
- }
26817
- ];
26924
+ const scopes = cut ? [{ kind: "refs" }, { kind: "cells", sheet: cut.sheet, ranges: [cut.range] }, destCells] : writeScopes(sheet, s, dest, [
26925
+ "merges",
26926
+ "comments",
26927
+ "dataValidations",
26928
+ "hyperlinks",
26929
+ "conditionalFormats"
26930
+ ]);
26818
26931
  const move = cut && {
26819
26932
  fromSheet: sheetAt(workbook, cut.sheet).name,
26820
26933
  range: cut.range,
@@ -26887,6 +27000,8 @@ function pasteAt(ctx, s, at, payload, request) {
26887
27000
  const origin = cells.source?.range.start;
26888
27001
  const formula = clip?.formula === void 0 ? void 0 : move ? moveReferencesInFormula(clip.formula, move.fromSheet, move, move.toSheet) : !origin ? clip.formula : moveFormula(clip.formula, at.row - origin.row, at.col - origin.col);
26889
27002
  writeClip(workbook, sheet, row2, col, clip, mode, formula, styleOf2, operation);
27003
+ if (mode !== "formats" && mode !== "comments" && mode !== "validation")
27004
+ syncTableHeader(ctx, sheet, row2, col);
26890
27005
  }
26891
27006
  if (mode === "all" || mode === "mergeFormats" || mode === "noBorders" || mode === "formats") {
26892
27007
  sheet.merges = sheet.merges.filter((m) => !rangesIntersect(m, dest));
@@ -26930,14 +27045,13 @@ function pasteSelection(ctx, sheet, selection, payload, request) {
26930
27045
  return ctx.run(
26931
27046
  "Paste",
26932
27047
  "cells",
26933
- [
26934
- { kind: "cells", sheet, ranges: [dest] },
26935
- {
26936
- kind: "parts",
26937
- sheet,
26938
- parts: ["merges", "comments", "dataValidations", "hyperlinks", "conditionalFormats"]
26939
- }
26940
- ],
27048
+ writeScopes(sheetAt(ctx.workbook, sheet), sheet, dest, [
27049
+ "merges",
27050
+ "comments",
27051
+ "dataValidations",
27052
+ "hyperlinks",
27053
+ "conditionalFormats"
27054
+ ]),
26941
27055
  () => {
26942
27056
  for (let row2 = dest.start.row; row2 <= dest.end.row; row2 += height)
26943
27057
  for (let col = dest.start.col; col <= dest.end.col; col += width)
@@ -27089,6 +27203,15 @@ function setHidden(ctx, s, axis, indices, hidden) {
27089
27203
  }
27090
27204
  for (const row2 of indices) {
27091
27205
  const info = { ...sheet.rowInfo.get(row2) };
27206
+ if (info.filteredOut) {
27207
+ if (hidden) info.manuallyHidden = true;
27208
+ else delete info.manuallyHidden;
27209
+ sheet.rowInfo.set(row2, info);
27210
+ continue;
27211
+ }
27212
+ const filter2 = sheet.autoFilter;
27213
+ if (filter2?.columns?.length && row2 > filter2.range.start.row && row2 <= filter2.range.end.row)
27214
+ info.filteredOut = false;
27092
27215
  if (hidden) info.hidden = true;
27093
27216
  else {
27094
27217
  delete info.hidden;
@@ -27243,14 +27366,21 @@ function currentRegion(sheet, at) {
27243
27366
  function applyFilter(ctx, sheet, filter2) {
27244
27367
  const { range } = filter2;
27245
27368
  for (let row2 = range.start.row + 1; row2 <= range.end.row; row2++) {
27246
- const visible = (filter2.columns ?? []).every((fc) => {
27247
- if (!fc.values && !fc.blank) return true;
27248
- const text3 = displayText(ctx.workbook, getCell(sheet, row2, range.start.col + fc.offset));
27249
- return text3 === "" ? !!fc.blank : (fc.values ?? []).includes(text3);
27250
- });
27369
+ const visible = rowMatchesFilter(ctx.workbook, sheet, row2, filter2);
27251
27370
  const info = { ...sheet.rowInfo.get(row2) };
27252
- if (visible) delete info.hidden;
27253
- else info.hidden = true;
27371
+ const manual = info.manuallyHidden || info.hidden && !info.filteredOut;
27372
+ if (visible) {
27373
+ delete info.filteredOut;
27374
+ delete info.manuallyHidden;
27375
+ if (manual) {
27376
+ info.hidden = true;
27377
+ if (filter2.columns?.length) info.filteredOut = false;
27378
+ } else delete info.hidden;
27379
+ } else {
27380
+ info.hidden = true;
27381
+ info.filteredOut = true;
27382
+ if (manual) info.manuallyHidden = true;
27383
+ }
27254
27384
  if (Object.keys(info).length) sheet.rowInfo.set(row2, info);
27255
27385
  else sheet.rowInfo.delete(row2);
27256
27386
  }
@@ -27414,16 +27544,16 @@ var isSpilledAt = (workbook, at) => {
27414
27544
  return !!sheet && isSpilledCell(getCell(sheet, at.row, at.col));
27415
27545
  };
27416
27546
  var replaceQuery = (query) => query.lookIn === "comments" ? query : { ...query, lookIn: "formulas" };
27417
- function replaceScopes(query, sheet, matches2) {
27547
+ function replaceScopes(ctx, query, sheet, matches2) {
27418
27548
  if (query.lookIn === "comments") return [{ kind: "parts", sheet, parts: ["comments"] }];
27419
27549
  const ranges = matches2.map((m) => ({
27420
27550
  start: { row: m.row, col: m.col },
27421
27551
  end: { row: m.row, col: m.col }
27422
27552
  }));
27423
- return [
27424
- { kind: "cells", sheet, ranges },
27425
- { kind: "parts", sheet, parts: ["tables"] }
27426
- ];
27553
+ const header = ranges.some(
27554
+ (range) => writeScopes(sheetAt(ctx.workbook, sheet), sheet, range).some((scope) => scope.kind === "refs")
27555
+ );
27556
+ return [...header ? [{ kind: "refs" }] : [], { kind: "cells", sheet, ranges }];
27427
27557
  }
27428
27558
  function applyReplace(ctx, query, replacement, matches2) {
27429
27559
  let changed = 0;
@@ -27453,14 +27583,24 @@ function replaceAll(ctx, query, replacement) {
27453
27583
  const sheets = [...new Set(matches2.map((m) => m.sheet))];
27454
27584
  const scopes = sheets.flatMap(
27455
27585
  (sheet) => replaceScopes(
27586
+ ctx,
27456
27587
  q,
27457
27588
  sheet,
27458
27589
  matches2.filter((m) => m.sheet === sheet)
27459
27590
  )
27460
27591
  );
27461
- return ctx.run("Replace", "cells", scopes, () => applyReplace(ctx, q, replacement, matches2), {
27462
- ...sheets.length === 1 && sheets[0] !== void 0 ? { sheet: sheets[0] } : {}
27463
- });
27592
+ const uniqueScopes = scopes.filter(
27593
+ (scope, index2) => scope.kind !== "refs" || scopes.findIndex((entry3) => entry3.kind === "refs") === index2
27594
+ );
27595
+ return ctx.run(
27596
+ "Replace",
27597
+ "cells",
27598
+ uniqueScopes,
27599
+ () => applyReplace(ctx, q, replacement, matches2),
27600
+ {
27601
+ ...sheets.length === 1 && sheets[0] !== void 0 ? { sheet: sheets[0] } : {}
27602
+ }
27603
+ );
27464
27604
  }
27465
27605
  function replaceOne(ctx, query, replacement, at) {
27466
27606
  const q = replaceQuery(query);
@@ -27473,7 +27613,7 @@ function replaceOne(ctx, query, replacement, at) {
27473
27613
  ctx.run(
27474
27614
  "Replace",
27475
27615
  "cells",
27476
- replaceScopes(q, at.sheet, [match2]),
27616
+ replaceScopes(ctx, q, at.sheet, [match2]),
27477
27617
  () => applyReplace(ctx, q, replacement, [match2]),
27478
27618
  { sheet: at.sheet }
27479
27619
  );
@@ -28476,90 +28616,6 @@ function applyTotalsRow(sheet, table2) {
28476
28616
  });
28477
28617
  }
28478
28618
 
28479
- // xlsx/edit/tables.ts
28480
- function nextTableName(workbook) {
28481
- const taken = new Set(workbook.sheets.flatMap((s) => s.tables.map((t) => t.name.toLowerCase())));
28482
- const names = new Set(workbook.definedNames.map((n2) => n2.name.toLowerCase()));
28483
- for (let n2 = 1; ; n2++) if (!taken.has(`table${n2}`) && !names.has(`table${n2}`)) return `Table${n2}`;
28484
- }
28485
- function uniqueHeaders(texts) {
28486
- const used = /* @__PURE__ */ new Set();
28487
- return texts.map((text3, i2) => {
28488
- let base = text3.trim() || `Column${i2 + 1}`;
28489
- if (!used.has(base.toLowerCase())) {
28490
- used.add(base.toLowerCase());
28491
- return base;
28492
- }
28493
- for (let n2 = 2; ; n2++) {
28494
- const candidate = `${base}${n2}`;
28495
- if (!used.has(candidate.toLowerCase())) {
28496
- used.add(candidate.toLowerCase());
28497
- base = candidate;
28498
- return base;
28499
- }
28500
- }
28501
- });
28502
- }
28503
- function createTable(ctx, s, range, hasHeader, styleName = "TableStyleMedium2") {
28504
- const { workbook } = ctx;
28505
- const sheet = sheetAt(workbook, s);
28506
- let r = normalizeRange(range);
28507
- if (r.start.row === r.end.row && r.start.col === r.end.col) r = currentRegion(sheet, r.start);
28508
- if (sheet.tables.some((t) => rangesIntersect(t.range, r)))
28509
- throw new Error("A table cannot overlap another table.");
28510
- if (sheet.merges.some((m) => rangesIntersect(m, r)))
28511
- throw new Error("A table cannot contain merged cells.");
28512
- return ctx.run(
28513
- "Create table",
28514
- "structure",
28515
- [hasHeader ? { kind: "sheet", sheet: s } : { kind: "workbook" }],
28516
- () => {
28517
- if (!hasHeader) {
28518
- const shift2 = { axis: "row", at: r.start.row, count: 1 };
28519
- const band = { lo: r.start.col, hi: r.end.col };
28520
- rewriteFormulas(workbook, (f6, fs) => shiftFormulaInBand(f6, fs, sheet.name, shift2, band));
28521
- shiftSheetContent(sheet, shift2, band);
28522
- r = { start: r.start, end: { row: r.end.row + 1, col: r.end.col } };
28523
- }
28524
- const width = r.end.col - r.start.col + 1;
28525
- const texts = Array.from(
28526
- { length: width },
28527
- (_v, i2) => hasHeader ? displayText(workbook, getCell(sheet, r.start.row, r.start.col + i2)) : ""
28528
- );
28529
- const headers = uniqueHeaders(texts);
28530
- const columns = headers.map((name2, i2) => {
28531
- const cell = getCell(sheet, r.start.row, r.start.col + i2);
28532
- if (cell?.value !== name2) {
28533
- const styleId = cell?.styleId;
28534
- writeValue(sheet, r.start.row, r.start.col + i2, name2);
28535
- const written = getCell(sheet, r.start.row, r.start.col + i2);
28536
- if (written && styleId) written.styleId = styleId;
28537
- }
28538
- return { name: name2 };
28539
- });
28540
- const name = nextTableName(workbook);
28541
- const id = Math.max(0, ...workbook.sheets.flatMap((ws) => ws.tables.map((t) => t.id))) + 1;
28542
- const table2 = {
28543
- id,
28544
- name,
28545
- displayName: name,
28546
- range: r,
28547
- headerRow: true,
28548
- totalsRow: false,
28549
- columns,
28550
- styleName,
28551
- showRowStripes: true,
28552
- showColumnStripes: false,
28553
- showFirstColumn: false,
28554
- showLastColumn: false
28555
- };
28556
- sheet.tables.push(table2);
28557
- return table2;
28558
- },
28559
- { sheet: s, ranges: [r], structural: !hasHeader }
28560
- );
28561
- }
28562
-
28563
28619
  // xlsx/edit/table-edits.ts
28564
28620
  function tableIndex(sheet, ref) {
28565
28621
  const index2 = typeof ref === "number" ? ref : sheet.tables.findIndex((t) => t.name.toLowerCase() === ref.toLowerCase());
@@ -31815,8 +31871,8 @@ function setSheetState(ctx, index2, state) {
31815
31871
  const { workbook } = ctx;
31816
31872
  const sheet = sheetAt(workbook, index2);
31817
31873
  if (sheet.state === state) return;
31874
+ assertUnlocked(workbook);
31818
31875
  if (state !== "visible") {
31819
- assertUnlocked(workbook);
31820
31876
  if (sheet.state === "visible" && visibleCount(workbook) <= 1)
31821
31877
  throw new Error("A workbook must contain at least one visible sheet.");
31822
31878
  }
@@ -31846,6 +31902,72 @@ function setTabColor(ctx, index2, color) {
31846
31902
  );
31847
31903
  }
31848
31904
 
31905
+ // xlsx/edit/tables.ts
31906
+ function nextTableName(workbook) {
31907
+ const taken = new Set(workbook.sheets.flatMap((s) => s.tables.map((t) => t.name.toLowerCase())));
31908
+ const names = new Set(workbook.definedNames.map((n2) => n2.name.toLowerCase()));
31909
+ for (let n2 = 1; ; n2++) if (!taken.has(`table${n2}`) && !names.has(`table${n2}`)) return `Table${n2}`;
31910
+ }
31911
+ function createTable(ctx, s, range, hasHeader, styleName = "TableStyleMedium2") {
31912
+ const { workbook } = ctx;
31913
+ const sheet = sheetAt(workbook, s);
31914
+ let r = normalizeRange(range);
31915
+ if (r.start.row === r.end.row && r.start.col === r.end.col) r = currentRegion(sheet, r.start);
31916
+ if (sheet.tables.some((t) => rangesIntersect(t.range, r)))
31917
+ throw new Error("A table cannot overlap another table.");
31918
+ if (sheet.merges.some((m) => rangesIntersect(m, r)))
31919
+ throw new Error("A table cannot contain merged cells.");
31920
+ return ctx.run(
31921
+ "Create table",
31922
+ "structure",
31923
+ [hasHeader ? { kind: "sheet", sheet: s } : { kind: "workbook" }],
31924
+ () => {
31925
+ if (!hasHeader) {
31926
+ const shift2 = { axis: "row", at: r.start.row, count: 1 };
31927
+ const band = { lo: r.start.col, hi: r.end.col };
31928
+ rewriteFormulas(workbook, (f6, fs) => shiftFormulaInBand(f6, fs, sheet.name, shift2, band));
31929
+ shiftSheetContent(sheet, shift2, band);
31930
+ r = { start: r.start, end: { row: r.end.row + 1, col: r.end.col } };
31931
+ }
31932
+ const width = r.end.col - r.start.col + 1;
31933
+ const texts = Array.from(
31934
+ { length: width },
31935
+ (_v, i2) => hasHeader ? displayText(workbook, getCell(sheet, r.start.row, r.start.col + i2)) : ""
31936
+ );
31937
+ const headers = uniqueHeaders(texts);
31938
+ const columns = headers.map((name2, i2) => {
31939
+ const cell = getCell(sheet, r.start.row, r.start.col + i2);
31940
+ if (cell?.value !== name2) {
31941
+ const styleId = cell?.styleId;
31942
+ writeValue(sheet, r.start.row, r.start.col + i2, name2);
31943
+ const written = getCell(sheet, r.start.row, r.start.col + i2);
31944
+ if (written && styleId) written.styleId = styleId;
31945
+ }
31946
+ return { name: name2 };
31947
+ });
31948
+ const name = nextTableName(workbook);
31949
+ const id = Math.max(0, ...workbook.sheets.flatMap((ws) => ws.tables.map((t) => t.id))) + 1;
31950
+ const table2 = {
31951
+ id,
31952
+ name,
31953
+ displayName: name,
31954
+ range: r,
31955
+ headerRow: true,
31956
+ totalsRow: false,
31957
+ columns,
31958
+ styleName,
31959
+ showRowStripes: true,
31960
+ showColumnStripes: false,
31961
+ showFirstColumn: false,
31962
+ showLastColumn: false
31963
+ };
31964
+ sheet.tables.push(table2);
31965
+ return table2;
31966
+ },
31967
+ { sheet: s, ranges: [r], structural: !hasHeader }
31968
+ );
31969
+ }
31970
+
31849
31971
  // xlsx/edit/validation-list.ts
31850
31972
  function listSource(workbook, sheet, formula, evaluate, at, depth = 0) {
31851
31973
  const text3 = formula.replace(/^=/, "").trim();
@@ -32147,7 +32269,7 @@ function createEditSession(workbook, options = {}) {
32147
32269
  const befores = scopes.map((scope) => captureScope(workbook, scope));
32148
32270
  let result2;
32149
32271
  try {
32150
- result2 = fn();
32272
+ result2 = fn(befores.find((snapshot) => snapshot.kind === "refs")?.data.formulas);
32151
32273
  } catch (error) {
32152
32274
  for (let i2 = befores.length - 1; i2 >= 0; i2--) {
32153
32275
  const snapshot = befores[i2];