@file-viewer/renderer-spreadsheet 2.2.6 → 2.2.8

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.
@@ -1,5 +1,6 @@
1
- import type { WorkSheet } from 'styled-exceljs';
1
+ import type { Range, WorkSheet } from 'styled-exceljs';
2
2
  import type { CellMerge, SheetChartDefinition, SheetColumn, SheetModel, SheetStructure, SheetWindow } from '../type.js';
3
+ export declare const createAutoFitSampleRanges: (totalRowCount: number, totalColCount: number) => Range[];
3
4
  export declare const defaults: {
4
5
  rowHeight: number;
5
6
  colWidth: number;
@@ -11,6 +12,9 @@ interface SheetSliceOptions {
11
12
  totalCols?: number;
12
13
  charts?: SheetChartDefinition[];
13
14
  }
15
+ interface SheetObjectOptions {
16
+ includeLayout?: boolean;
17
+ }
14
18
  type SheetCellMeta = {
15
19
  className?: string;
16
20
  style: Record<string, string>;
@@ -66,11 +70,12 @@ export default class SheetJsModel implements SheetModel {
66
70
  private getRowHeights;
67
71
  private getAllRowHeights;
68
72
  private getAutoFitColumns;
73
+ private getAutoFitSampleRanges;
69
74
  private get autoFitColumns();
70
75
  private getColumnMeta;
71
76
  private getColWidths;
72
77
  private getColumns;
73
78
  private getStructure;
74
- toObject(): object;
79
+ toObject(options?: SheetObjectOptions): object;
75
80
  }
76
81
  export {};
@@ -8,6 +8,36 @@ const DEFAULT_IMAGE_WIDTH = 480;
8
8
  const DEFAULT_IMAGE_HEIGHT = 288;
9
9
  const AUTO_FIT_MIN_WIDTH = 24;
10
10
  const AUTO_FIT_PADDING = 8;
11
+ const AUTO_FIT_MAX_SAMPLE_CELLS = 100000;
12
+ const AUTO_FIT_MAX_SAMPLE_WINDOWS = 8;
13
+ const AUTO_FIT_MAX_ROWS_PER_WINDOW = 128;
14
+ export const createAutoFitSampleRanges = (totalRowCount, totalColCount) => {
15
+ const totalRows = Math.max(totalRowCount, 1);
16
+ const totalCols = Math.max(totalColCount, 1);
17
+ const endColumn = totalCols - 1;
18
+ const totalCells = totalRows * totalCols;
19
+ if (totalCells <= AUTO_FIT_MAX_SAMPLE_CELLS) {
20
+ return [{
21
+ s: { r: 0, c: 0 },
22
+ e: { r: totalRows - 1, c: endColumn }
23
+ }];
24
+ }
25
+ const rowsPerWindow = Math.max(1, Math.min(AUTO_FIT_MAX_ROWS_PER_WINDOW, Math.floor(AUTO_FIT_MAX_SAMPLE_CELLS / totalCols / AUTO_FIT_MAX_SAMPLE_WINDOWS)));
26
+ const maxStartRow = Math.max(totalRows - rowsPerWindow, 0);
27
+ const maxWindowsWithinBudget = Math.max(1, Math.floor(AUTO_FIT_MAX_SAMPLE_CELLS / totalCols / rowsPerWindow));
28
+ const windowCount = Math.min(AUTO_FIT_MAX_SAMPLE_WINDOWS, maxWindowsWithinBudget, Math.ceil(totalRows / rowsPerWindow));
29
+ const starts = new Set();
30
+ for (let index = 0; index < windowCount; index += 1) {
31
+ const startRow = windowCount === 1
32
+ ? 0
33
+ : Math.round(maxStartRow * index / (windowCount - 1));
34
+ starts.add(startRow);
35
+ }
36
+ return Array.from(starts, startRow => ({
37
+ s: { r: startRow, c: 0 },
38
+ e: { r: Math.min(startRow + rowsPerWindow - 1, totalRows - 1), c: endColumn }
39
+ }));
40
+ };
11
41
  const toFiniteNumber = (value) => {
12
42
  return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
13
43
  };
@@ -567,25 +597,47 @@ class SheetJsModel {
567
597
  return heights.length ? heights : undefined;
568
598
  }
569
599
  getAutoFitColumns() {
600
+ var _a, _b;
570
601
  const autoFitColumns = utils.auto_fit_columns || utils.autofit_columns;
571
602
  if (typeof autoFitColumns !== 'function') {
572
603
  return undefined;
573
604
  }
574
605
  try {
575
- // 自动列宽只作为缺失列宽的兜底。合并标题和 Excel 溢出文本不应该反向撑开基础列宽。
576
- return autoFitColumns(this.ws, {
577
- set: false,
578
- skipHidden: true,
579
- includeMerged: false,
580
- minPx: AUTO_FIT_MIN_WIDTH,
581
- padding: AUTO_FIT_PADDING
582
- });
606
+ const sampleRanges = this.getAutoFitSampleRanges();
607
+ const fittedColumns = [];
608
+ for (const range of sampleRanges) {
609
+ // 自动列宽只作为缺失列宽的兜底。合并标题和 Excel 溢出文本不应该反向撑开基础列宽。
610
+ // 大表只抽样固定数量的单元格,避免为了首屏列宽扫描整张工作表。
611
+ const measuredColumns = autoFitColumns(this.ws, {
612
+ range,
613
+ set: false,
614
+ skipHidden: true,
615
+ includeMerged: false,
616
+ minPx: AUTO_FIT_MIN_WIDTH,
617
+ padding: AUTO_FIT_PADDING
618
+ });
619
+ for (let colIndex = 0; colIndex < this.totalCols; colIndex += 1) {
620
+ const measured = measuredColumns === null || measuredColumns === void 0 ? void 0 : measuredColumns[colIndex];
621
+ if (!measured) {
622
+ continue;
623
+ }
624
+ const currentWidth = (_a = getColumnPixelWidth(fittedColumns[colIndex])) !== null && _a !== void 0 ? _a : -1;
625
+ const measuredWidth = (_b = getColumnPixelWidth(measured)) !== null && _b !== void 0 ? _b : -1;
626
+ if (!fittedColumns[colIndex] || measuredWidth > currentWidth) {
627
+ fittedColumns[colIndex] = measured;
628
+ }
629
+ }
630
+ }
631
+ return fittedColumns.length ? fittedColumns : undefined;
583
632
  }
584
633
  catch (error) {
585
634
  console.warn('[file-viewer] Excel 自动列宽计算失败,已回退到原始列宽。', error);
586
635
  return undefined;
587
636
  }
588
637
  }
638
+ getAutoFitSampleRanges() {
639
+ return createAutoFitSampleRanges(this.totalRows, this.totalCols);
640
+ }
589
641
  get autoFitColumns() {
590
642
  if (this._autoFitColumns === undefined) {
591
643
  this._autoFitColumns = this.getAutoFitColumns() || null;
@@ -638,16 +690,18 @@ class SheetJsModel {
638
690
  charts: this.getCharts()
639
691
  };
640
692
  }
641
- toObject() {
642
- const { defaults, data, cell, merge, rowHeights, colWidths, columns, meta } = this;
693
+ toObject(options = {}) {
694
+ const { defaults, data, cell, merge, rowHeights, meta } = this;
643
695
  return {
644
696
  defaults,
645
697
  data,
646
698
  cell,
647
699
  merge,
648
700
  rowHeights,
649
- colWidths,
650
- columns,
701
+ ...(options.includeLayout === false ? {} : {
702
+ colWidths: this.colWidths,
703
+ columns: this.columns
704
+ }),
651
705
  meta
652
706
  };
653
707
  }
@@ -229,7 +229,9 @@ export const parseSpreadsheetSheet = (context, payload = {}) => {
229
229
  totalCols: sheetMeta === null || sheetMeta === void 0 ? void 0 : sheetMeta.colCount,
230
230
  charts: context.charts[sheetName],
231
231
  });
232
- const windowData = sheetModel.toObject();
232
+ // Keep the first response backward-compatible; later virtual windows only need rows and cells.
233
+ // Avoid recalculating auto-fit column widths for every 500-row request.
234
+ const windowData = sheetModel.toObject({ includeLayout: startRow === 0 });
233
235
  const structure = startRow === 0 ? sheetModel.structure : undefined;
234
236
  return [{
235
237
  type: 'parseSheet',