@file-viewer/renderer-spreadsheet 2.2.8 → 2.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.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -6
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.d.ts +5 -1
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +34 -4
- package/dist/spreadsheet/worker/sheetjs/chartParser.d.ts +16 -1
- package/dist/spreadsheet/worker/sheetjs/chartParser.js +114 -25
- package/dist/spreadsheet/worker/sheetjs/parser.d.ts +2 -1
- package/dist/spreadsheet/worker/sheetjs/parser.js +16 -7
- package/dist/spreadsheet/worker/sheetjs/richDataParser.d.ts +2 -0
- package/dist/spreadsheet/worker/sheetjs/richDataParser.js +320 -0
- package/dist/spreadsheet/worker/sheetjs/textEncoding.d.ts +2 -1
- package/dist/spreadsheet/worker/sheetjs/textEncoding.js +3 -116
- package/dist/spreadsheet/worker/type.d.ts +7 -0
- package/dist/spreadsheet.js +97 -2
- package/dist/worker/sheet.worker.js +67 -51
- package/package.json +5 -4
package/README.en.md
CHANGED
|
@@ -32,7 +32,7 @@ const options = {
|
|
|
32
32
|
- CSV / TSV automatically detects UTF-8 (with or without BOM) and GBK / GB18030. For known inputs, set `options.spreadsheet.textEncoding` to `utf-8`, `gbk`, or `gb18030`.
|
|
33
33
|
- Defaults to `options.spreadsheet.worker: 'auto'`: small files keep the main-thread compatibility path, while files at or above `options.spreadsheet.workerAutoThreshold` (1MB by default) automatically try the static worker.
|
|
34
34
|
- Can force the static worker with `options.spreadsheet.worker: true`, disable auto worker with `worker: false`, and override custom static paths with `options.spreadsheet.workerUrl`.
|
|
35
|
-
- Supports multiple sheets, horizontally scrollable sheet tabs, merged cells, row and column sizing, borders, fills, alignment, text color, workbook images, the global zoom provider, and optional header drag column resizing.
|
|
35
|
+
- Supports multiple sheets, horizontally scrollable sheet tabs, merged cells, row and column sizing, borders, fills, alignment, text color, Office 365 and WPS in-cell images, workbook drawing images, double-click image previews, the global zoom provider, and optional header drag column resizing.
|
|
36
36
|
- Uses virtual rendering for interaction. Host components usually hide full-document print for spreadsheets to avoid printing only the current viewport.
|
|
37
37
|
|
|
38
38
|
## Offline Assets
|
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ const options = {
|
|
|
32
32
|
- CSV / TSV 默认自动识别 UTF-8(含 BOM)与 GBK / GB18030;遇到来源编码已知的文件时,可通过 `options.spreadsheet.textEncoding` 固定为 `utf-8`、`gbk` 或 `gb18030`。
|
|
33
33
|
- 默认 `options.spreadsheet.worker: 'auto'`:小文件走主线程兼容路径,大文件达到 `options.spreadsheet.workerAutoThreshold`(默认 1MB)后自动尝试静态 Worker。
|
|
34
34
|
- 可以通过 `options.spreadsheet.worker: true` 强制启用静态 Worker,通过 `worker: false` 关闭自动启用;静态路径特殊时配置 `options.spreadsheet.workerUrl`。
|
|
35
|
-
- 支持多 sheet
|
|
35
|
+
- 支持多 sheet、横向滚动标签栏、合并单元格、列宽/行高、边框、填充、对齐、文本颜色、Office 365 / WPS 单元格内嵌图片、workbook drawing 图片、图片双击大图预览、统一缩放 provider 和可选表头拖拽调整列宽。
|
|
36
36
|
- 表格使用虚拟渲染,适合交互预览;完整打印按钮通常由上层能力判断隐藏,避免只打印当前视口。
|
|
37
37
|
|
|
38
38
|
## 离线资产
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type FileRenderHandler, type FileViewerRenderedInstance, type FileViewerRendererPlugin, type RendererDefinition } from '@file-viewer/core';
|
|
2
2
|
export declare const spreadsheetRendererDefinition: RendererDefinition;
|
|
3
|
+
export declare const dbfRendererDefinition: RendererDefinition;
|
|
3
4
|
export declare const renderFileViewerSpreadsheet: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
4
5
|
export declare const spreadsheetRenderer: FileViewerRendererPlugin<FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>>;
|
|
5
6
|
export default spreadsheetRenderer;
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
2
|
const spreadsheetDefinition = DEFAULT_RENDERER_DEFINITIONS.find(definition => definition.id === 'spreadsheet-openxml');
|
|
3
|
-
|
|
3
|
+
const dbfDefinition = DEFAULT_RENDERER_DEFINITIONS.find(definition => definition.id === 'spreadsheet-dbf');
|
|
4
|
+
if (!spreadsheetDefinition || !dbfDefinition) {
|
|
4
5
|
throw new Error('@file-viewer/renderer-spreadsheet could not locate the shared Spreadsheet format definition.');
|
|
5
6
|
}
|
|
6
7
|
export const spreadsheetRendererDefinition = spreadsheetDefinition;
|
|
8
|
+
export const dbfRendererDefinition = dbfDefinition;
|
|
7
9
|
export const renderFileViewerSpreadsheet = (buffer, target, type, context) => import('./spreadsheet.js').then(({ default: renderSpreadsheet }) => renderSpreadsheet(buffer, target, type, context));
|
|
8
10
|
export const spreadsheetRenderer = {
|
|
9
11
|
id: 'file-viewer-renderer-spreadsheet',
|
|
10
12
|
label: 'Flyfish File Viewer Spreadsheet renderer',
|
|
11
|
-
definitions: [spreadsheetRendererDefinition],
|
|
12
|
-
handlers: [{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
definitions: [spreadsheetRendererDefinition, dbfRendererDefinition],
|
|
14
|
+
handlers: [spreadsheetRendererDefinition, dbfRendererDefinition].map(definition => ({
|
|
15
|
+
rendererId: definition.id,
|
|
16
|
+
handler: renderFileViewerSpreadsheet,
|
|
17
|
+
})),
|
|
16
18
|
};
|
|
17
19
|
export default spreadsheetRenderer;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Range, WorkSheet } from 'styled-exceljs';
|
|
2
|
-
import type { CellMerge, SheetChartDefinition, SheetColumn, SheetModel, SheetStructure, SheetWindow } from '../type.js';
|
|
2
|
+
import type { CellMerge, SheetChartDefinition, SheetCellImage, SheetColumn, SheetModel, SheetStructure, SheetWindow } from '../type.js';
|
|
3
3
|
export declare const createAutoFitSampleRanges: (totalRowCount: number, totalColCount: number) => Range[];
|
|
4
4
|
export declare const defaults: {
|
|
5
5
|
rowHeight: number;
|
|
@@ -11,6 +11,7 @@ interface SheetSliceOptions {
|
|
|
11
11
|
totalRows?: number;
|
|
12
12
|
totalCols?: number;
|
|
13
13
|
charts?: SheetChartDefinition[];
|
|
14
|
+
cellImages?: SheetCellImage[];
|
|
14
15
|
}
|
|
15
16
|
interface SheetObjectOptions {
|
|
16
17
|
includeLayout?: boolean;
|
|
@@ -26,6 +27,8 @@ export default class SheetJsModel implements SheetModel {
|
|
|
26
27
|
private readonly _totalRows?;
|
|
27
28
|
private readonly _totalCols?;
|
|
28
29
|
private readonly _charts;
|
|
30
|
+
private readonly _cellImages;
|
|
31
|
+
private readonly _cellImageKeys;
|
|
29
32
|
private static readonly defaults;
|
|
30
33
|
private _data;
|
|
31
34
|
private _cell;
|
|
@@ -73,6 +76,7 @@ export default class SheetJsModel implements SheetModel {
|
|
|
73
76
|
private getAutoFitSampleRanges;
|
|
74
77
|
private get autoFitColumns();
|
|
75
78
|
private getColumnMeta;
|
|
79
|
+
private get hasAnchoredObjects();
|
|
76
80
|
private getColWidths;
|
|
77
81
|
private getColumns;
|
|
78
82
|
private getStructure;
|
|
@@ -349,6 +349,8 @@ class SheetJsModel {
|
|
|
349
349
|
this._totalRows = options.totalRows;
|
|
350
350
|
this._totalCols = options.totalCols;
|
|
351
351
|
this._charts = options.charts || [];
|
|
352
|
+
this._cellImages = options.cellImages || [];
|
|
353
|
+
this._cellImageKeys = new Set(this._cellImages.map((image) => cellKey(image.row, image.col)));
|
|
352
354
|
const { '!ref': refs } = ws;
|
|
353
355
|
this.range = utils.decode_range(refs || 'A1');
|
|
354
356
|
}
|
|
@@ -447,10 +449,10 @@ class SheetJsModel {
|
|
|
447
449
|
getImages() {
|
|
448
450
|
const drawings = this.ws['!drawings'];
|
|
449
451
|
const images = (drawings === null || drawings === void 0 ? void 0 : drawings.images) || [];
|
|
450
|
-
if (!images.length) {
|
|
452
|
+
if (!images.length && !this._cellImages.length) {
|
|
451
453
|
return undefined;
|
|
452
454
|
}
|
|
453
|
-
const
|
|
455
|
+
const drawingImages = images.flatMap((image, index) => {
|
|
454
456
|
var _a, _b, _c, _d, _e;
|
|
455
457
|
const anchor = image.anchor;
|
|
456
458
|
if (!image.dataURI || !anchor) {
|
|
@@ -473,6 +475,21 @@ class SheetJsModel {
|
|
|
473
475
|
col: (from === null || from === void 0 ? void 0 : from.col) || 0
|
|
474
476
|
}];
|
|
475
477
|
});
|
|
478
|
+
const cellImages = this._cellImages.flatMap((image) => {
|
|
479
|
+
const width = getVectorSize(this.getColWidths(), image.col, this.defaults.colWidth);
|
|
480
|
+
const height = getVectorSize(this.getAllRowHeights(), image.row, this.defaults.rowHeight);
|
|
481
|
+
if (width <= 0 || height <= 0) {
|
|
482
|
+
return [];
|
|
483
|
+
}
|
|
484
|
+
return [{
|
|
485
|
+
...image,
|
|
486
|
+
left: this.getAxisOffset(image.col, this.getColWidths(), this.defaults.colWidth),
|
|
487
|
+
top: this.getAxisOffset(image.row, this.getAllRowHeights(), this.defaults.rowHeight),
|
|
488
|
+
width,
|
|
489
|
+
height
|
|
490
|
+
}];
|
|
491
|
+
});
|
|
492
|
+
const result = [...drawingImages, ...cellImages];
|
|
476
493
|
return result.length ? result : undefined;
|
|
477
494
|
}
|
|
478
495
|
getCharts() {
|
|
@@ -526,8 +543,10 @@ class SheetJsModel {
|
|
|
526
543
|
for (let rowIndex = this.startRow; rowIndex < this.endRow; rowIndex += 1) {
|
|
527
544
|
const row = rows === null || rows === void 0 ? void 0 : rows[rowIndex];
|
|
528
545
|
const values = row
|
|
529
|
-
? row.slice(0, this.totalCols).map(cell => formatCellValue(cell))
|
|
530
|
-
: Array.from({ length: this.totalCols }, (_, colIndex) =>
|
|
546
|
+
? row.slice(0, this.totalCols).map((cell, colIndex) => (this._cellImageKeys.has(cellKey(rowIndex, colIndex)) ? '' : formatCellValue(cell)))
|
|
547
|
+
: Array.from({ length: this.totalCols }, (_, colIndex) => (this._cellImageKeys.has(cellKey(rowIndex, colIndex))
|
|
548
|
+
? ''
|
|
549
|
+
: formatCellValue(this.getCellAt(rowIndex, colIndex))));
|
|
531
550
|
result.push(values);
|
|
532
551
|
}
|
|
533
552
|
return fixMatrix(result, this.totalCols);
|
|
@@ -650,9 +669,20 @@ class SheetJsModel {
|
|
|
650
669
|
if (hasColumnWidth(sourceColumn)) {
|
|
651
670
|
return sourceColumn;
|
|
652
671
|
}
|
|
672
|
+
// A drawing anchor is measured against the workbook's stored/default
|
|
673
|
+
// column geometry. Auto-fitting blank columns to 24px changes that
|
|
674
|
+
// coordinate system and makes two-cell images visibly smaller than Excel.
|
|
675
|
+
if (this.hasAnchoredObjects) {
|
|
676
|
+
return sourceColumn;
|
|
677
|
+
}
|
|
653
678
|
// 没有显式列宽时,再使用 styled-exceljs 的内容测量兜底,避免报表类标题污染原始宽度。
|
|
654
679
|
return ((_a = this.autoFitColumns) === null || _a === void 0 ? void 0 : _a[colIndex]) || sourceColumn;
|
|
655
680
|
}
|
|
681
|
+
get hasAnchoredObjects() {
|
|
682
|
+
var _a;
|
|
683
|
+
const drawings = this.ws['!drawings'];
|
|
684
|
+
return !!((_a = drawings === null || drawings === void 0 ? void 0 : drawings.images) === null || _a === void 0 ? void 0 : _a.length) || !!this._charts.length || !!this._cellImages.length;
|
|
685
|
+
}
|
|
656
686
|
getColWidths() {
|
|
657
687
|
const { colWidth } = this.defaults;
|
|
658
688
|
const { '!cols': sourceCols = [] } = this.ws;
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
+
import { type Document as XmlDocument, type Element as XmlElement, type Node as XmlNode } from '@xmldom/xmldom';
|
|
2
|
+
import JSZip from 'jszip';
|
|
3
|
+
import type { WorkBook } from 'styled-exceljs';
|
|
1
4
|
import type { SheetChartDefinition } from '../type.js';
|
|
2
|
-
export
|
|
5
|
+
export type Relationship = {
|
|
6
|
+
id: string;
|
|
7
|
+
target: string;
|
|
8
|
+
type: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const localName: (node: XmlNode) => string;
|
|
11
|
+
export declare const childElements: (node: XmlNode | null | undefined) => XmlElement[];
|
|
12
|
+
export declare const elementsByLocal: (node: XmlNode | null | undefined, name: string) => XmlElement[];
|
|
13
|
+
export declare const relationshipId: (element: XmlElement | undefined) => string;
|
|
14
|
+
export declare const loadXml: (zip: JSZip, path: string) => Promise<XmlDocument | null>;
|
|
15
|
+
export declare const loadRelationships: (zip: JSZip, sourcePart: string) => Promise<Relationship[]>;
|
|
16
|
+
export declare const relationById: (relationships: Relationship[], id: string) => Relationship | undefined;
|
|
17
|
+
export declare const parseSpreadsheetCharts: (data: ArrayBuffer, workbook?: WorkBook | null) => Promise<Record<string, SheetChartDefinition[]>>;
|
|
@@ -37,11 +37,11 @@ const SCHEME_COLORS = {
|
|
|
37
37
|
tx1: '#000000',
|
|
38
38
|
tx2: '#44546a'
|
|
39
39
|
};
|
|
40
|
-
const localName = (node) => {
|
|
40
|
+
export const localName = (node) => {
|
|
41
41
|
const name = node.localName || node.nodeName;
|
|
42
42
|
return name.split(':').pop() || name;
|
|
43
43
|
};
|
|
44
|
-
const childElements = (node) => {
|
|
44
|
+
export const childElements = (node) => {
|
|
45
45
|
if (!node) {
|
|
46
46
|
return [];
|
|
47
47
|
}
|
|
@@ -53,7 +53,7 @@ const childrenByLocal = (node, name) => {
|
|
|
53
53
|
const firstChildByLocal = (node, name) => {
|
|
54
54
|
return childrenByLocal(node, name)[0];
|
|
55
55
|
};
|
|
56
|
-
const elementsByLocal = (node, name) => {
|
|
56
|
+
export const elementsByLocal = (node, name) => {
|
|
57
57
|
const result = [];
|
|
58
58
|
const visit = (current) => {
|
|
59
59
|
childElements(current).forEach((child) => {
|
|
@@ -79,7 +79,7 @@ const textContent = (element) => {
|
|
|
79
79
|
var _a;
|
|
80
80
|
return ((_a = element === null || element === void 0 ? void 0 : element.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
|
|
81
81
|
};
|
|
82
|
-
const relationshipId = (element) => {
|
|
82
|
+
export const relationshipId = (element) => {
|
|
83
83
|
if (!element) {
|
|
84
84
|
return '';
|
|
85
85
|
}
|
|
@@ -112,16 +112,19 @@ const relationshipPartPath = (sourcePart) => {
|
|
|
112
112
|
return `${directory ? `${directory}/` : ''}_rels/${filename}.rels`;
|
|
113
113
|
};
|
|
114
114
|
const parseXml = (xml) => {
|
|
115
|
-
|
|
115
|
+
// Some valid Office producers prefix relationship parts with a UTF-8 BOM.
|
|
116
|
+
// XML declarations must otherwise be the first character, and xmldom reports
|
|
117
|
+
// the BOM as content outside the root element before aborting chart parsing.
|
|
118
|
+
return new DOMParser().parseFromString(xml.replace(/^[\uFEFF\s]+/, ''), 'application/xml');
|
|
116
119
|
};
|
|
117
|
-
const loadXml = async (zip, path) => {
|
|
120
|
+
export const loadXml = async (zip, path) => {
|
|
118
121
|
const file = zip.file(path);
|
|
119
122
|
if (!file) {
|
|
120
123
|
return null;
|
|
121
124
|
}
|
|
122
125
|
return parseXml(await file.async('text'));
|
|
123
126
|
};
|
|
124
|
-
const loadRelationships = async (zip, sourcePart) => {
|
|
127
|
+
export const loadRelationships = async (zip, sourcePart) => {
|
|
125
128
|
const document = await loadXml(zip, relationshipPartPath(sourcePart));
|
|
126
129
|
if (!document) {
|
|
127
130
|
return [];
|
|
@@ -136,7 +139,7 @@ const loadRelationships = async (zip, sourcePart) => {
|
|
|
136
139
|
return [{ id, target: resolvePartPath(sourcePart, target), type }];
|
|
137
140
|
});
|
|
138
141
|
};
|
|
139
|
-
const relationById = (relationships, id) => {
|
|
142
|
+
export const relationById = (relationships, id) => {
|
|
140
143
|
return relationships.find((relationship) => relationship.id === id);
|
|
141
144
|
};
|
|
142
145
|
const parseMarker = (element) => {
|
|
@@ -150,23 +153,109 @@ const parseMarker = (element) => {
|
|
|
150
153
|
colOff: Number(textContent(firstChildByLocal(element, 'colOff'))) || 0
|
|
151
154
|
};
|
|
152
155
|
};
|
|
153
|
-
const
|
|
156
|
+
const columnIndex = (letters) => {
|
|
157
|
+
let result = 0;
|
|
158
|
+
for (const letter of letters.toUpperCase()) {
|
|
159
|
+
result = result * 26 + letter.charCodeAt(0) - 64;
|
|
160
|
+
}
|
|
161
|
+
return result - 1;
|
|
162
|
+
};
|
|
163
|
+
const parseCellAddress = (address) => {
|
|
164
|
+
const match = /^\$?([A-Z]{1,3})\$?(\d+)$/i.exec(address.trim());
|
|
165
|
+
if (!match) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
col: columnIndex(match[1]),
|
|
170
|
+
row: Number(match[2]) - 1
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
const encodeCellAddress = (row, col) => {
|
|
174
|
+
let value = col + 1;
|
|
175
|
+
let letters = '';
|
|
176
|
+
while (value > 0) {
|
|
177
|
+
const remainder = (value - 1) % 26;
|
|
178
|
+
letters = String.fromCharCode(65 + remainder) + letters;
|
|
179
|
+
value = Math.floor((value - 1) / 26);
|
|
180
|
+
}
|
|
181
|
+
return `${letters}${row + 1}`;
|
|
182
|
+
};
|
|
183
|
+
const getWorksheetCell = (worksheet, row, col) => {
|
|
184
|
+
var _a, _b;
|
|
185
|
+
return ((_b = (_a = worksheet['!data']) === null || _a === void 0 ? void 0 : _a[row]) === null || _b === void 0 ? void 0 : _b[col])
|
|
186
|
+
|| worksheet[encodeCellAddress(row, col)];
|
|
187
|
+
};
|
|
188
|
+
const parseFormulaRange = (formula) => {
|
|
189
|
+
const normalized = formula.trim().replace(/^=/, '');
|
|
190
|
+
const separator = normalized.lastIndexOf('!');
|
|
191
|
+
if (separator <= 0) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
const sheetToken = normalized.slice(0, separator).trim();
|
|
195
|
+
const rangeToken = normalized.slice(separator + 1).trim();
|
|
196
|
+
if (!sheetToken || sheetToken.includes('[')) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
const sheetName = sheetToken.startsWith("'") && sheetToken.endsWith("'")
|
|
200
|
+
? sheetToken.slice(1, -1).replace(/''/g, "'")
|
|
201
|
+
: sheetToken;
|
|
202
|
+
const [startToken, endToken = startToken] = rangeToken.split(':');
|
|
203
|
+
const start = parseCellAddress(startToken);
|
|
204
|
+
const end = parseCellAddress(endToken);
|
|
205
|
+
if (!sheetName || !start || !end) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
sheetName,
|
|
210
|
+
start: {
|
|
211
|
+
row: Math.min(start.row, end.row),
|
|
212
|
+
col: Math.min(start.col, end.col)
|
|
213
|
+
},
|
|
214
|
+
end: {
|
|
215
|
+
row: Math.max(start.row, end.row),
|
|
216
|
+
col: Math.max(start.col, end.col)
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
const resolveFormulaValues = (formula, workbook, formatted) => {
|
|
221
|
+
var _a;
|
|
222
|
+
const range = parseFormulaRange(formula);
|
|
223
|
+
const worksheet = range && ((_a = workbook === null || workbook === void 0 ? void 0 : workbook.Sheets) === null || _a === void 0 ? void 0 : _a[range.sheetName]);
|
|
224
|
+
if (!range || !worksheet) {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
const values = [];
|
|
228
|
+
for (let row = range.start.row; row <= range.end.row; row += 1) {
|
|
229
|
+
for (let col = range.start.col; col <= range.end.col; col += 1) {
|
|
230
|
+
const cell = getWorksheetCell(worksheet, row, col);
|
|
231
|
+
const value = formatted && (cell === null || cell === void 0 ? void 0 : cell.w) !== undefined ? cell.w : cell === null || cell === void 0 ? void 0 : cell.v;
|
|
232
|
+
values.push(value === undefined || value === null ? '' : `${value}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return values;
|
|
236
|
+
};
|
|
237
|
+
const parsePointValues = (element, workbook, formatted = true) => {
|
|
154
238
|
if (!element) {
|
|
155
239
|
return [];
|
|
156
240
|
}
|
|
157
|
-
|
|
241
|
+
const cachedValues = elementsByLocal(element, 'pt')
|
|
158
242
|
.map((point) => ({
|
|
159
243
|
index: Number(point.getAttribute('idx')) || 0,
|
|
160
244
|
value: textContent(firstChildByLocal(point, 'v')) || textContent(firstByLocal(point, 'v'))
|
|
161
245
|
}))
|
|
162
246
|
.sort((left, right) => left.index - right.index)
|
|
163
247
|
.map((point) => point.value);
|
|
248
|
+
if (cachedValues.length) {
|
|
249
|
+
return cachedValues;
|
|
250
|
+
}
|
|
251
|
+
const formula = textContent(firstByLocal(element, 'f'));
|
|
252
|
+
return formula ? resolveFormulaValues(formula, workbook, formatted) : [];
|
|
164
253
|
};
|
|
165
|
-
const chartText = (element) => {
|
|
254
|
+
const chartText = (element, workbook) => {
|
|
166
255
|
if (!element) {
|
|
167
256
|
return '';
|
|
168
257
|
}
|
|
169
|
-
const points = parsePointValues(element);
|
|
258
|
+
const points = parsePointValues(element, workbook);
|
|
170
259
|
if (points.length) {
|
|
171
260
|
return points.join(' ').trim();
|
|
172
261
|
}
|
|
@@ -187,15 +276,15 @@ const parseSeriesColor = (series) => {
|
|
|
187
276
|
const scheme = ((_b = firstByLocal(solidFill, 'schemeClr')) === null || _b === void 0 ? void 0 : _b.getAttribute('val')) || '';
|
|
188
277
|
return SCHEME_COLORS[scheme];
|
|
189
278
|
};
|
|
190
|
-
const parseSeries = (chartNode) => {
|
|
279
|
+
const parseSeries = (chartNode, workbook) => {
|
|
191
280
|
return childrenByLocal(chartNode, 'ser').map((series, index) => {
|
|
192
281
|
const tx = firstChildByLocal(series, 'tx');
|
|
193
282
|
const category = firstChildByLocal(series, 'cat') || firstChildByLocal(series, 'xVal');
|
|
194
283
|
const value = firstChildByLocal(series, 'val') || firstChildByLocal(series, 'yVal');
|
|
195
|
-
const categories = parsePointValues(category);
|
|
196
|
-
const values = parsePointValues(value).map(Number).filter(Number.isFinite);
|
|
284
|
+
const categories = parsePointValues(category, workbook);
|
|
285
|
+
const values = parsePointValues(value, workbook, false).map(Number).filter(Number.isFinite);
|
|
197
286
|
return {
|
|
198
|
-
name: chartText(tx) || `Series ${index + 1}`,
|
|
287
|
+
name: chartText(tx, workbook) || `Series ${index + 1}`,
|
|
199
288
|
categories: categories.length
|
|
200
289
|
? categories
|
|
201
290
|
: values.map((_, valueIndex) => `${valueIndex + 1}`),
|
|
@@ -204,7 +293,7 @@ const parseSeries = (chartNode) => {
|
|
|
204
293
|
};
|
|
205
294
|
});
|
|
206
295
|
};
|
|
207
|
-
const parseChart = (document) => {
|
|
296
|
+
const parseChart = (document, workbook) => {
|
|
208
297
|
var _a, _b, _c;
|
|
209
298
|
const root = document.documentElement;
|
|
210
299
|
const chart = firstByLocal(root, 'chart');
|
|
@@ -224,16 +313,16 @@ const parseChart = (document) => {
|
|
|
224
313
|
: 'column';
|
|
225
314
|
return {
|
|
226
315
|
type: chartEntry.type,
|
|
227
|
-
title: chartText(firstChildByLocal(chart, 'title')) || undefined,
|
|
228
|
-
categoryAxisTitle: chartText(firstChildByLocal(categoryAxis, 'title')) || undefined,
|
|
229
|
-
valueAxisTitle: chartText(firstChildByLocal(valueAxis, 'title')) || undefined,
|
|
316
|
+
title: chartText(firstChildByLocal(chart, 'title'), workbook) || undefined,
|
|
317
|
+
categoryAxisTitle: chartText(firstChildByLocal(categoryAxis, 'title'), workbook) || undefined,
|
|
318
|
+
valueAxisTitle: chartText(firstChildByLocal(valueAxis, 'title'), workbook) || undefined,
|
|
230
319
|
barDirection,
|
|
231
320
|
grouping: ((_c = firstChildByLocal(chartEntry.element, 'grouping')) === null || _c === void 0 ? void 0 : _c.getAttribute('val')) || undefined,
|
|
232
321
|
legendPosition: legend ? LEGEND_POSITION_MAP[legendPositionValue] || 'bottom' : undefined,
|
|
233
|
-
series: parseSeries(chartEntry.element)
|
|
322
|
+
series: parseSeries(chartEntry.element, workbook)
|
|
234
323
|
};
|
|
235
324
|
};
|
|
236
|
-
const parseDrawingCharts = async (zip, drawingPart) => {
|
|
325
|
+
const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
237
326
|
const [document, relationships] = await Promise.all([
|
|
238
327
|
loadXml(zip, drawingPart),
|
|
239
328
|
loadRelationships(zip, drawingPart)
|
|
@@ -250,7 +339,7 @@ const parseDrawingCharts = async (zip, drawingPart) => {
|
|
|
250
339
|
return null;
|
|
251
340
|
}
|
|
252
341
|
const chartDocument = await loadXml(zip, chartRelationship.target);
|
|
253
|
-
const chart = chartDocument ? parseChart(chartDocument) : null;
|
|
342
|
+
const chart = chartDocument ? parseChart(chartDocument, workbook) : null;
|
|
254
343
|
if (!chart || !chart.series.length || chart.series.every((series) => !series.values.length)) {
|
|
255
344
|
return null;
|
|
256
345
|
}
|
|
@@ -278,7 +367,7 @@ const parseDrawingCharts = async (zip, drawingPart) => {
|
|
|
278
367
|
}));
|
|
279
368
|
return charts.filter((chart) => chart !== null);
|
|
280
369
|
};
|
|
281
|
-
export const parseSpreadsheetCharts = async (data) => {
|
|
370
|
+
export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
282
371
|
const zip = await JSZip.loadAsync(data);
|
|
283
372
|
const workbookPart = 'xl/workbook.xml';
|
|
284
373
|
const [workbookDocument, workbookRelationships] = await Promise.all([
|
|
@@ -304,7 +393,7 @@ export const parseSpreadsheetCharts = async (data) => {
|
|
|
304
393
|
const drawingParts = Array.from(new Set(worksheetRelationships
|
|
305
394
|
.filter((relationship) => relationship.type.endsWith(DRAWING_RELATIONSHIP_SUFFIX))
|
|
306
395
|
.map((relationship) => relationship.target)));
|
|
307
|
-
const charts = (await Promise.all(drawingParts.map((part) => parseDrawingCharts(zip, part)))).flat();
|
|
396
|
+
const charts = (await Promise.all(drawingParts.map((part) => parseDrawingCharts(zip, part, workbook)))).flat();
|
|
308
397
|
if (charts.length) {
|
|
309
398
|
result[name] = charts;
|
|
310
399
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { WorkBook } from 'styled-exceljs';
|
|
2
2
|
import { type SpreadsheetTextSource } from './textEncoding.js';
|
|
3
|
-
import type { SheetChartDefinition, SheetDefinition } from '../type.js';
|
|
3
|
+
import type { SheetCellImage, SheetChartDefinition, SheetDefinition } from '../type.js';
|
|
4
4
|
interface DrawingMarkerLike {
|
|
5
5
|
row?: number;
|
|
6
6
|
col?: number;
|
|
@@ -20,6 +20,7 @@ export interface SpreadsheetParserContext {
|
|
|
20
20
|
workbook: WorkBook | null;
|
|
21
21
|
sheets: SheetDefinition[];
|
|
22
22
|
charts: Record<string, SheetChartDefinition[]>;
|
|
23
|
+
cellImages: Record<string, SheetCellImage[]>;
|
|
23
24
|
}
|
|
24
25
|
export interface SpreadsheetWorkerRequest {
|
|
25
26
|
type: string;
|
|
@@ -2,6 +2,7 @@ import { read, utils } from 'styled-exceljs';
|
|
|
2
2
|
import SheetJsModel from './SheetJsModel.js';
|
|
3
3
|
import { parseSpreadsheetCharts } from './chartParser.js';
|
|
4
4
|
import { prepareSpreadsheetReadInput, } from './textEncoding.js';
|
|
5
|
+
import { parseSpreadsheetCellImages } from './richDataParser.js';
|
|
5
6
|
const readOptions = {
|
|
6
7
|
type: 'array',
|
|
7
8
|
dense: true,
|
|
@@ -15,6 +16,7 @@ export const createSpreadsheetParserContext = () => ({
|
|
|
15
16
|
workbook: null,
|
|
16
17
|
sheets: [],
|
|
17
18
|
charts: {},
|
|
19
|
+
cellImages: {},
|
|
18
20
|
});
|
|
19
21
|
const toErrorResponse = (error, payload = {}) => ({
|
|
20
22
|
type: 'parseError',
|
|
@@ -191,16 +193,22 @@ export const parseSpreadsheetWorkbook = async (context, data, source = {}) => {
|
|
|
191
193
|
: read(input.data, readOptions);
|
|
192
194
|
const signature = data.byteLength >= 2 ? new DataView(data).getUint16(0, false) : 0;
|
|
193
195
|
if (signature === 0x504b) {
|
|
194
|
-
|
|
195
|
-
context.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
196
|
+
const [charts, cellImages] = await Promise.all([
|
|
197
|
+
parseSpreadsheetCharts(data, context.workbook).catch((error) => {
|
|
198
|
+
console.warn('[file-viewer] Spreadsheet chart parsing failed; continuing with cell content.', error);
|
|
199
|
+
return {};
|
|
200
|
+
}),
|
|
201
|
+
parseSpreadsheetCellImages(data).catch((error) => {
|
|
202
|
+
console.warn('[file-viewer] Spreadsheet cell image parsing failed; continuing with cell content.', error);
|
|
203
|
+
return {};
|
|
204
|
+
}),
|
|
205
|
+
]);
|
|
206
|
+
context.charts = charts;
|
|
207
|
+
context.cellImages = cellImages;
|
|
201
208
|
}
|
|
202
209
|
else {
|
|
203
210
|
context.charts = {};
|
|
211
|
+
context.cellImages = {};
|
|
204
212
|
}
|
|
205
213
|
return parseSheets(context);
|
|
206
214
|
}
|
|
@@ -228,6 +236,7 @@ export const parseSpreadsheetSheet = (context, payload = {}) => {
|
|
|
228
236
|
totalRows: sheetMeta === null || sheetMeta === void 0 ? void 0 : sheetMeta.rowCount,
|
|
229
237
|
totalCols: sheetMeta === null || sheetMeta === void 0 ? void 0 : sheetMeta.colCount,
|
|
230
238
|
charts: context.charts[sheetName],
|
|
239
|
+
cellImages: context.cellImages[sheetName],
|
|
231
240
|
});
|
|
232
241
|
// Keep the first response backward-compatible; later virtual windows only need rows and cells.
|
|
233
242
|
// Avoid recalculating auto-fit column widths for every 500-row request.
|