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
@@ -7815,6 +7815,39 @@ function liftCriteria(ctx, pairs, fn) {
7815
7815
  });
7816
7816
  }
7817
7817
 
7818
+ // xlsx/display-text.ts
7819
+ function displayText(workbook, cell) {
7820
+ if (!cell || cell.value === null) return "";
7821
+ const value = cell.value;
7822
+ if (isCellError(value)) return value.error;
7823
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
7824
+ if (cell.richText && typeof value === "string") return value;
7825
+ return formatValue(value, styleAt(workbook, cell.styleId).numFmt, {
7826
+ date1904: workbook.date1904
7827
+ }).text;
7828
+ }
7829
+
7830
+ // xlsx/filter-rows.ts
7831
+ function rowMatchesFilter(workbook, sheet, row2, filter2) {
7832
+ return (filter2.columns ?? []).every((column) => {
7833
+ if (!column.values && !column.blank) return true;
7834
+ const text3 = displayText(workbook, getCell(sheet, row2, filter2.range.start.col + column.offset));
7835
+ return text3 === "" ? !!column.blank : (column.values ?? []).includes(text3);
7836
+ });
7837
+ }
7838
+ function isFilteredRow(workbook, sheet, row2) {
7839
+ const info = sheet.rowInfo.get(row2);
7840
+ if (info?.filteredOut !== void 0) return info.filteredOut;
7841
+ const filter2 = sheet.autoFilter;
7842
+ return !!info?.hidden && !!filter2 && row2 > filter2.range.start.row && row2 <= filter2.range.end.row && !rowMatchesFilter(workbook, sheet, row2, filter2);
7843
+ }
7844
+ function readFilteredRows(workbook) {
7845
+ for (const sheet of workbook.sheets)
7846
+ for (const [row2, info] of sheet.rowInfo)
7847
+ if (sheet.autoFilter && row2 > sheet.autoFilter.range.start.row && row2 <= sheet.autoFilter.range.end.row && info.hidden)
7848
+ info.filteredOut = isFilteredRow(workbook, sheet, row2);
7849
+ }
7850
+
7818
7851
  // xlsx/formula/functions/math-sum.ts
7819
7852
  var C18 = "Math & Trig";
7820
7853
  var numbersOf = (values2) => values2.filter((v) => typeof v === "number");
@@ -7852,7 +7885,7 @@ function pairwise(ctx, args, fn) {
7852
7885
  return total2;
7853
7886
  }
