@file-viewer/renderer-spreadsheet 2.3.2 → 3.0.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.
@@ -285,11 +285,17 @@ const mergeStyle = (...styles) => {
285
285
  });
286
286
  return Object.keys(result).length ? result : undefined;
287
287
  };
288
+ const resolveTableCellStyle = (worksheet, rowIndex, colIndex, baseStyle) => {
289
+ const resolver = utils.resolve_table_cell_style;
290
+ return typeof resolver === 'function'
291
+ ? resolver(worksheet, rowIndex, colIndex, baseStyle)
292
+ : baseStyle;
293
+ };
288
294
  const getCellStyle = (cellStyle) => {
289
295
  const style = {};
290
296
  const fill = (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.fill) || {};
291
- const fillColor = normalizeColor((cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.fgColor) || fill.fgColor || (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.bgColor) || fill.bgColor);
292
- const patternType = (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.patternType) || fill.patternType;
297
+ const fillColor = normalizeColor(fill.fgColor || (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.fgColor) || fill.bgColor || (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.bgColor));
298
+ const patternType = fill.patternType || (cellStyle === null || cellStyle === void 0 ? void 0 : cellStyle.patternType);
293
299
  if (fillColor && patternType !== 'none') {
294
300
  style.backgroundColor = fillColor;
295
301
  }
@@ -558,7 +564,8 @@ class SheetJsModel {
558
564
  for (let rowIndex = this.startRow; rowIndex < this.endRow; rowIndex += 1) {
559
565
  for (let colIndex = 0; colIndex < this.totalCols; colIndex += 1) {
560
566
  const cell = this.getCellAt(rowIndex, colIndex);
561
- const rawStyle = mergeStyle((_a = cols[colIndex]) === null || _a === void 0 ? void 0 : _a.s, (_b = rows[rowIndex]) === null || _b === void 0 ? void 0 : _b.s, cell === null || cell === void 0 ? void 0 : cell.s);
567
+ const inheritedStyle = mergeStyle((_a = cols[colIndex]) === null || _a === void 0 ? void 0 : _a.s, (_b = rows[rowIndex]) === null || _b === void 0 ? void 0 : _b.s, cell === null || cell === void 0 ? void 0 : cell.s);
568
+ const rawStyle = resolveTableCellStyle(this.ws, rowIndex, colIndex, inheritedStyle);
562
569
  const className = alignToClassName(rawStyle === null || rawStyle === void 0 ? void 0 : rawStyle.alignment);
563
570
  const style = getCellStyle(rawStyle);
564
571
  if (!className && !style) {
@@ -10,7 +10,87 @@ const readOptions = {
10
10
  cellStyles: true,
11
11
  browserPixels: true,
12
12
  drawings: true,
13
- validateMerges: true,
13
+ };
14
+ const isMergeValidationError = (error) => (error instanceof Error
15
+ && typeof error.code === 'string'
16
+ && /^E_MERGE_/.test(error.code));
17
+ const sanitizeToleratedMerges = (workbook) => {
18
+ for (const name of workbook.SheetNames || []) {
19
+ const worksheet = workbook.Sheets[name];
20
+ const merges = worksheet === null || worksheet === void 0 ? void 0 : worksheet['!merges'];
21
+ const errors = worksheet === null || worksheet === void 0 ? void 0 : worksheet['!mergeErrors'];
22
+ if (!worksheet || !(merges === null || merges === void 0 ? void 0 : merges.length) || !(errors === null || errors === void 0 ? void 0 : errors.length))
23
+ continue;
24
+ let worksheetRange;
25
+ try {
26
+ worksheetRange = worksheet['!ref'] ? utils.decode_range(worksheet['!ref']) : undefined;
27
+ }
28
+ catch {
29
+ worksheetRange = undefined;
30
+ }
31
+ const errorsByIndex = new Map();
32
+ for (const error of errors) {
33
+ const index = error.index;
34
+ if (!Number.isInteger(index) || typeof index !== 'number' || typeof error.code !== 'string')
35
+ continue;
36
+ const codes = errorsByIndex.get(index) || new Set();
37
+ codes.add(error.code);
38
+ errorsByIndex.set(index, codes);
39
+ }
40
+ worksheet['!merges'] = merges.flatMap((merge, index) => {
41
+ var _a, _b, _c, _d;
42
+ const codes = errorsByIndex.get(index);
43
+ const startRow = Number((_a = merge.s) === null || _a === void 0 ? void 0 : _a.r);
44
+ const startCol = Number((_b = merge.s) === null || _b === void 0 ? void 0 : _b.c);
45
+ let endRow = Number((_c = merge.e) === null || _c === void 0 ? void 0 : _c.r);
46
+ let endCol = Number((_d = merge.e) === null || _d === void 0 ? void 0 : _d.c);
47
+ if (!Number.isInteger(startRow)
48
+ || !Number.isInteger(startCol)
49
+ || !Number.isInteger(endRow)
50
+ || !Number.isInteger(endCol)
51
+ || startRow < 0
52
+ || startCol < 0
53
+ || endRow < startRow
54
+ || endCol < startCol
55
+ || (codes === null || codes === void 0 ? void 0 : codes.has('E_MERGE_RANGE'))
56
+ || (codes === null || codes === void 0 ? void 0 : codes.has('E_MERGE_DUP'))
57
+ || (codes === null || codes === void 0 ? void 0 : codes.has('E_MERGE_OVERLAP'))) {
58
+ return [];
59
+ }
60
+ if (codes === null || codes === void 0 ? void 0 : codes.has('E_MERGE_BOUNDS')) {
61
+ if (!worksheetRange
62
+ || startRow < worksheetRange.s.r
63
+ || startCol < worksheetRange.s.c
64
+ || startRow > worksheetRange.e.r
65
+ || startCol > worksheetRange.e.c) {
66
+ return [];
67
+ }
68
+ endRow = Math.min(endRow, worksheetRange.e.r);
69
+ endCol = Math.min(endCol, worksheetRange.e.c);
70
+ }
71
+ return [{
72
+ ...merge,
73
+ s: { ...merge.s, r: startRow, c: startCol },
74
+ e: { ...merge.e, r: endRow, c: endCol },
75
+ }];
76
+ });
77
+ }
78
+ };
79
+ const readPreparedWorkbook = (input, validateMerges) => (input.kind === 'text'
80
+ ? read(input.data, { ...readOptions, type: 'string', validateMerges })
81
+ : read(input.data, { ...readOptions, validateMerges }));
82
+ const readWorkbookWithMergeRecovery = (input) => {
83
+ try {
84
+ return readPreparedWorkbook(input, true);
85
+ }
86
+ catch (error) {
87
+ if (!isMergeValidationError(error))
88
+ throw error;
89
+ const workbook = readPreparedWorkbook(input, false);
90
+ sanitizeToleratedMerges(workbook);
91
+ console.warn(`[file-viewer] Recovered a workbook with invalid merge metadata: ${error.message}`);
92
+ return workbook;
93
+ }
14
94
  };
15
95
  export const createSpreadsheetParserContext = () => ({
16
96
  workbook: null,
@@ -188,9 +268,7 @@ const parseSheets = (context) => {
188
268
  export const parseSpreadsheetWorkbook = async (context, data, source = {}) => {
189
269
  try {
190
270
  const input = prepareSpreadsheetReadInput(data, source);
191
- context.workbook = input.kind === 'text'
192
- ? read(input.data, { ...readOptions, type: 'string' })
193
- : read(input.data, readOptions);
271
+ context.workbook = readWorkbookWithMergeRecovery(input);
194
272
  const signature = data.byteLength >= 2 ? new DataView(data).getUint16(0, false) : 0;
195
273
  if (signature === 0x504b) {
196
274
  const [charts, cellImages] = await Promise.all([