@extend-ai/react-xlsx 0.8.1 → 0.8.2

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.
@@ -4308,6 +4308,75 @@ function resolveSheetColumnWidthPixels(width, columnWidthCharacterWidthPx) {
4308
4308
  return sheetColumnWidthToPixels(width, columnWidthCharacterWidthPx);
4309
4309
  }
4310
4310
 
4311
+ // src/safe-calculate.ts
4312
+ var SHEET_REF_REGEX = /'((?:[^']|'')+)'!|([A-Za-z_\u0080-\uFFFF][\w.\u0080-\uFFFF]*)!/g;
4313
+ function collectReferencedSheetNames(workbook2) {
4314
+ const referenced = /* @__PURE__ */ new Set();
4315
+ for (let sheetIdx = 0; sheetIdx < workbook2.sheetCount; sheetIdx += 1) {
4316
+ let sheet;
4317
+ try {
4318
+ sheet = workbook2.getSheet(sheetIdx);
4319
+ } catch {
4320
+ continue;
4321
+ }
4322
+ const cells = sheet.formulaCells;
4323
+ if (!Array.isArray(cells)) {
4324
+ continue;
4325
+ }
4326
+ for (const cell of cells) {
4327
+ const formula = cell?.formula;
4328
+ if (!formula) {
4329
+ continue;
4330
+ }
4331
+ SHEET_REF_REGEX.lastIndex = 0;
4332
+ let match;
4333
+ while ((match = SHEET_REF_REGEX.exec(formula)) !== null) {
4334
+ const raw = match[1] ?? match[2];
4335
+ if (!raw) {
4336
+ continue;
4337
+ }
4338
+ referenced.add(raw.replace(/''/g, "'"));
4339
+ }
4340
+ }
4341
+ }
4342
+ return referenced;
4343
+ }
4344
+ function hasUnresolvedSheetReferences(workbook2) {
4345
+ let names;
4346
+ try {
4347
+ names = workbook2.sheetNames;
4348
+ } catch {
4349
+ return false;
4350
+ }
4351
+ const known = new Set(names);
4352
+ const referenced = collectReferencedSheetNames(workbook2);
4353
+ for (const name of referenced) {
4354
+ if (!known.has(name)) {
4355
+ return true;
4356
+ }
4357
+ }
4358
+ return false;
4359
+ }
4360
+ function safeCalculate(workbook2, options = {}) {
4361
+ if (hasUnresolvedSheetReferences(workbook2)) {
4362
+ return { workbook: workbook2, calculated: false, skipReason: "unresolved-sheet-refs" };
4363
+ }
4364
+ try {
4365
+ workbook2.calculate();
4366
+ return { workbook: workbook2, calculated: true, skipReason: null };
4367
+ } catch (err) {
4368
+ console.warn("[react-xlsx] workbook.calculate() trapped; falling back to cached formula values", err);
4369
+ if (options.reparse) {
4370
+ try {
4371
+ return { workbook: options.reparse(), calculated: false, skipReason: "calculate-trapped" };
4372
+ } catch (reparseErr) {
4373
+ console.warn("[react-xlsx] workbook reparse after calculate trap failed", reparseErr);
4374
+ }
4375
+ }
4376
+ return { workbook: workbook2, calculated: false, skipReason: "calculate-trapped" };
4377
+ }
4378
+ }
4379
+
4311
4380
  // src/wasm.ts
4312
4381
  var wasmModulePromise = null;
4313
4382
  function getSheetsWasmModule() {
@@ -4659,14 +4728,18 @@ async function loadWorkbook(buffer, skipXmlParsing = false, showHiddenSheets = f
4659
4728
  const wasmModule = await getSheetsWasmModule();
4660
4729
  const bytes = new Uint8Array(buffer);
4661
4730
  const effectiveSkipXmlParsing = shouldSkipXmlParsingForWorkbook(bytes, skipXmlParsing);
4662
- const nextWorkbook = wasmModule.Workbook.fromBytes(bytes);
4731
+ let activeWorkbook = wasmModule.Workbook.fromBytes(bytes);
4663
4732
  let totalFormulas = 0;
4664
- for (let index = 0; index < nextWorkbook.sheetCount; index += 1) {
4665
- totalFormulas += nextWorkbook.getSheet(index).formulaCount;
4733
+ for (let index = 0; index < activeWorkbook.sheetCount; index += 1) {
4734
+ totalFormulas += activeWorkbook.getSheet(index).formulaCount;
4666
4735
  }
4667
4736
  if (totalFormulas <= FORMULA_COUNT_THRESHOLD) {
4668
- nextWorkbook.calculate();
4737
+ const result = safeCalculate(activeWorkbook, {
4738
+ reparse: () => wasmModule.Workbook.fromBytes(bytes)
4739
+ });
4740
+ activeWorkbook = result.workbook;
4669
4741
  }
4742
+ const nextWorkbook = activeWorkbook;
4670
4743
  const shouldUseFastStructureParse = bytes.byteLength >= FAST_STRUCTURE_PARSE_THRESHOLD_BYTES && totalFormulas <= FORMULA_COUNT_THRESHOLD;
4671
4744
  const structureAssets = effectiveSkipXmlParsing || shouldUseFastStructureParse ? null : parseWorkbookStructureAssets(bytes, {
4672
4745
  includeCachedFormulaValues: true
@@ -4709,14 +4782,18 @@ async function parseCharts(buffer, skipXmlParsing = false, showHiddenSheets = fa
4709
4782
  const wasmModule = await getSheetsWasmModule();
4710
4783
  const bytes = new Uint8Array(buffer);
4711
4784
  const effectiveSkipXmlParsing = shouldSkipXmlParsingForWorkbook(bytes, skipXmlParsing);
4712
- const nextWorkbook = wasmModule.Workbook.fromBytes(bytes);
4785
+ let activeWorkbook = wasmModule.Workbook.fromBytes(bytes);
4713
4786
  let totalFormulas = 0;
4714
- for (let index = 0; index < nextWorkbook.sheetCount; index += 1) {
4715
- totalFormulas += nextWorkbook.getSheet(index).formulaCount;
4787
+ for (let index = 0; index < activeWorkbook.sheetCount; index += 1) {
4788
+ totalFormulas += activeWorkbook.getSheet(index).formulaCount;
4716
4789
  }
4717
4790
  if (totalFormulas <= FORMULA_COUNT_THRESHOLD) {
4718
- nextWorkbook.calculate();
4791
+ const result = safeCalculate(activeWorkbook, {
4792
+ reparse: () => wasmModule.Workbook.fromBytes(bytes)
4793
+ });
4794
+ activeWorkbook = result.workbook;
4719
4795
  }
4796
+ const nextWorkbook = activeWorkbook;
4720
4797
  const visibleSheetIndexByWorkbookSheetIndex = buildVisibleSheetIndexByWorkbookSheetIndex(nextWorkbook, showHiddenSheets);
4721
4798
  const chartStyleAssets = effectiveSkipXmlParsing ? null : parseWorkbookChartStyleAssets(bytes);
4722
4799
  const chartAssets = loadWorkbookChartAssets(