7854
7887
  var SUBTOTAL_RE = /\b(?:_xlfn\.)?(?:SUBTOTAL|AGGREGATE)\s*\(/i;
7855
- function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true) {
7888
+ function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true, skipFiltered = false) {
7856
7889
  const out = [];
7857
7890
  const host = ctx.frame.host;
7858
7891
  for (const arg of args) {
@@ -7865,6 +7898,7 @@ function subtotalValues(ctx, args, skipHidden, skipErrors, skipNested = true) {
7865
7898
  for (const area of arg.areas) {
7866
7899
  const sheet = ctx.workbook.sheets[area.sheet];
7867
7900
  host.forEachStored(area.sheet, area.range, (value, row2, col) => {
7901
+ if (skipFiltered && sheet && isFilteredRow(ctx.workbook, sheet, row2)) return;
7868
7902
  if (skipHidden && sheet?.rowInfo.get(row2)?.hidden) return;
7869
7903
  if (skipNested && SUBTOTAL_RE.test(host.cellFormula(area.sheet, row2, col) ?? "")) return;
7870
7904
  if (skipErrors && isError(value)) return;
@@ -8030,7 +8064,7 @@ var SUM_FUNCTIONS = [
8030
8064
  const code = int(args[0]);
8031
8065
  const base = code > 100 ? code - 100 : code;
8032
8066
  if (base < 1 || base > 11) fail(ERR.VALUE);
8033
- return aggregate(base, subtotalValues(ctx, args.slice(1), code > 100, false));
8067
+ return aggregate(base, subtotalValues(ctx, args.slice(1), code > 100, false, true, true));
8034
8068
  },
8035
8069
  ["value", "any"]
8036
8070
  ),
@@ -13568,6 +13602,31 @@ function renameTableInFormula(formula, oldName, newName) {
13568
13602
  }
13569
13603
  return changed ? joinTokens(tokens2) : formula;
13570
13604
  }
13605
+ function renameTableColumnInFormula(formula, tableName, oldName, newName, insideTable = false) {
13606
+ const tokens2 = safeTokens2(formula);
13607
+ if (!tokens2) return formula;
13608
+ const escaped = newName.replace(/['#@[\]]/g, "'$&");
13609
+ let changed = false;
13610
+ for (const [index2, token] of tokens2.entries()) {
13611
+ if (token.kind !== "structured" || token.prefix) continue;
13612
+ if (token.value === "" && tokens2[index2 - 1]?.prefix) continue;
13613
+ if (token.value === "" ? !insideTable : String(token.value).toLowerCase() !== tableName.toLowerCase())
13614
+ continue;
13615
+ const next = token.text.replace(
13616
+ /\[(@?)((?:[^\[\]']|'[\s\S])*)\]/g,
13617
+ (original, at, name) => {
13618
+ if (name.startsWith("#") || name.replace(/'(.)/g, "$1").toLowerCase() !== oldName.toLowerCase())
13619
+ return original;
13620
+ return `[${at}${escaped}]`;
13621
+ }
13622
+ );
13623
+ if (next !== token.text) {
13624
+ token.text = next;
13625
+ changed = true;
13626
+ }
13627
+ }
13628
+ return changed ? joinTokens(tokens2) : formula;
13629
+ }
13571
13630
 
13572
13631
  // xlsx/formula/structured-to-a1.ts
13573
13632
  var abs = (row2, col, rowAbs = true) => `$${columnLabel(col)}${rowAbs ? "$" : ""}${row2 + 1}`;
@@ -20324,6 +20383,7 @@ async function loadXlsx(input) {
20324
20383
  if (!sheets.length) throw new Error("XLSX workbook has no sheets");
20325
20384
  await resolveSmartArt(sheets, source2, warn);
20326
20385
  liftPrintAreas(workbook);
20386
+ readFilteredRows(workbook);
20327
20387
  return workbook;
20328
20388
  }
20329
20389
  function readProperties(core, app, custom, warn) {
@@ -24626,16 +24686,6 @@ function forEachPosition(sheet, range, visit2) {
24626
24686
  for (let row2 = r.start.row; row2 <= r.end.row; row2++)
24627
24687
  for (let col = r.start.col; col <= r.end.col; col++) visit2(row2, col);
24628
24688
  }
24629
- function displayText(workbook, cell) {
24630
- if (!cell) return "";
24631
- const value = cell.value;
24632
- if (value === null) return "";
24633
- if (isCellError(value)) return value.error;
24634
- if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
24635
- if (cell.richText && typeof value === "string") return value;
24636
- const format = styleAt(workbook, cell.styleId).numFmt;
24637
- return formatValue(value, format, { date1904: workbook.date1904 }).text;
24638
- }
24639
24689
  function rawText(value) {
24640
24690
  if (value === null) return "";
24641
24691
  if (isCellError(value)) return value.error;
@@ -24652,19 +24702,266 @@ function pruneCell(sheet, row2, col) {
24652
24702
  }
24653
24703
  }
24654
24704
 
24705
+ // xlsx/edit/shift-preserved.ts
24706
+ var XM3 = "http://schemas.microsoft.com/office/excel/2006/main";
24707
+ function descendants3(root, ns, local) {
24708
+ return Array.from(root.getElementsByTagNameNS(ns, local));
24709
+ }
24710
+ function editEntries(sheet, key, edit, quick) {
24711
+ const list = sheet.preserved.get(key);
24712
+ if (!list) return;
24713
+ const next = [];
24714
+ for (const xml of list) {
24715
+ if (!quick(xml)) {
24716
+ next.push(xml);
24717
+ continue;
24718
+ }
24719
+ let doc;
24720
+ try {
24721
+ doc = parseXml(xml);
24722
+ } catch {
24723
+ next.push(xml);
24724
+ continue;
24725
+ }
24726
+ const outcome = edit(doc.documentElement);
24727
+ if (outcome === "same") next.push(xml);
24728
+ else if (outcome === "changed") next.push(buildXml(doc));
24729
+ }
24730
+ if (next.length) sheet.preserved.set(key, next);
24731
+ else sheet.preserved.delete(key);
24732
+ }
24733
+ function shiftBreaks(root, shift2) {
24734
+ let changed = false;
24735
+ for (const brk of elements(root).filter((e) => e.localName === "brk")) {
24736
+ const id = Number(brk.getAttribute("id") ?? "0");
24737
+ const moved = shiftIndex(id, shift2);
24738
+ if (moved === id) continue;
24739
+ changed = true;
24740
+ if (moved === void 0 || moved === 0) root.removeChild(brk);
24741
+ else brk.setAttribute("id", String(moved));
24742
+ }
24743
+ if (!changed) return "same";
24744
+ const left2 = elements(root).filter((e) => e.localName === "brk");
24745
+ if (!left2.length) return "drop";
24746
+ if (root.hasAttribute("count")) root.setAttribute("count", String(left2.length));
24747
+ if (root.hasAttribute("manualBreakCount")) {
24748
+ const manual = left2.filter((b) => /^(1|true)$/.test(b.getAttribute("man") ?? "")).length;
24749
+ root.setAttribute("manualBreakCount", String(manual));
24750
+ }
24751
+ return "changed";
24752
+ }
24753
+ function removeUpwards(node, root) {
24754
+ let current = node;
24755
+ while (current && current !== root) {
24756
+ const parent = current.parentNode;
24757
+ parent?.removeChild(current);
24758
+ if (!parent) break;
24759
+ const emptied = elements(parent).length === 0 || parent.localName === "sparklineGroup" && !elements(parent).some((e) => e.localName === "sparklines");
24760
+ if (!emptied) break;
24761
+ current = parent;
24762
+ }
24763
+ }
24764
+ function shiftSqrefs(root, moveRange) {
24765
+ let changed = false;
24766
+ for (const sqref2 of descendants3(root, XM3, "sqref")) {
24767
+ const old = sqref2.textContent ?? "";
24768
+ const ranges = parseSqref(old).map(moveRange).filter((r) => !!r);
24769
+ const text3 = ranges.map(formatRange).join(" ");
24770
+ if (text3 === old.trim()) continue;
24771
+ changed = true;
24772
+ const owner = sqref2.parentNode;
24773
+ if (ranges.length) sqref2.textContent = text3;
24774
+ else if (owner) removeUpwards(owner, root);
24775
+ }
24776
+ if (!changed) return "same";
24777
+ for (const list of elements(root).flatMap((ext) => elements(ext)))
24778
+ if (list.localName === "dataValidations" && list.hasAttribute("count"))
24779
+ list.setAttribute("count", String(elements(list).length));
24780
+ return elements(root).length > 0 ? "changed" : "drop";
24781
+ }
24782
+ function shiftPreservedXml(sheet, shift2, moveRange, banded) {
24783
+ if (!banded)
24784
+ editEntries(
24785
+ sheet,
24786
+ shift2.axis === "row" ? "rowBreaks" : "colBreaks",
24787
+ (root) => shiftBreaks(root, shift2),
24788
+ (xml) => xml.includes("brk")
24789
+ );
24790
+ editEntries(
24791
+ sheet,
24792
+ "extLst",
24793
+ (root) => shiftSqrefs(root, moveRange),
24794
+ (xml) => xml.includes("sqref")
24795
+ );
24796
+ }
24797
+ function rewritePreservedFormulas(sheet, rewrite2) {
24798
+ editEntries(
24799
+ sheet,
24800
+ "extLst",
24801
+ (root) => {
24802
+ let changed = false;
24803
+ for (const f6 of descendants3(root, XM3, "f")) {
24804
+ const text3 = f6.textContent ?? "";
24805
+ let next = text3;
24806
+ try {
24807
+ next = rewrite2(text3, sheet.name);
24808
+ } catch {
24809
+ next = text3;
24810
+ }
24811
+ if (next === text3) continue;
24812
+ f6.textContent = next;
24813
+ changed = true;
24814
+ }
24815
+ return changed ? "changed" : "same";
24816
+ },
24817
+ (xml) => xml.includes(":f>")
24818
+ );
24819
+ }
24820
+
24821
+ // xlsx/edit/conditional-formulas.ts
24822
+ function rewriteConditionalRule(rule, rewrite2) {
24823
+ const safe2 = (formula) => {
24824
+ try {
24825
+ return rewrite2(formula);
24826
+ } catch {
24827
+ return formula;
24828
+ }
24829
+ };
24830
+ if (rule.type === "cellIs") rule.formulas = rule.formulas.map(safe2);
24831
+ else if (rule.type === "expression") rule.formula = safe2(rule.formula);
24832
+ if (rule.type === "colorScale" || rule.type === "iconSet") {
24833
+ for (const threshold of rule.thresholds)
24834
+ if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
24835
+ }
24836
+ if (rule.type === "dataBar") {
24837
+ for (const threshold of [rule.min, rule.max])
24838
+ if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
24839
+ }
24840
+ if (rule.type === "dataBar") rewriteConditionalExtension(rule, safe2);
24841
+ }
24842
+
24843
+ // xlsx/edit/shift-formulas.ts
24844
+ function safe(rewrite2, formula, formulaSheet, at) {
24845
+ try {
24846
+ return rewrite2(formula, formulaSheet, at);
24847
+ } catch {
24848
+ return formula;
24849
+ }
24850
+ }
24851
+ function rewriteFormulas(workbook, rewrite2, formulaCells) {
24852
+ const bySheet = formulaCells && /* @__PURE__ */ new Map();
24853
+ if (bySheet)
24854
+ for (const cell of formulaCells ?? []) {
24855
+ const positions2 = bySheet.get(cell.sheet);
24856
+ if (positions2) positions2.push(cell);
24857
+ else bySheet.set(cell.sheet, [cell]);
24858
+ }
24859
+ workbook.sheets.forEach(
24860
+ (sheet, index2) => rewriteSheetFormulas(sheet, rewrite2, bySheet ? bySheet.get(index2) ?? [] : void 0)
24861
+ );
24862
+ for (const definedName of workbook.definedNames) {
24863
+ const local = definedName.localSheet === void 0 ? void 0 : workbook.sheets[definedName.localSheet];
24864
+ definedName.formula = safe(rewrite2, definedName.formula, local?.name ?? "");
24865
+ }
24866
+ }
24867
+ function rewriteSheetFormulas(sheet, rewrite2, formulaCells) {
24868
+ rewritePreservedFormulas(sheet, rewrite2);
24869
+ {
24870
+ const name = sheet.name;
24871
+ for (const table2 of sheet.tables)
24872
+ for (const [index2, column] of table2.columns.entries())
24873
+ if (column.calculatedColumnFormula !== void 0)
24874
+ column.calculatedColumnFormula = safe(rewrite2, column.calculatedColumnFormula, name, {
24875
+ row: table2.range.start.row + (table2.headerRow ? 1 : 0),
24876
+ col: table2.range.start.col + index2
24877
+ });
24878
+ if (formulaCells) {
24879
+ for (const at of formulaCells) {
24880
+ const cell = getCell(sheet, at.row, at.col);
24881
+ if (cell?.formula !== void 0) cell.formula = safe(rewrite2, cell.formula, name, at);
24882
+ }
24883
+ } else {
24884
+ sheet.rows.forEach(
24885
+ (cells, row2) => cells.forEach((cell, col) => {
24886
+ if (cell.formula !== void 0)
24887
+ cell.formula = safe(rewrite2, cell.formula, name, { row: row2, col });
24888
+ })
24889
+ );
24890
+ }
24891
+ for (const cf of sheet.conditionalFormats)
24892
+ for (const rule of cf.rules)
24893
+ rewriteConditionalRule(rule, (f6) => rewrite2(f6, name, cf.ranges[0]?.start));
24894
+ for (const dv of sheet.dataValidations) {
24895
+ if (dv.formula1 !== void 0)
24896
+ dv.formula1 = safe(rewrite2, dv.formula1, name, dv.ranges[0]?.start);
24897
+ if (dv.formula2 !== void 0)
24898
+ dv.formula2 = safe(rewrite2, dv.formula2, name, dv.ranges[0]?.start);
24899
+ }
24900
+ for (const link of sheet.hyperlinks)
24901
+ if (link.location && /![A-Za-z$]/.test(link.location))
24902
+ link.location = safe(rewrite2, link.location, name);
24903
+ for (const drawing of sheet.drawings) {
24904
+ if (drawing.kind !== "chart") continue;
24905
+ for (const series of drawing.series) {
24906
+ if (series.nameRef) series.nameRef = safe(rewrite2, series.nameRef, name);
24907
+ if (series.categoriesRef) series.categoriesRef = safe(rewrite2, series.categoriesRef, name);
24908
+ if (series.valuesRef) series.valuesRef = safe(rewrite2, series.valuesRef, name);
24909
+ }
24910
+ }
24911
+ }
24912
+ }
24913
+
24914
+ // xlsx/edit/table-header-names.ts
24915
+ function uniqueHeaders(texts) {
24916
+ const used = /* @__PURE__ */ new Set();
24917
+ return texts.map((text3, i2) => {
24918
+ let base = text3.trim() || `Column${i2 + 1}`;
24919
+ if (!used.has(base.toLowerCase())) {
24920
+ used.add(base.toLowerCase());
24921
+ return base;
24922
+ }
24923
+ for (let n2 = 2; ; n2++) {
24924
+ const candidate = `${base}${n2}`;
24925
+ if (!used.has(candidate.toLowerCase())) {
24926
+ used.add(candidate.toLowerCase());
24927
+ base = candidate;
24928
+ return base;
24929
+ }
24930
+ }
24931
+ });
24932
+ }
24933
+
24655
24934
  // xlsx/edit/cell-values.ts
24656
24935
  function tableHeaderAt(sheet, row2, col) {
24657
24936
  return sheet.tables.find(
24658
24937
  (t) => t.headerRow && t.range.start.row === row2 && col >= t.range.start.col && col <= t.range.end.col
24659
24938
  );
24660
24939
  }
24661
- function syncTableHeader(ctx, sheet, row2, col) {
24940
+ function syncTableHeader(ctx, sheet, row2, col, formulaCells) {
24662
24941
  const table2 = tableHeaderAt(sheet, row2, col);
24663
24942
  if (!table2) return;
24664
24943
  const column = table2.columns[col - table2.range.start.col];
24665
24944
  if (!column) return;
24666
24945
  const text3 = displayText(ctx.workbook, getCell(sheet, row2, col)).trim();
24667
- if (text3) column.name = text3;
24946
+ if (!text3 || column.name === text3) return;
24947
+ const oldName = column.name;
24948
+ const others = table2.columns.filter((entry3) => entry3 !== column).map((entry3) => entry3.name);
24949
+ const newName = uniqueHeaders([...others, text3]).at(-1);
24950
+ column.name = newName;
24951
+ if (newName !== text3) writeValue(sheet, row2, col, newName);
24952
+ const index2 = ctx.workbook.sheets.indexOf(sheet);
24953
+ const cells = formulaCells && (formulaCells.some((cell) => cell.sheet === index2 && cell.row === row2 && cell.col === col) ? formulaCells : [...formulaCells, { sheet: index2, row: row2, col }]);
24954
+ rewriteFormulas(
24955
+ ctx.workbook,
24956
+ (formula, formulaSheet, at) => renameTableColumnInFormula(
24957
+ formula,
24958
+ table2.name,
24959
+ oldName,
24960
+ newName,
24961
+ formulaSheet === sheet.name && !!at && rangeContains(table2.range, at)
24962
+ ),
24963
+ cells
24964
+ );
24668
24965
  }
24669
24966
  function writeScopes(sheet, index2, range, parts = []) {
24670
24967
  const r = normalizeRange(range);
@@ -24674,9 +24971,9 @@ function writeScopes(sheet, index2, range, parts = []) {
24674
24971
  cellRange(t.range.start.row, t.range.start.col, t.range.start.row, t.range.end.col)
24675
24972
  )
24676
24973
  );
24677
- const all2 = touchesHeader && !parts.includes("tables") ? [...parts, "tables"] : parts;
24974
+ if (touchesHeader) return [{ kind: "refs" }, { kind: "cells", sheet: index2, ranges: [r] }];
24678
24975
  const scopes = [{ kind: "cells", sheet: index2, ranges: [r] }];
24679
- if (all2.length) scopes.push({ kind: "parts", sheet: index2, parts: all2 });
24976
+ if (parts.length) scopes.push({ kind: "parts", sheet: index2, parts });
24680
24977
  return scopes;
24681
24978
  }
24682
24979
  function writeInput(ctx, sheet, row2, col, text3) {
@@ -24727,9 +25024,9 @@ function setCellInput(ctx, s, row2, col, text3) {
24727
25024
  `Typing in ${formatAddress({ row: row2, col })}`,
24728
25025
  "cells",
24729
25026
  writeScopes(sheet, s, range),
24730
- () => {
25027
+ (formulaCells) => {
24731
25028
  writeInput(ctx, sheet, row2, col, text3);
24732
- syncTableHeader(ctx, sheet, row2, col);
25029
+ syncTableHeader(ctx, sheet, row2, col, formulaCells);
24733
25030
  },
24734
25031
  { sheet: s, ranges: [range] }
24735
25032
  );
@@ -24741,9 +25038,9 @@ function setCellValue(ctx, s, row2, col, value) {
24741
25038
  `Edit ${formatAddress({ row: row2, col })}`,
24742
25039
  "cells",
24743
25040
  writeScopes(sheet, s, range),
24744
- () => {
25041
+ (formulaCells) => {
24745
25042
  writeValue(sheet, row2, col, value);
24746
- syncTableHeader(ctx, sheet, row2, col);
25043
+ syncTableHeader(ctx, sheet, row2, col, formulaCells);
24747
25044
  },
24748
25045
  { sheet: s, ranges: [range] }
24749
25046
  );
@@ -25170,7 +25467,7 @@ function fillRange(ctx, s, source2, target, mode = "auto") {
25170
25467
  ctx.run(
25171
25468
  "Fill",
25172
25469
  "cells",
25173
- [{ kind: "cells", sheet: s, ranges: [all2] }],
25470
+ writeScopes(sheet, s, all2),
25174
25471
  () => {
25175
25472
  const laneCount = vertical ? src.end.col - src.start.col + 1 : src.end.row - src.start.row + 1;
25176
25473
  const length = vertical ? src.end.row - src.start.row + 1 : src.end.col - src.start.col + 1;
@@ -25193,6 +25490,7 @@ function fillRange(ctx, s, source2, target, mode = "auto") {
25193
25490
  const step2 = series?.(k);
25194
25491
  const cell = !step2 || step2.kind === "copy" ? copyCell(origin, row2 - srcRow, col - srcCol) : { value: step2.value, ...origin.styleId ? { styleId: origin.styleId } : {} };
25195
25492
  putCell(sheet, row2, col, cell);
25493
+ syncTableHeader(ctx, sheet, row2, col);
25196
25494
  }
25197
25495
  }
25198
25496
  },
@@ -25231,28 +25529,6 @@ function removeValidationArea(rules, area) {
25231
25529
  });
25232
25530
  }
25233
25531
 
25234
- // xlsx/edit/conditional-formulas.ts
25235
- function rewriteConditionalRule(rule, rewrite2) {
25236
- const safe2 = (formula) => {
25237
- try {
25238
- return rewrite2(formula);
25239
- } catch {
25240
- return formula;
25241
- }
25242
- };
25243
- if (rule.type === "cellIs") rule.formulas = rule.formulas.map(safe2);
25244
- else if (rule.type === "expression") rule.formula = safe2(rule.formula);
25245
- if (rule.type === "colorScale" || rule.type === "iconSet") {
25246
- for (const threshold of rule.thresholds)
25247
- if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
25248
- }
25249
- if (rule.type === "dataBar") {
25250
- for (const threshold of [rule.min, rule.max])
25251
- if (threshold.type === "formula" && threshold.value) threshold.value = safe2(threshold.value);
25252
- }
25253
- if (rule.type === "dataBar") rewriteConditionalExtension(rule, safe2);
25254
- }
25255
-
25256
25532
  // xlsx/edit/conditional-formats.ts
25257
25533
  function formatAt(sheet, index2) {
25258
25534
  const format = sheet.conditionalFormats[index2];
@@ -25662,168 +25938,6 @@ function setBorders(ctx, s, range, preset, edge2 = { style: "thin" }) {
25662
25938
  );
25663
25939
  }
25664
25940
 
25665
- // xlsx/edit/shift-preserved.ts
25666
- var XM3 = "http://schemas.microsoft.com/office/excel/2006/main";
25667
- function descendants3(root, ns, local) {
25668
- return Array.from(root.getElementsByTagNameNS(ns, local));
25669
- }
25670
- function editEntries(sheet, key, edit, quick) {
25671
- const list = sheet.preserved.get(key);
25672
- if (!list) return;
25673
- const next = [];
25674
- for (const xml of list) {
25675
- if (!quick(xml)) {
25676
- next.push(xml);
25677
- continue;
25678
- }
25679
- let doc;
25680
- try {
25681
- doc = parseXml(xml);
25682
- } catch {
25683
- next.push(xml);
25684
- continue;
25685
- }
25686
- const outcome = edit(doc.documentElement);
25687
- if (outcome === "same") next.push(xml);
25688
- else if (outcome === "changed") next.push(buildXml(doc));
25689
- }
25690
- if (next.length) sheet.preserved.set(key, next);
25691
- else sheet.preserved.delete(key);
25692
- }
25693
- function shiftBreaks(root, shift2) {
25694
- let changed = false;
25695
- for (const brk of elements(root).filter((e) => e.localName === "brk")) {
25696
- const id = Number(brk.getAttribute("id") ?? "0");
25697
- const moved = shiftIndex(id, shift2);
25698
- if (moved === id) continue;
25699
- changed = true;
25700
- if (moved === void 0 || moved === 0) root.removeChild(brk);
25701
- else brk.setAttribute("id", String(moved));
25702
- }
25703
- if (!changed) return "same";
25704
- const left2 = elements(root).filter((e) => e.localName === "brk");
25705
- if (!left2.length) return "drop";
25706
- if (root.hasAttribute("count")) root.setAttribute("count", String(left2.length));
25707
- if (root.hasAttribute("manualBreakCount")) {
25708
- const manual = left2.filter((b) => /^(1|true)$/.test(b.getAttribute("man") ?? "")).length;
25709
- root.setAttribute("manualBreakCount", String(manual));
25710
- }
25711
- return "changed";
25712
- }
25713
- function removeUpwards(node, root) {
25714
- let current = node;
25715
- while (current && current !== root) {
25716
- const parent = current.parentNode;
25717
- parent?.removeChild(current);
25718
- if (!parent) break;
25719
- const emptied = elements(parent).length === 0 || parent.localName === "sparklineGroup" && !elements(parent).some((e) => e.localName === "sparklines");
25720
- if (!emptied) break;
25721
- current = parent;
25722
- }
25723
- }
25724
- function shiftSqrefs(root, moveRange) {
25725
- let changed = false;
25726
- for (const sqref2 of descendants3(root, XM3, "sqref")) {
25727
- const old = sqref2.textContent ?? "";
25728
- const ranges = parseSqref(old).map(moveRange).filter((r) => !!r);
25729
- const text3 = ranges.map(formatRange).join(" ");
25730
- if (text3 === old.trim()) continue;
25731
- changed = true;
25732
- const owner = sqref2.parentNode;
25733
- if (ranges.length) sqref2.textContent = text3;
25734
- else if (owner) removeUpwards(owner, root);
25735
- }
25736
- if (!changed) return "same";
25737
- for (const list of elements(root).flatMap((ext) => elements(ext)))
25738
- if (list.localName === "dataValidations" && list.hasAttribute("count"))
25739
- list.setAttribute("count", String(elements(list).length));
25740
- return elements(root).length > 0 ? "changed" : "drop";
25741
- }
25742
- function shiftPreservedXml(sheet, shift2, moveRange, banded) {
25743
- if (!banded)
25744
- editEntries(
25745
- sheet,
25746
- shift2.axis === "row" ? "rowBreaks" : "colBreaks",
25747
- (root) => shiftBreaks(root, shift2),
25748
- (xml) => xml.includes("brk")
25749
- );
25750
- editEntries(
25751
- sheet,
25752
- "extLst",
25753
- (root) => shiftSqrefs(root, moveRange),
25754
- (xml) => xml.includes("sqref")
25755
- );
25756
- }
25757
- function rewritePreservedFormulas(sheet, rewrite2) {
25758
- editEntries(
25759
- sheet,
25760
- "extLst",
25761
- (root) => {
25762
- let changed = false;
25763
- for (const f6 of descendants3(root, XM3, "f")) {
25764
- const text3 = f6.textContent ?? "";
25765
- let next = text3;
25766
- try {
25767
- next = rewrite2(text3, sheet.name);
25768
- } catch {
25769
- next = text3;
25770
- }
25771
- if (next === text3) continue;
25772
- f6.textContent = next;
25773
- changed = true;
25774
- }
25775
- return changed ? "changed" : "same";
25776
- },
25777
- (xml) => xml.includes(":f>")
25778
- );
25779
- }
25780
-
25781
- // xlsx/edit/shift-formulas.ts
25782
- function safe(rewrite2, formula, formulaSheet) {
25783
- try {
25784
- return rewrite2(formula, formulaSheet);
25785
- } catch {
25786
- return formula;
25787
- }
25788
- }
25789
- function rewriteFormulas(workbook, rewrite2) {
25790
- for (const sheet of workbook.sheets) rewriteSheetFormulas(sheet, rewrite2);
25791
- for (const definedName of workbook.definedNames) {
25792
- const local = definedName.localSheet === void 0 ? void 0 : workbook.sheets[definedName.localSheet];
25793
- definedName.formula = safe(rewrite2, definedName.formula, local?.name ?? "");
25794
- }
25795
- }
25796
- function rewriteSheetFormulas(sheet, rewrite2) {
25797
- rewritePreservedFormulas(sheet, rewrite2);
25798
- {
25799
- const name = sheet.name;
25800
- for (const table2 of sheet.tables)
25801
- for (const column of table2.columns)
25802
- if (column.calculatedColumnFormula !== void 0)
25803
- column.calculatedColumnFormula = safe(rewrite2, column.calculatedColumnFormula, name);
25804
- for (const cells of sheet.rows.values())
25805
- for (const cell of cells.values())
25806
- if (cell.formula !== void 0) cell.formula = safe(rewrite2, cell.formula, name);
25807
- for (const cf of sheet.conditionalFormats)
25808
- for (const rule of cf.rules) rewriteConditionalRule(rule, (f6) => rewrite2(f6, name));
25809
- for (const dv of sheet.dataValidations) {
25810
- if (dv.formula1 !== void 0) dv.formula1 = safe(rewrite2, dv.formula1, name);
25811
- if (dv.formula2 !== void 0) dv.formula2 = safe(rewrite2, dv.formula2, name);
25812
- }
25813
- for (const link of sheet.hyperlinks)
25814
- if (link.location && /![A-Za-z$]/.test(link.location))
25815
- link.location = safe(rewrite2, link.location, name);
25816
- for (const drawing of sheet.drawings) {
25817
- if (drawing.kind !== "chart") continue;
25818
- for (const series of drawing.series) {
25819
- if (series.nameRef) series.nameRef = safe(rewrite2, series.nameRef, name);
25820
- if (series.categoriesRef) series.categoriesRef = safe(rewrite2, series.categoriesRef, name);
25821
- if (series.valuesRef) series.valuesRef = safe(rewrite2, series.valuesRef, name);
25822
- }
25823
- }
25824
- }
25825
- }
25826
-
25827
25941
  // xlsx/edit/shift-sheet.ts
25828
25942
  function shiftSheetContent(sheet, shift2, band) {
25829
25943
  const moveRange = (range) => band ? shiftRangeInBand(range, shift2, band.lo, band.hi) : shiftRange(range, shift2);
@@ -26813,14 +26927,13 @@ function pasteAt(ctx, s, at, payload, request) {
26813
26927
  if (cut && operation !== "none")
26814
26928
  throw new RangeError("Paste operations are not available for cut cells.");
26815
26929
  const destCells = { kind: "cells", sheet: s, ranges: [dest] };
26816
- const scopes = cut ? [{ kind: "refs" }, { kind: "cells", sheet: cut.sheet, ranges: [cut.range] }, destCells] : [
26817
- destCells,
26818
- {
26819
- kind: "parts",
26820
- sheet: s,
26821
- parts: ["merges", "comments", "dataValidations", "hyperlinks", "conditionalFormats"]
26822
- }
26823
- ];
26930
+ const scopes = cut ? [{ kind: "refs" }, { kind: "cells", sheet: cut.sheet, ranges: [cut.range] }, destCells] : writeScopes(sheet, s, dest, [
26931
+ "merges",
26932
+ "comments",
26933
+ "dataValidations",
26934
+ "hyperlinks",
26935
+ "conditionalFormats"
26936
+ ]);
26824
26937
  const move = cut && {
26825
26938
  fromSheet: sheetAt(workbook, cut.sheet).name,
26826
26939
  range: cut.range,
@@ -26893,6 +27006,8 @@ function pasteAt(ctx, s, at, payload, request) {
26893
27006
  const origin = cells.source?.range.start;
26894
27007
  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);
26895
27008
  writeClip(workbook, sheet, row2, col, clip, mode, formula, styleOf2, operation);
27009
+ if (mode !== "formats" && mode !== "comments" && mode !== "validation")
27010
+ syncTableHeader(ctx, sheet, row2, col);
26896
27011
  }
26897
27012
  if (mode === "all" || mode === "mergeFormats" || mode === "noBorders" || mode === "formats") {
26898
27013
  sheet.merges = sheet.merges.filter((m) => !rangesIntersect(m, dest));
@@ -26936,14 +27051,13 @@ function pasteSelection(ctx, sheet, selection, payload, request) {
26936
27051
  return ctx.run(
26937
27052
  "Paste",
26938
27053
  "cells",
26939
- [
26940
- { kind: "cells", sheet, ranges: [dest] },
26941
- {
26942
- kind: "parts",
26943
- sheet,
26944
- parts: ["merges", "comments", "dataValidations", "hyperlinks", "conditionalFormats"]
26945
- }
26946
- ],
27054
+ writeScopes(sheetAt(ctx.workbook, sheet), sheet, dest, [
27055
+ "merges",
27056
+ "comments",
27057
+ "dataValidations",
27058
+ "hyperlinks",
27059
+ "conditionalFormats"
27060
+ ]),
26947
27061
  () => {
26948
27062
  for (let row2 = dest.start.row; row2 <= dest.end.row; row2 += height)
26949
27063
  for (let col = dest.start.col; col <= dest.end.col; col += width)
@@ -27095,6 +27209,15 @@ function setHidden(ctx, s, axis, indices, hidden) {
27095
27209
  }
27096
27210
  for (const row2 of indices) {
27097
27211
  const info = { ...sheet.rowInfo.get(row2) };
27212
+ if (info.filteredOut) {
27213
+ if (hidden) info.manuallyHidden = true;
27214
+ else delete info.manuallyHidden;
27215
+ sheet.rowInfo.set(row2, info);
27216
+ continue;
27217
+ }
27218
+ const filter2 = sheet.autoFilter;
27219
+ if (filter2?.columns?.length && row2 > filter2.range.start.row && row2 <= filter2.range.end.row)
27220
+ info.filteredOut = false;
27098
27221
  if (hidden) info.hidden = true;
27099
27222
  else {
27100
27223
  delete info.hidden;
@@ -27249,14 +27372,21 @@ function currentRegion(sheet, at) {
27249
27372
  function applyFilter(ctx, sheet, filter2) {
27250
27373
  const { range } = filter2;
27251
27374
  for (let row2 = range.start.row + 1; row2 <= range.end.row; row2++) {
27252
- const visible = (filter2.columns ?? []).every((fc) => {
27253
- if (!fc.values && !fc.blank) return true;
27254
- const text3 = displayText(ctx.workbook, getCell(sheet, row2, range.start.col + fc.offset));
27255
- return text3 === "" ? !!fc.blank : (fc.values ?? []).includes(text3);
27256
- });
27375
+ const visible = rowMatchesFilter(ctx.workbook, sheet, row2, filter2);
27257
27376
  const info = { ...sheet.rowInfo.get(row2) };
27258
- if (visible) delete info.hidden;
27259
- else info.hidden = true;
27377
+ const manual = info.manuallyHidden || info.hidden && !info.filteredOut;
27378
+ if (visible) {
27379
+ delete info.filteredOut;
27380
+ delete info.manuallyHidden;
27381
+ if (manual) {
27382
+ info.hidden = true;
27383
+ if (filter2.columns?.length) info.filteredOut = false;
27384
+ } else delete info.hidden;
27385
+ } else {
27386
+ info.hidden = true;
27387
+ info.filteredOut = true;
27388
+ if (manual) info.manuallyHidden = true;
27389
+ }
27260
27390
  if (Object.keys(info).length) sheet.rowInfo.set(row2, info);
27261
27391
  else sheet.rowInfo.delete(row2);
27262
27392
  }
@@ -27420,16 +27550,16 @@ var isSpilledAt = (workbook, at) => {
27420
27550
  return !!sheet && isSpilledCell(getCell(sheet, at.row, at.col));
27421
27551
  };
27422
27552
  var replaceQuery = (query) => query.lookIn === "comments" ? query : { ...query, lookIn: "formulas" };
27423
- function replaceScopes(query, sheet, matches2) {
27553
+ function replaceScopes(ctx, query, sheet, matches2) {
27424
27554
  if (query.lookIn === "comments") return [{ kind: "parts", sheet, parts: ["comments"] }];
27425
27555
  const ranges = matches2.map((m) => ({
27426
27556
  start: { row: m.row, col: m.col },
27427
27557
  end: { row: m.row, col: m.col }
27428
27558
  }));
27429
- return [
27430
- { kind: "cells", sheet, ranges },
27431
- { kind: "parts", sheet, parts: ["tables"] }
27432
- ];
27559
+ const header = ranges.some(
27560
+ (range) => writeScopes(sheetAt(ctx.workbook, sheet), sheet, range).some((scope) => scope.kind === "refs")
27561
+ );
27562
+ return [...header ? [{ kind: "refs" }] : [], { kind: "cells", sheet, ranges }];
27433
27563
  }
27434
27564
  function applyReplace(ctx, query, replacement, matches2) {
27435
27565
  let changed = 0;
@@ -27459,14 +27589,24 @@ function replaceAll(ctx, query, replacement) {
27459
27589
  const sheets = [...new Set(matches2.map((m) => m.sheet))];
27460
27590
  const scopes = sheets.flatMap(
27461
27591
  (sheet) => replaceScopes(
27592
+ ctx,
27462
27593
  q,
27463
27594
  sheet,
27464
27595
  matches2.filter((m) => m.sheet === sheet)
27465
27596
  )
27466
27597
  );
27467
- return ctx.run("Replace", "cells", scopes, () => applyReplace(ctx, q, replacement, matches2), {
27468
- ...sheets.length === 1 && sheets[0] !== void 0 ? { sheet: sheets[0] } : {}
27469
- });
27598
+ const uniqueScopes = scopes.filter(
27599
+ (scope, index2) => scope.kind !== "refs" || scopes.findIndex((entry3) => entry3.kind === "refs") === index2
27600
+ );
27601
+ return ctx.run(
27602
+ "Replace",
27603
+ "cells",
27604
+ uniqueScopes,
27605
+ () => applyReplace(ctx, q, replacement, matches2),
27606
+ {
27607
+ ...sheets.length === 1 && sheets[0] !== void 0 ? { sheet: sheets[0] } : {}
27608
+ }
27609
+ );
27470
27610
  }
27471
27611
  function replaceOne(ctx, query, replacement, at) {
27472
27612
  const q = replaceQuery(query);
@@ -27479,7 +27619,7 @@ function replaceOne(ctx, query, replacement, at) {
27479
27619
  ctx.run(
27480
27620
  "Replace",
27481
27621
  "cells",
27482
- replaceScopes(q, at.sheet, [match2]),
27622
+ replaceScopes(ctx, q, at.sheet, [match2]),
27483
27623
  () => applyReplace(ctx, q, replacement, [match2]),
27484
27624
  { sheet: at.sheet }
27485
27625
  );
@@ -28482,90 +28622,6 @@ function applyTotalsRow(sheet, table2) {
28482
28622
  });
28483
28623
  }
28484
28624
 
28485
- // xlsx/edit/tables.ts
28486
- function nextTableName(workbook) {
28487
- const taken = new Set(workbook.sheets.flatMap((s) => s.tables.map((t) => t.name.toLowerCase())));
28488
- const names = new Set(workbook.definedNames.map((n2) => n2.name.toLowerCase()));
28489
- for (let n2 = 1; ; n2++) if (!taken.has(`table${n2}`) && !names.has(`table${n2}`)) return `Table${n2}`;
28490
- }
28491
- function uniqueHeaders(texts) {
28492
- const used = /* @__PURE__ */ new Set();
28493
- return texts.map((text3, i2) => {
28494
- let base = text3.trim() || `Column${i2 + 1}`;
28495
- if (!used.has(base.toLowerCase())) {
28496
- used.add(base.toLowerCase());
28497
- return base;
28498
- }
28499
- for (let n2 = 2; ; n2++) {
28500
- const candidate = `${base}${n2}`;
28501
- if (!used.has(candidate.toLowerCase())) {
28502
- used.add(candidate.toLowerCase());
28503
- base = candidate;
28504
- return base;
28505
- }
28506
- }
28507
- });
28508
- }
28509
- function createTable(ctx, s, range, hasHeader, styleName = "TableStyleMedium2") {
28510
- const { workbook } = ctx;
28511
- const sheet = sheetAt(workbook, s);
28512
- let r = normalizeRange(range);
28513
- if (r.start.row === r.end.row && r.start.col === r.end.col) r = currentRegion(sheet, r.start);
28514
- if (sheet.tables.some((t) => rangesIntersect(t.range, r)))
28515
- throw new Error("A table cannot overlap another table.");
28516
- if (sheet.merges.some((m) => rangesIntersect(m, r)))
28517
- throw new Error("A table cannot contain merged cells.");
28518
- return ctx.run(
28519
- "Create table",
28520
- "structure",
28521
- [hasHeader ? { kind: "sheet", sheet: s } : { kind: "workbook" }],
28522
- () => {
28523
- if (!hasHeader) {
28524
- const shift2 = { axis: "row", at: r.start.row, count: 1 };
28525
- const band = { lo: r.start.col, hi: r.end.col };
28526
- rewriteFormulas(workbook, (f6, fs) => shiftFormulaInBand(f6, fs, sheet.name, shift2, band));
28527
- shiftSheetContent(sheet, shift2, band);
28528
- r = { start: r.start, end: { row: r.end.row + 1, col: r.end.col } };
28529
- }
28530
- const width = r.end.col - r.start.col + 1;
28531
- const texts = Array.from(
28532
- { length: width },
28533
- (_v, i2) => hasHeader ? displayText(workbook, getCell(sheet, r.start.row, r.start.col + i2)) : ""
28534
- );
28535
- const headers = uniqueHeaders(texts);
28536
- const columns = headers.map((name2, i2) => {
28537
- const cell = getCell(sheet, r.start.row, r.start.col + i2);
28538
- if (cell?.value !== name2) {
28539
- const styleId = cell?.styleId;
28540
- writeValue(sheet, r.start.row, r.start.col + i2, name2);
28541
- const written = getCell(sheet, r.start.row, r.start.col + i2);
28542
- if (written && styleId) written.styleId = styleId;
28543
- }
28544
- return { name: name2 };
28545
- });
28546
- const name = nextTableName(workbook);
28547
- const id = Math.max(0, ...workbook.sheets.flatMap((ws) => ws.tables.map((t) => t.id))) + 1;
28548
- const table2 = {
28549
- id,
28550
- name,
28551
- displayName: name,
28552
- range: r,
28553
- headerRow: true,
28554
- totalsRow: false,
28555
- columns,
28556
- styleName,
28557
- showRowStripes: true,
28558
- showColumnStripes: false,
28559
- showFirstColumn: false,
28560
- showLastColumn: false
28561
- };
28562
- sheet.tables.push(table2);
28563
- return table2;
28564
- },
28565
- { sheet: s, ranges: [r], structural: !hasHeader }
28566
- );
28567
- }
28568
-
28569
28625
  // xlsx/edit/table-edits.ts
28570
28626
  function tableIndex(sheet, ref) {
28571
28627
  const index2 = typeof ref === "number" ? ref : sheet.tables.findIndex((t) => t.name.toLowerCase() === ref.toLowerCase());
@@ -31821,8 +31877,8 @@ function setSheetState(ctx, index2, state) {
31821
31877
  const { workbook } = ctx;
31822
31878
  const sheet = sheetAt(workbook, index2);
31823
31879
  if (sheet.state === state) return;
31880
+ assertUnlocked(workbook);
31824
31881
  if (state !== "visible") {
31825
- assertUnlocked(workbook);
31826
31882
  if (sheet.state === "visible" && visibleCount(workbook) <= 1)
31827
31883
  throw new Error("A workbook must contain at least one visible sheet.");
31828
31884
  }
@@ -31852,6 +31908,72 @@ function setTabColor(ctx, index2, color) {
31852
31908
  );
31853
31909
  }
31854
31910
 
31911
+ // xlsx/edit/tables.ts
31912
+ function nextTableName(workbook) {
31913
+ const taken = new Set(workbook.sheets.flatMap((s) => s.tables.map((t) => t.name.toLowerCase())));
31914
+ const names = new Set(workbook.definedNames.map((n2) => n2.name.toLowerCase()));
31915
+ for (let n2 = 1; ; n2++) if (!taken.has(`table${n2}`) && !names.has(`table${n2}`)) return `Table${n2}`;
31916
+ }
31917
+ function createTable(ctx, s, range, hasHeader, styleName = "TableStyleMedium2") {
31918
+ const { workbook } = ctx;
31919
+ const sheet = sheetAt(workbook, s);
31920
+ let r = normalizeRange(range);
31921
+ if (r.start.row === r.end.row && r.start.col === r.end.col) r = currentRegion(sheet, r.start);
31922
+ if (sheet.tables.some((t) => rangesIntersect(t.range, r)))
31923
+ throw new Error("A table cannot overlap another table.");
31924
+ if (sheet.merges.some((m) => rangesIntersect(m, r)))
31925
+ throw new Error("A table cannot contain merged cells.");
31926
+ return ctx.run(
31927
+ "Create table",
31928
+ "structure",
31929
+ [hasHeader ? { kind: "sheet", sheet: s } : { kind: "workbook" }],
31930
+ () => {
31931
+ if (!hasHeader) {
31932
+ const shift2 = { axis: "row", at: r.start.row, count: 1 };
31933
+ const band = { lo: r.start.col, hi: r.end.col };
31934
+ rewriteFormulas(workbook, (f6, fs) => shiftFormulaInBand(f6, fs, sheet.name, shift2, band));
31935
+ shiftSheetContent(sheet, shift2, band);
31936
+ r = { start: r.start, end: { row: r.end.row + 1, col: r.end.col } };
31937
+ }
31938
+ const width = r.end.col - r.start.col + 1;
31939
+ const texts = Array.from(
31940
+ { length: width },
31941
+ (_v, i2) => hasHeader ? displayText(workbook, getCell(sheet, r.start.row, r.start.col + i2)) : ""
31942
+ );
31943
+ const headers = uniqueHeaders(texts);
31944
+ const columns = headers.map((name2, i2) => {
31945
+ const cell = getCell(sheet, r.start.row, r.start.col + i2);
31946
+ if (cell?.value !== name2) {
31947
+ const styleId = cell?.styleId;
31948
+ writeValue(sheet, r.start.row, r.start.col + i2, name2);
31949
+ const written = getCell(sheet, r.start.row, r.start.col + i2);
31950
+ if (written && styleId) written.styleId = styleId;
31951
+ }
31952
+ return { name: name2 };
31953
+ });
31954
+ const name = nextTableName(workbook);
31955
+ const id = Math.max(0, ...workbook.sheets.flatMap((ws) => ws.tables.map((t) => t.id))) + 1;
31956
+ const table2 = {
31957
+ id,
31958
+ name,
31959
+ displayName: name,
31960
+ range: r,
31961
+ headerRow: true,
31962
+ totalsRow: false,
31963
+ columns,
31964
+ styleName,
31965
+ showRowStripes: true,
31966
+ showColumnStripes: false,
31967
+ showFirstColumn: false,
31968
+ showLastColumn: false
31969
+ };
31970
+ sheet.tables.push(table2);
31971
+ return table2;
31972
+ },
31973
+ { sheet: s, ranges: [r], structural: !hasHeader }
31974
+ );
31975
+ }
31976
+
31855
31977
  // xlsx/edit/validation-list.ts
31856
31978
  function listSource(workbook, sheet, formula, evaluate, at, depth = 0) {
31857
31979
  const text3 = formula.replace(/^=/, "").trim();
@@ -32153,7 +32275,7 @@ function createEditSession(workbook, options = {}) {
32153
32275
  const befores = scopes.map((scope) => captureScope(workbook, scope));
32154
32276
  let result2;
32155
32277
  try {
32156
- result2 = fn();
32278
+ result2 = fn(befores.find((snapshot) => snapshot.kind === "refs")?.data.formulas);
32157
32279
  } catch (error) {
32158
32280
  for (let i2 = befores.length - 1; i2 >= 0; i2--) {
32159
32281
  const snapshot = befores[i2];