@file-viewer/renderer-spreadsheet 3.2.2 → 3.3.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.
@@ -3,6 +3,7 @@ import SheetJsModel, { formatCellValue } from './SheetJsModel.js';
3
3
  import { parseSpreadsheetCharts } from './chartParser.js';
4
4
  import { prepareSpreadsheetReadInput, } from './textEncoding.js';
5
5
  import { parseSpreadsheetCellImages } from './richDataParser.js';
6
+ import { recoverUncachedFormulaValues } from './formulaValues.js';
6
7
  const readOptions = {
7
8
  type: 'array',
8
9
  dense: true,
@@ -147,10 +148,49 @@ const EMPTY_RANGE_COLUMN_LIMIT = 256;
147
148
  const RANGE_ROW_SLACK = 256;
148
149
  const RANGE_COLUMN_SLACK = 64;
149
150
  const RANGE_GROWTH_FACTOR = 4;
151
+ const CONTENT_GAP_ROW_MULTIPLIER = 4;
152
+ const CONTENT_GAP_COL_MULTIPLIER = 4;
153
+ const compactSparseExtent = (indices, slack, gapMultiplier, contentExtent) => {
154
+ if (indices.length === 0)
155
+ return 0;
156
+ const sorted = [...new Set(indices)].sort((a, b) => a - b);
157
+ const originThreshold = Math.max(slack * gapMultiplier, RANGE_GROWTH_FACTOR);
158
+ if (!contentExtent && sorted[0] > originThreshold)
159
+ return 0;
160
+ let end = Math.max(sorted[0], contentExtent - 1);
161
+ for (let i = 1; i < sorted.length; i += 1) {
162
+ const current = sorted[i];
163
+ if (current <= end)
164
+ continue;
165
+ const currentExtent = end + 1;
166
+ const gap = current - end;
167
+ const threshold = Math.max(slack * gapMultiplier, currentExtent * RANGE_GROWTH_FACTOR);
168
+ if (gap > threshold)
169
+ break;
170
+ end = current;
171
+ }
172
+ return end + 1;
173
+ };
150
174
  const getCellBounds = (worksheet) => {
151
175
  const bounds = { rowCount: 0, colCount: 0 };
152
176
  if (!worksheet)
153
177
  return bounds;
178
+ const rowIndices = [];
179
+ const colIndices = [];
180
+ let contentRows = 0;
181
+ let contentCols = 0;
182
+ const observe = (row, col, value) => {
183
+ rowIndices.push(row);
184
+ colIndices.push(col);
185
+ const cell = value;
186
+ const meaningful = typeof value !== 'object' || cell.v != null || cell.f != null
187
+ || cell.F != null || !!cell.w || !!cell.r || !!cell.h || !!cell.l
188
+ || (Array.isArray(cell.c) && cell.c.length > 0);
189
+ if (meaningful) {
190
+ contentRows = Math.max(contentRows, row + 1);
191
+ contentCols = Math.max(contentCols, col + 1);
192
+ }
193
+ };
154
194
  const denseRows = Array.isArray(worksheet)
155
195
  ? worksheet
156
196
  : worksheet['!data'];
@@ -164,23 +204,24 @@ const getCellBounds = (worksheet) => {
164
204
  const colIndex = Number(colKey);
165
205
  if (!Number.isInteger(colIndex) || colIndex < 0 || row[colIndex] == null)
166
206
  continue;
167
- bounds.rowCount = Math.max(bounds.rowCount, rowIndex + 1);
168
- bounds.colCount = Math.max(bounds.colCount, colIndex + 1);
207
+ observe(rowIndex, colIndex, row[colIndex]);
169
208
  }
170
209
  }
171
- return bounds;
172
210
  }
173
- for (const key of Object.keys(worksheet)) {
174
- if (key.startsWith('!') || !/^[A-Z]+[1-9][0-9]*$/i.test(key) || worksheet[key] == null)
175
- continue;
176
- try {
177
- const cell = utils.decode_cell(key);
178
- bounds.rowCount = Math.max(bounds.rowCount, cell.r + 1);
179
- bounds.colCount = Math.max(bounds.colCount, cell.c + 1);
180
- }
181
- catch {
211
+ else {
212
+ for (const key of Object.keys(worksheet)) {
213
+ if (key.startsWith('!') || !/^[A-Z]+[1-9][0-9]*$/i.test(key) || worksheet[key] == null)
214
+ continue;
215
+ try {
216
+ const cell = utils.decode_cell(key);
217
+ observe(cell.r, cell.c, worksheet[key]);
218
+ }
219
+ catch {
220
+ }
182
221
  }
183
222
  }
223
+ bounds.rowCount = compactSparseExtent(rowIndices, RANGE_ROW_SLACK, CONTENT_GAP_ROW_MULTIPLIER, contentRows);
224
+ bounds.colCount = compactSparseExtent(colIndices, RANGE_COLUMN_SLACK, CONTENT_GAP_COL_MULTIPLIER, contentCols);
184
225
  return bounds;
185
226
  };
186
227
  const getMergeBounds = (worksheet) => {
@@ -222,8 +263,14 @@ export const getWorksheetDisplayBounds = (worksheet, charts) => {
222
263
  const chartBounds = getChartBounds(charts);
223
264
  const observedRowCount = Math.max(cellBounds.rowCount, mergeBounds.rowCount, drawingBounds.rowCount, chartBounds.rowCount);
224
265
  const observedColCount = Math.max(cellBounds.colCount, mergeBounds.colCount, drawingBounds.colCount, chartBounds.colCount);
225
- const rowCount = reconcileDeclaredRange(declaredRowCount, observedRowCount, RANGE_ROW_SLACK, EMPTY_RANGE_ROW_LIMIT);
226
- const colCount = reconcileDeclaredRange(declaredColCount, observedColCount, RANGE_COLUMN_SLACK, EMPTY_RANGE_COLUMN_LIMIT);
266
+ const reconciledRowCount = reconcileDeclaredRange(declaredRowCount, observedRowCount, RANGE_ROW_SLACK, EMPTY_RANGE_ROW_LIMIT);
267
+ const reconciledColCount = reconcileDeclaredRange(declaredColCount, observedColCount, RANGE_COLUMN_SLACK, EMPTY_RANGE_COLUMN_LIMIT);
268
+ const rowCount = reconciledRowCount;
269
+ const colCount = reconciledColCount;
270
+ const trimmed = (rowCount < declaredRowCount
271
+ || colCount < declaredColCount
272
+ || rowCount < reconciledRowCount
273
+ || colCount < reconciledColCount);
227
274
  return {
228
275
  rowCount,
229
276
  colCount,
@@ -231,7 +278,7 @@ export const getWorksheetDisplayBounds = (worksheet, charts) => {
231
278
  declaredColCount,
232
279
  observedRowCount,
233
280
  observedColCount,
234
- trimmed: rowCount < declaredRowCount || colCount < declaredColCount,
281
+ trimmed,
235
282
  };
236
283
  };
237
284
  const parseSheets = (context, includeEmptySheets = false) => {
@@ -268,6 +315,7 @@ export const parseSpreadsheetWorkbook = async (context, data, source = {}) => {
268
315
  try {
269
316
  const input = prepareSpreadsheetReadInput(data, source);
270
317
  context.workbook = readWorkbookWithMergeRecovery(input);
318
+ recoverUncachedFormulaValues(context.workbook);
271
319
  const signature = data.byteLength >= 2 ? new DataView(data).getUint16(0, false) : 0;
272
320
  if (signature === 0x504b) {
273
321
  const [charts, cellImages] = await Promise.all([