@extend-ai/react-xlsx 0.17.1 → 0.17.3

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.
@@ -4690,13 +4690,52 @@ function hasUnresolvedSheetReferences(workbook2) {
4690
4690
  }
4691
4691
  return false;
4692
4692
  }
4693
+ function collectFormulaCellsWithSavedValues(workbook2) {
4694
+ const positions = [];
4695
+ for (let sheetIdx = 0; sheetIdx < workbook2.sheetCount; sheetIdx += 1) {
4696
+ let sheet;
4697
+ try {
4698
+ sheet = workbook2.getSheet(sheetIdx);
4699
+ } catch {
4700
+ continue;
4701
+ }
4702
+ const cells = sheet.formulaCells;
4703
+ if (!Array.isArray(cells)) {
4704
+ continue;
4705
+ }
4706
+ for (const cell of cells) {
4707
+ if (typeof cell?.row !== "number" || typeof cell?.col !== "number") {
4708
+ continue;
4709
+ }
4710
+ let saved;
4711
+ try {
4712
+ saved = sheet.getCalculatedValueAt(cell.row, cell.col);
4713
+ } catch {
4714
+ continue;
4715
+ }
4716
+ if (!saved.is_empty && !saved.is_error) {
4717
+ positions.push({ sheetIdx, row: cell.row, col: cell.col });
4718
+ }
4719
+ }
4720
+ }
4721
+ return positions;
4722
+ }
4723
+ function anyBecameError(workbook2, positions) {
4724
+ return positions.some(({ sheetIdx, row, col }) => {
4725
+ try {
4726
+ return workbook2.getSheet(sheetIdx).getCalculatedValueAt(row, col).is_error;
4727
+ } catch {
4728
+ return false;
4729
+ }
4730
+ });
4731
+ }
4693
4732
  function safeCalculate(workbook2, options = {}) {
4694
4733
  if (hasUnresolvedSheetReferences(workbook2)) {
4695
4734
  return { workbook: workbook2, calculated: false, skipReason: "unresolved-sheet-refs" };
4696
4735
  }
4736
+ const withSavedValues = options.reparse ? collectFormulaCellsWithSavedValues(workbook2) : [];
4697
4737
  try {
4698
4738
  workbook2.calculate();
4699
- return { workbook: workbook2, calculated: true, skipReason: null };
4700
4739
  } catch (err) {
4701
4740
  console.warn("[react-xlsx] workbook.calculate() trapped; falling back to cached formula values", err);
4702
4741
  if (options.reparse) {
@@ -4708,6 +4747,14 @@ function safeCalculate(workbook2, options = {}) {
4708
4747
  }
4709
4748
  return { workbook: workbook2, calculated: false, skipReason: "calculate-trapped" };
4710
4749
  }
4750
+ if (options.reparse && anyBecameError(workbook2, withSavedValues)) {
4751
+ try {
4752
+ return { workbook: options.reparse(), calculated: false, skipReason: "calculate-regressed" };
4753
+ } catch (reparseErr) {
4754
+ console.warn("[react-xlsx] workbook reparse after calculate regression failed", reparseErr);
4755
+ }
4756
+ }
4757
+ return { workbook: workbook2, calculated: true, skipReason: null };
4711
4758
  }
4712
4759
 
4713
4760
  // src/wasm.ts