@extend-ai/react-xlsx 0.5.0 → 0.7.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.
@@ -16,6 +16,9 @@ var SERIES_COLORS = [
16
16
  "#636363",
17
17
  "#997300"
18
18
  ];
19
+ function normalizeWorksheetVisibility(value) {
20
+ return value === "hidden" || value === "veryHidden" ? value : "visible";
21
+ }
19
22
  var EMU_PER_PIXEL = 9525;
20
23
  var THEME_COLOR_INDEX_BY_NAME = {
21
24
  accent1: 4,
@@ -950,7 +953,7 @@ function applyChartSeriesStyleFromXml(chart, chartTypeNode, themePalette) {
950
953
  const invertNegativeStyle = parseInvertNegativeStyle(seriesNode, themePalette);
951
954
  const invertIfNegative = readChartBooleanAttribute(seriesNode, "invertIfNegative");
952
955
  const isScatterChart = chart.chartType === "Scatter" || chart.chartType === "ScatterLines" || chart.chartType === "ScatterSmooth" || chart.chartType === "Bubble";
953
- const cachedCategories = isScatterChart ? parseChartCacheValues(getFirstLocalChild(seriesNode, "xVal"), "numCache", "value") ?? parseChartMultiLevelCacheValues(getFirstLocalChild(seriesNode, "xVal"), "category") : parseChartCacheValues(getFirstLocalChild(seriesNode, "cat"), "strCache", "category");
956
+ const cachedCategories = isScatterChart ? parseChartCacheValues(getFirstLocalChild(seriesNode, "xVal"), "numCache", "value") ?? parseChartMultiLevelCacheValues(getFirstLocalChild(seriesNode, "xVal"), "category") : parseChartCacheValues(getFirstLocalChild(seriesNode, "cat"), "strCache", "category") ?? parseChartCacheValues(getFirstLocalChild(seriesNode, "cat"), "numCache", "category") ?? parseChartMultiLevelCacheValues(getFirstLocalChild(seriesNode, "cat"), "category");
954
957
  const cachedValues = isScatterChart ? parseChartCacheValues(getFirstLocalChild(seriesNode, "yVal"), "numCache", "value") : parseChartCacheValues(getFirstLocalChild(seriesNode, "val"), "numCache", "value");
955
958
  const cachedBubbleSizes = chart.chartType === "Bubble" ? parseChartCacheValues(getFirstLocalChild(seriesNode, "bubbleSize"), "numCache", "value") : null;
956
959
  const existingShapeProperties = series.shapeProperties && typeof series.shapeProperties === "object" ? series.shapeProperties : null;
@@ -2691,17 +2694,25 @@ function normalizeChartsheet(raw, index) {
2691
2694
  workbookSheetIndex: typeof chartsheet.workbookSheetIndex === "number" ? chartsheet.workbookSheetIndex : void 0
2692
2695
  };
2693
2696
  }
2694
- function buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetIndex) {
2697
+ function buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetIndex, showHiddenSheets = false) {
2695
2698
  const rawOrder = Array.isArray(workbook2.sheetOrder) ? workbook2.sheetOrder : [];
2696
2699
  if (rawOrder.length === 0) {
2697
- return workbook2.sheetNames.map((name, index) => ({
2698
- id: `sheet-${index}`,
2699
- index,
2700
- kind: "sheet",
2701
- name,
2702
- sheetIndex: visibleSheetIndexByWorkbookSheetIndex.get(index) ?? index,
2703
- workbookSheetIndex: index
2704
- }));
2700
+ return workbook2.sheetNames.flatMap((name, index) => {
2701
+ const worksheet = workbook2.getSheet(index);
2702
+ const visibility = normalizeWorksheetVisibility(worksheet.visibility);
2703
+ if (!showHiddenSheets && visibility !== "visible") {
2704
+ return [];
2705
+ }
2706
+ return [{
2707
+ id: `sheet-${index}`,
2708
+ index,
2709
+ kind: "sheet",
2710
+ name,
2711
+ sheetIndex: visibleSheetIndexByWorkbookSheetIndex.get(index) ?? index,
2712
+ visibility,
2713
+ workbookSheetIndex: index
2714
+ }];
2715
+ });
2705
2716
  }
2706
2717
  return rawOrder.flatMap((entry, index) => {
2707
2718
  const slotType = typeof entry.slotType === "string" ? entry.slotType : "worksheet";
@@ -2717,7 +2728,8 @@ function buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetInde
2717
2728
  }] : [];
2718
2729
  }
2719
2730
  const worksheet = workbook2.getSheet(slotIndex);
2720
- if (worksheet.visibility !== "visible") {
2731
+ const visibility = normalizeWorksheetVisibility(worksheet.visibility);
2732
+ if (!showHiddenSheets && visibility !== "visible") {
2721
2733
  return [];
2722
2734
  }
2723
2735
  return [{
@@ -2726,6 +2738,7 @@ function buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetInde
2726
2738
  kind: "sheet",
2727
2739
  name: worksheet.name,
2728
2740
  sheetIndex: visibleSheetIndexByWorkbookSheetIndex.get(slotIndex) ?? slotIndex,
2741
+ visibility,
2729
2742
  workbookSheetIndex: slotIndex
2730
2743
  }];
2731
2744
  });
@@ -2815,7 +2828,7 @@ function applyChartOrigins(chartsByWorkbookSheetIndex2, chartOriginsById, archiv
2815
2828
  });
2816
2829
  }
2817
2830
  }
2818
- function loadWorkbookChartAssets(workbook2, imageAssets, visibleSheetIndexByWorkbookSheetIndex) {
2831
+ function loadWorkbookChartAssets(workbook2, imageAssets, visibleSheetIndexByWorkbookSheetIndex, showHiddenSheets = false) {
2819
2832
  const chartsByWorkbookSheetIndex2 = Array.from({ length: workbook2.sheetCount }, (_, workbookSheetIndex) => {
2820
2833
  const worksheet = workbook2.getSheet(workbookSheetIndex);
2821
2834
  const rawCharts = Array.isArray(worksheet.charts) ? worksheet.charts : [];
@@ -2903,7 +2916,7 @@ function loadWorkbookChartAssets(workbook2, imageAssets, visibleSheetIndexByWork
2903
2916
  return [...classicCharts, ...modernCharts];
2904
2917
  });
2905
2918
  const chartsheets2 = Array.isArray(workbook2.chartsheets) ? workbook2.chartsheets.map((entry, index) => normalizeChartsheet(entry, index)) : [];
2906
- const tabs2 = buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetIndex);
2919
+ const tabs2 = buildTabs(workbook2, chartsheets2, visibleSheetIndexByWorkbookSheetIndex, showHiddenSheets);
2907
2920
  const chartOriginsById = /* @__PURE__ */ new Map();
2908
2921
  if (imageAssets) {
2909
2922
  applyChartOrigins(chartsByWorkbookSheetIndex2, chartOriginsById, imageAssets.archive, imageAssets.sheetOrigins);
@@ -3605,6 +3618,8 @@ function parseSpreadsheetAlignment(node) {
3605
3618
  const vertical = node.getAttribute("vertical");
3606
3619
  const wrapText = node.getAttribute("wrapText");
3607
3620
  const indent = node.getAttribute("indent");
3621
+ const shrinkToFit = node.getAttribute("shrinkToFit");
3622
+ const textRotation = node.getAttribute("textRotation");
3608
3623
  if (horizontal) {
3609
3624
  alignment.horizontal = horizontal;
3610
3625
  }
@@ -3614,9 +3629,18 @@ function parseSpreadsheetAlignment(node) {
3614
3629
  if (wrapText !== null) {
3615
3630
  alignment.wrapText = wrapText === "1";
3616
3631
  }
3632
+ if (shrinkToFit !== null) {
3633
+ alignment.shrinkToFit = shrinkToFit === "1";
3634
+ }
3617
3635
  if (indent !== null) {
3618
3636
  alignment.indent = Number(indent);
3619
3637
  }
3638
+ if (textRotation !== null) {
3639
+ const parsedRotation = Number(textRotation);
3640
+ if (Number.isFinite(parsedRotation)) {
3641
+ alignment.textRotation = parsedRotation;
3642
+ }
3643
+ }
3620
3644
  return Object.keys(alignment).length > 0 ? alignment : void 0;
3621
3645
  }
3622
3646
  function parseDifferentialStyle(node) {
@@ -4284,18 +4308,28 @@ var DEFAULT_COL_WIDTH = 80;
4284
4308
  var DEFAULT_ZOOM_SCALE = 100;
4285
4309
  var FORMULA_COUNT_THRESHOLD = 1e3;
4286
4310
  var FAST_STRUCTURE_PARSE_THRESHOLD_BYTES = 5 * 1024 * 1024;
4311
+ function isLegacyXlsWorkbook(bytes) {
4312
+ return bytes.byteLength >= 8 && bytes[0] === 208 && bytes[1] === 207 && bytes[2] === 17 && bytes[3] === 224 && bytes[4] === 161 && bytes[5] === 177 && bytes[6] === 26 && bytes[7] === 225;
4313
+ }
4314
+ function shouldSkipXmlParsingForWorkbook(bytes, skipXmlParsing = false) {
4315
+ return skipXmlParsing || isLegacyXlsWorkbook(bytes);
4316
+ }
4317
+ function normalizeWorksheetVisibility2(value) {
4318
+ return value === "hidden" || value === "veryHidden" ? value : "visible";
4319
+ }
4287
4320
  var workbook = null;
4288
4321
  var chartsByWorkbookSheetIndex = [];
4289
4322
  var chartsheets = [];
4290
4323
  var sheets = [];
4291
4324
  var tablesByWorkbookSheetIndex = [];
4292
4325
  var tabs = [];
4293
- function buildVisibleSheetIndexByWorkbookSheetIndex(nextWorkbook) {
4326
+ function buildVisibleSheetIndexByWorkbookSheetIndex(nextWorkbook, showHiddenSheets = false) {
4294
4327
  const mapping = /* @__PURE__ */ new Map();
4295
4328
  let visibleIndex = 0;
4296
4329
  for (let workbookSheetIndex = 0; workbookSheetIndex < nextWorkbook.sheetCount; workbookSheetIndex += 1) {
4297
4330
  const worksheet = nextWorkbook.getSheet(workbookSheetIndex);
4298
- if (worksheet.visibility !== "visible") {
4331
+ const visibility = normalizeWorksheetVisibility2(worksheet.visibility);
4332
+ if (!showHiddenSheets && visibility !== "visible") {
4299
4333
  continue;
4300
4334
  }
4301
4335
  mapping.set(workbookSheetIndex, visibleIndex);
@@ -4391,12 +4425,13 @@ function resolveWorksheetZoomScale(worksheet, sheetState) {
4391
4425
  const value = candidates.find((entry) => typeof entry === "number" && Number.isFinite(entry) && entry > 0);
4392
4426
  return value ?? DEFAULT_ZOOM_SCALE;
4393
4427
  }
4394
- function buildSheetList(nextWorkbook, structureAssets) {
4428
+ function buildSheetList(nextWorkbook, structureAssets, showHiddenSheets = false) {
4395
4429
  const sheetsByWorkbookSheetIndex = [];
4396
4430
  for (let index = 0; index < nextWorkbook.sheetCount; index += 1) {
4397
4431
  const worksheet = nextWorkbook.getSheet(index);
4398
4432
  const sheetState = structureAssets?.sheetStatesByWorkbookSheetIndex[index] ?? null;
4399
- if (worksheet.visibility !== "visible") {
4433
+ const visibility = normalizeWorksheetVisibility2(worksheet.visibility);
4434
+ if (!showHiddenSheets && visibility !== "visible") {
4400
4435
  continue;
4401
4436
  }
4402
4437
  const resolveColumnWidthPx = (col) => {
@@ -4433,9 +4468,12 @@ function buildSheetList(nextWorkbook, structureAssets) {
4433
4468
  maxVerticalMergeEndRow: sheetState?.maxVerticalMergeEndRow ?? -1,
4434
4469
  hiddenCols: sheetState?.hiddenCols ?? [],
4435
4470
  hiddenRows: sheetState?.hiddenRows ?? [],
4471
+ minUsedCol: -1,
4472
+ minUsedRow: -1,
4436
4473
  maxUsedCol: -1,
4437
4474
  maxUsedRow: -1,
4438
4475
  name: worksheet.name,
4476
+ visibility,
4439
4477
  namedCellStyleByName: structureAssets?.namedCellStyleByName ?? {},
4440
4478
  rowCount: 0,
4441
4479
  rowHeightOverridesPx: sheetState?.rowHeightOverridesPx ?? {},
@@ -4453,7 +4491,7 @@ function buildSheetList(nextWorkbook, structureAssets) {
4453
4491
  });
4454
4492
  continue;
4455
4493
  }
4456
- const [, , maxRow, maxCol] = usedRange;
4494
+ const [minRow, minCol, maxRow, maxCol] = usedRange;
4457
4495
  const hiddenRows = (sheetState?.hiddenRows ?? []).filter((row) => row >= 0 && row <= maxRow);
4458
4496
  const hiddenCols = (sheetState?.hiddenCols ?? []).filter((col) => col >= 0 && col <= maxCol);
4459
4497
  sheetsByWorkbookSheetIndex.push({
@@ -4474,9 +4512,12 @@ function buildSheetList(nextWorkbook, structureAssets) {
4474
4512
  maxVerticalMergeEndRow: sheetState?.maxVerticalMergeEndRow ?? -1,
4475
4513
  hiddenCols,
4476
4514
  hiddenRows,
4515
+ minUsedCol: minCol,
4516
+ minUsedRow: minRow,
4477
4517
  maxUsedCol: maxCol,
4478
4518
  maxUsedRow: maxRow,
4479
4519
  name: worksheet.name,
4520
+ visibility,
4480
4521
  namedCellStyleByName: structureAssets?.namedCellStyleByName ?? {},
4481
4522
  rowCount: Math.max(0, maxRow + 1 - hiddenRows.length),
4482
4523
  rowHeightOverridesPx: sheetState?.rowHeightOverridesPx ?? {},
@@ -4560,9 +4601,10 @@ function cellAddressToA1(cell) {
4560
4601
  }
4561
4602
  return `${label}${cell.row + 1}`;
4562
4603
  }
4563
- async function loadWorkbook(buffer, skipXmlParsing = false) {
4604
+ async function loadWorkbook(buffer, skipXmlParsing = false, showHiddenSheets = false) {
4564
4605
  const wasmModule = await getSheetsWasmModule();
4565
4606
  const bytes = new Uint8Array(buffer);
4607
+ const effectiveSkipXmlParsing = shouldSkipXmlParsingForWorkbook(bytes, skipXmlParsing);
4566
4608
  const nextWorkbook = wasmModule.Workbook.fromBytes(bytes);
4567
4609
  let totalFormulas = 0;
4568
4610
  for (let index = 0; index < nextWorkbook.sheetCount; index += 1) {
@@ -4572,11 +4614,11 @@ async function loadWorkbook(buffer, skipXmlParsing = false) {
4572
4614
  nextWorkbook.calculate();
4573
4615
  }
4574
4616
  const shouldUseFastStructureParse = bytes.byteLength >= FAST_STRUCTURE_PARSE_THRESHOLD_BYTES && totalFormulas <= FORMULA_COUNT_THRESHOLD;
4575
- const structureAssets = skipXmlParsing || shouldUseFastStructureParse ? null : parseWorkbookStructureAssets(bytes, {
4617
+ const structureAssets = effectiveSkipXmlParsing || shouldUseFastStructureParse ? null : parseWorkbookStructureAssets(bytes, {
4576
4618
  includeCachedFormulaValues: true
4577
4619
  });
4578
4620
  workbook = nextWorkbook;
4579
- sheets = buildSheetList(nextWorkbook, structureAssets);
4621
+ sheets = buildSheetList(nextWorkbook, structureAssets, showHiddenSheets);
4580
4622
  tablesByWorkbookSheetIndex = Array.from(
4581
4623
  { length: nextWorkbook.sheetCount },
4582
4624
  (_, workbookSheetIndex) => mapWorksheetTables(
@@ -4591,8 +4633,13 @@ async function loadWorkbook(buffer, skipXmlParsing = false) {
4591
4633
  const hasModernCharts = Array.isArray(worksheet.chartsEx) && worksheet.chartsEx.length > 0;
4592
4634
  return hasClassicCharts || hasModernCharts;
4593
4635
  }).some(Boolean);
4594
- const chartStyleAssets = skipXmlParsing || !hasCharts ? null : parseWorkbookChartStyleAssets(bytes);
4595
- const chartAssets = loadWorkbookChartAssets(nextWorkbook, chartStyleAssets, visibleSheetIndexByWorkbookSheetIndex);
4636
+ const chartStyleAssets = effectiveSkipXmlParsing || !hasCharts ? null : parseWorkbookChartStyleAssets(bytes);
4637
+ const chartAssets = loadWorkbookChartAssets(
4638
+ nextWorkbook,
4639
+ chartStyleAssets,
4640
+ visibleSheetIndexByWorkbookSheetIndex,
4641
+ showHiddenSheets
4642
+ );
4596
4643
  chartsByWorkbookSheetIndex = chartAssets.chartsByWorkbookSheetIndex;
4597
4644
  chartsheets = chartAssets.chartsheets;
4598
4645
  tabs = chartAssets.tabs;
@@ -4604,9 +4651,10 @@ async function loadWorkbook(buffer, skipXmlParsing = false) {
4604
4651
  tabs
4605
4652
  };
4606
4653
  }
4607
- async function parseCharts(buffer, skipXmlParsing = false) {
4654
+ async function parseCharts(buffer, skipXmlParsing = false, showHiddenSheets = false) {
4608
4655
  const wasmModule = await getSheetsWasmModule();
4609
4656
  const bytes = new Uint8Array(buffer);
4657
+ const effectiveSkipXmlParsing = shouldSkipXmlParsingForWorkbook(bytes, skipXmlParsing);
4610
4658
  const nextWorkbook = wasmModule.Workbook.fromBytes(bytes);
4611
4659
  let totalFormulas = 0;
4612
4660
  for (let index = 0; index < nextWorkbook.sheetCount; index += 1) {
@@ -4615,9 +4663,14 @@ async function parseCharts(buffer, skipXmlParsing = false) {
4615
4663
  if (totalFormulas <= FORMULA_COUNT_THRESHOLD) {
4616
4664
  nextWorkbook.calculate();
4617
4665
  }
4618
- const visibleSheetIndexByWorkbookSheetIndex = buildVisibleSheetIndexByWorkbookSheetIndex(nextWorkbook);
4619
- const chartStyleAssets = skipXmlParsing ? null : parseWorkbookChartStyleAssets(bytes);
4620
- const chartAssets = loadWorkbookChartAssets(nextWorkbook, chartStyleAssets, visibleSheetIndexByWorkbookSheetIndex);
4666
+ const visibleSheetIndexByWorkbookSheetIndex = buildVisibleSheetIndexByWorkbookSheetIndex(nextWorkbook, showHiddenSheets);
4667
+ const chartStyleAssets = effectiveSkipXmlParsing ? null : parseWorkbookChartStyleAssets(bytes);
4668
+ const chartAssets = loadWorkbookChartAssets(
4669
+ nextWorkbook,
4670
+ chartStyleAssets,
4671
+ visibleSheetIndexByWorkbookSheetIndex,
4672
+ showHiddenSheets
4673
+ );
4621
4674
  return {
4622
4675
  chartsByWorkbookSheetIndex: chartAssets.chartsByWorkbookSheetIndex,
4623
4676
  chartsheets: chartAssets.chartsheets,
@@ -4630,10 +4683,10 @@ function respond(message) {
4630
4683
  async function handleMessage(message) {
4631
4684
  switch (message.type) {
4632
4685
  case "load": {
4633
- return loadWorkbook(message.payload.buffer, message.payload.skipXmlParsing);
4686
+ return loadWorkbook(message.payload.buffer, message.payload.skipXmlParsing, message.payload.showHiddenSheets);
4634
4687
  }
4635
4688
  case "parseCharts": {
4636
- return parseCharts(message.payload.buffer, message.payload.skipXmlParsing);
4689
+ return parseCharts(message.payload.buffer, message.payload.skipXmlParsing, message.payload.showHiddenSheets);
4637
4690
  }
4638
4691
  case "getCellSnapshot": {
4639
4692
  if (!workbook) {