@file-viewer/renderer-spreadsheet 3.3.3 → 3.3.5
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 +14 -0
- package/README.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/spreadsheet/autoRowHeight.d.ts +16 -0
- package/dist/spreadsheet/autoRowHeight.js +208 -0
- package/dist/spreadsheet/chartRenderer.js +203 -132
- package/dist/spreadsheet/comments.d.ts +40 -0
- package/dist/spreadsheet/comments.js +388 -0
- package/dist/spreadsheet/drawingAutoHeight.d.ts +7 -0
- package/dist/spreadsheet/drawingAutoHeight.js +51 -0
- package/dist/spreadsheet/sourceRegion.d.ts +37 -0
- package/dist/spreadsheet/sourceRegion.js +54 -0
- package/dist/spreadsheet/state.d.ts +7 -1
- package/dist/spreadsheet/state.js +6 -0
- package/dist/spreadsheet/view.d.ts +9 -4
- package/dist/spreadsheet/view.js +98 -27
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.d.ts +61 -2
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +137 -28
- package/dist/spreadsheet/worker/sheetjs/chartParser.d.ts +4 -3
- package/dist/spreadsheet/worker/sheetjs/chartParser.js +143 -29
- package/dist/spreadsheet/worker/sheetjs/commentParser.d.ts +7 -0
- package/dist/spreadsheet/worker/sheetjs/commentParser.js +232 -0
- package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.d.ts +4 -0
- package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.js +90 -0
- package/dist/spreadsheet/worker/sheetjs/parser.d.ts +40 -3
- package/dist/spreadsheet/worker/sheetjs/parser.js +452 -54
- package/dist/spreadsheet/worker/sheetjs/richDataParser.js +5 -2
- package/dist/spreadsheet/worker/sheetjs/rowSizing.d.ts +10 -0
- package/dist/spreadsheet/worker/sheetjs/rowSizing.js +156 -0
- package/dist/spreadsheet/worker/sheetjs/sheetLayout.d.ts +2 -0
- package/dist/spreadsheet/worker/sheetjs/sourceAnchor.d.ts +10 -0
- package/dist/spreadsheet/worker/sheetjs/sourceAnchor.js +286 -0
- package/dist/spreadsheet/worker/sheetjs/textEncoding.d.ts +1 -1
- package/dist/spreadsheet/worker/sheetjs/textEncoding.js +1 -1
- package/dist/spreadsheet/worker/sheetjs/workbookFont.d.ts +4 -0
- package/dist/spreadsheet/worker/sheetjs/workbookFont.js +15 -0
- package/dist/spreadsheet/worker/sheetjs/xmlBudget.d.ts +11 -0
- package/dist/spreadsheet/worker/sheetjs/xmlBudget.js +39 -0
- package/dist/spreadsheet/worker/type.d.ts +158 -0
- package/dist/spreadsheet.js +150 -39
- package/dist/worker/sheet.worker.js +43 -59
- package/package.json +23 -3
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { readSourceAnchor } from './sourceAnchor.js';
|
|
2
|
+
import { createDrawingRowAnchor } from './drawingRowAnchor.js';
|
|
3
|
+
import { readStoredRowSizing } from './rowSizing.js';
|
|
4
|
+
import { readBoundedZipText } from './xmlBudget.js';
|
|
1
5
|
import { DOMParser } from '@xmldom/xmldom';
|
|
2
6
|
import JSZip from 'jszip';
|
|
7
|
+
import { SSF } from 'styled-exceljs';
|
|
3
8
|
import { readStoredSheetLayout, readWorkbookDigitWidth } from './sheetLayout.js';
|
|
4
9
|
import { extremaPointIndexes, MAX_TRANSFERRED_LINE_CHART_POINTS } from '../../chartSampling.js';
|
|
5
10
|
const CHART_RELATIONSHIP_SUFFIX = '/chart';
|
|
@@ -118,15 +123,15 @@ const relationshipPartPath = (sourcePart) => {
|
|
|
118
123
|
const parseXml = (xml) => {
|
|
119
124
|
return new DOMParser().parseFromString(xml.replace(/^[\uFEFF\s]+/, ''), 'application/xml');
|
|
120
125
|
};
|
|
121
|
-
export const loadXml = async (zip, path) => {
|
|
126
|
+
export const loadXml = async (zip, path, budget) => {
|
|
122
127
|
const file = zip.file(path);
|
|
123
128
|
if (!file) {
|
|
124
129
|
return null;
|
|
125
130
|
}
|
|
126
|
-
return parseXml(await file.async('text'));
|
|
131
|
+
return parseXml(budget ? await readBoundedZipText(file, budget) : await file.async('text'));
|
|
127
132
|
};
|
|
128
|
-
export const loadRelationships = async (zip, sourcePart) => {
|
|
129
|
-
const document = await
|
|
133
|
+
export const loadRelationships = async (zip, sourcePart, readPart = (path) => loadXml(zip, path)) => {
|
|
134
|
+
const document = await readPart(relationshipPartPath(sourcePart));
|
|
130
135
|
if (!document) {
|
|
131
136
|
return [];
|
|
132
137
|
}
|
|
@@ -327,7 +332,47 @@ const parseSeriesMarker = (series) => {
|
|
|
327
332
|
size: Number.isFinite(sizeValue) && sizeValue > 0 ? sizeValue : undefined
|
|
328
333
|
};
|
|
329
334
|
};
|
|
330
|
-
const
|
|
335
|
+
const xmlBoolean = (value) => value === '1' || value === 'true';
|
|
336
|
+
const excelNumericText = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
337
|
+
const categoryDateFormatter = (format, date1904) => {
|
|
338
|
+
if (!format || format.length > 255)
|
|
339
|
+
return undefined;
|
|
340
|
+
try {
|
|
341
|
+
if (!SSF.is_date(format))
|
|
342
|
+
return undefined;
|
|
343
|
+
return (value) => {
|
|
344
|
+
if (!excelNumericText.test(value.trim()))
|
|
345
|
+
return value;
|
|
346
|
+
const serial = Number(value);
|
|
347
|
+
try {
|
|
348
|
+
return serial >= 0 && serial < 2958466 ? SSF.format(format, serial, { date1904 }) || value : value;
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
const categoryFormat = (category, axis) => {
|
|
360
|
+
const numFmt = firstChildByLocal(axis, 'numFmt');
|
|
361
|
+
const axisFormat = (numFmt === null || numFmt === void 0 ? void 0 : numFmt.getAttribute('formatCode')) || undefined;
|
|
362
|
+
const sourceLinked = numFmt === null || numFmt === void 0 ? void 0 : numFmt.getAttribute('sourceLinked');
|
|
363
|
+
const cache = firstChildByLocal(category, 'numLit') ||
|
|
364
|
+
firstChildByLocal(firstChildByLocal(category, 'numRef'), 'numCache');
|
|
365
|
+
const cacheFormat = textContent(firstChildByLocal(cache, 'formatCode'));
|
|
366
|
+
if (axisFormat && (sourceLinked === '0' || sourceLinked === 'false'))
|
|
367
|
+
return axisFormat;
|
|
368
|
+
if (cacheFormat && cacheFormat !== 'General')
|
|
369
|
+
return cacheFormat;
|
|
370
|
+
if (cache || !category) {
|
|
371
|
+
return axisFormat || (axis && localName(axis) === 'dateAx' ? 'm/d/yyyy' : undefined);
|
|
372
|
+
}
|
|
373
|
+
return undefined;
|
|
374
|
+
};
|
|
375
|
+
const parseSeries = (chartNode, workbook, blankMode, categoryAxis, date1904, seriesOffset) => {
|
|
331
376
|
const chartType = CHART_TYPE_MAP[localName(chartNode)];
|
|
332
377
|
const compactLineSeries = chartType === 'line'
|
|
333
378
|
|| chartType === 'area'
|
|
@@ -337,7 +382,12 @@ const parseSeries = (chartNode, workbook, blankMode = 'gap') => {
|
|
|
337
382
|
const tx = firstChildByLocal(series, 'tx');
|
|
338
383
|
const category = firstChildByLocal(series, 'cat') || firstChildByLocal(series, 'xVal');
|
|
339
384
|
const value = firstChildByLocal(series, 'val') || firstChildByLocal(series, 'yVal');
|
|
340
|
-
const
|
|
385
|
+
const format = categoryFormat(category, categoryAxis);
|
|
386
|
+
const textCategories = firstChildByLocal(category, 'strRef') ||
|
|
387
|
+
firstChildByLocal(category, 'strLit') || firstChildByLocal(category, 'multiLvlStrRef');
|
|
388
|
+
const formatDate = textCategories ? undefined : categoryDateFormatter(format, date1904);
|
|
389
|
+
const rawCategories = parsePointValues(category, workbook, !format || !!textCategories);
|
|
390
|
+
const categories = formatDate ? rawCategories.map(formatDate) : rawCategories;
|
|
341
391
|
const rawValues = parsePointValues(value, workbook, false);
|
|
342
392
|
const count = Math.max(categories.length, rawValues.length);
|
|
343
393
|
const spanBlankIndexes = [];
|
|
@@ -349,14 +399,17 @@ const parseSeries = (chartNode, workbook, blankMode = 'gap') => {
|
|
|
349
399
|
spanBlankIndexes.push(pointIndex);
|
|
350
400
|
return blankMode === 'zero' ? 0 : Number.NaN;
|
|
351
401
|
}
|
|
352
|
-
const number =
|
|
402
|
+
const number = excelNumericText.test(raw) ? Number(raw) : NaN;
|
|
353
403
|
return Number.isFinite(number) ? number : Number.NaN;
|
|
354
404
|
});
|
|
355
405
|
const result = {
|
|
356
|
-
name: chartText(tx, workbook) || `Series ${index + 1}`,
|
|
406
|
+
name: chartText(tx, workbook) || `Series ${seriesOffset + index + 1}`,
|
|
357
407
|
categories: categories.length
|
|
358
408
|
? Array.from({ length: count }, (_, i) => { var _a; return (_a = categories[i]) !== null && _a !== void 0 ? _a : ''; })
|
|
359
|
-
: values.map((_, valueIndex) =>
|
|
409
|
+
: values.map((_, valueIndex) => {
|
|
410
|
+
const label = `${valueIndex + 1}`;
|
|
411
|
+
return !category && formatDate ? formatDate(label) : label;
|
|
412
|
+
}),
|
|
360
413
|
values,
|
|
361
414
|
...(spanBlankIndexes.length ? { spanBlankIndexes } : {}),
|
|
362
415
|
color: parseSeriesColor(series),
|
|
@@ -392,39 +445,95 @@ const parseAxisFontSize = (axis, chartSpace) => {
|
|
|
392
445
|
};
|
|
393
446
|
return (_a = size(axis)) !== null && _a !== void 0 ? _a : size(chartSpace);
|
|
394
447
|
};
|
|
395
|
-
const
|
|
396
|
-
|
|
448
|
+
const axisId = (axis) => { var _a; return ((_a = firstChildByLocal(axis, 'axId')) === null || _a === void 0 ? void 0 : _a.getAttribute('val')) || undefined; };
|
|
449
|
+
const isCategoryAxis = (axis) => ['catAx', 'dateAx'].includes(localName(axis));
|
|
450
|
+
const legendPosition = (legend) => {
|
|
451
|
+
var _a, _b, _c, _d;
|
|
452
|
+
if (!legend)
|
|
453
|
+
return undefined;
|
|
454
|
+
const position = ((_a = firstChildByLocal(legend, 'legendPos')) === null || _a === void 0 ? void 0 : _a.getAttribute('val')) || '';
|
|
455
|
+
const manual = firstByLocal(firstChildByLocal(legend, 'layout'), 'manualLayout');
|
|
456
|
+
const y = Number((_c = (_b = firstChildByLocal(manual, 'y')) === null || _b === void 0 ? void 0 : _b.getAttribute('val')) !== null && _c !== void 0 ? _c : NaN);
|
|
457
|
+
if (position === 'b' && ((_d = firstChildByLocal(manual, 'yMode')) === null || _d === void 0 ? void 0 : _d.getAttribute('val')) === 'edge' && y >= 0 && y < 0.15) {
|
|
458
|
+
return 'top';
|
|
459
|
+
}
|
|
460
|
+
return LEGEND_POSITION_MAP[position] || 'bottom';
|
|
461
|
+
};
|
|
462
|
+
const parseChart = (document, workbook, workbookDate1904) => {
|
|
463
|
+
var _a, _b, _c, _d;
|
|
397
464
|
const root = document.documentElement;
|
|
398
465
|
const chart = firstByLocal(root, 'chart');
|
|
399
466
|
const plotArea = firstChildByLocal(chart, 'plotArea') || firstByLocal(chart, 'plotArea');
|
|
400
|
-
const
|
|
467
|
+
const chartEntries = childElements(plotArea)
|
|
401
468
|
.map((element) => ({ element, type: CHART_TYPE_MAP[localName(element)] }))
|
|
402
|
-
.
|
|
469
|
+
.filter((entry) => entry.type);
|
|
470
|
+
const chartEntry = chartEntries[0];
|
|
403
471
|
if (!chartEntry) {
|
|
404
472
|
return null;
|
|
405
473
|
}
|
|
406
|
-
const
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
474
|
+
const direction = (element) => { var _a; return ((_a = firstChildByLocal(element, 'barDir')) === null || _a === void 0 ? void 0 : _a.getAttribute('val')) === 'bar' ? 'bar' : 'column'; };
|
|
475
|
+
const supportsCombination = chartEntries.every(({ element, type }) => type === 'line' || (type === 'bar' && direction(element) === 'column'))
|
|
476
|
+
&& chartEntries.some(entry => entry.type === 'line') && chartEntries.some(entry => entry.type === 'bar');
|
|
477
|
+
const axes = childElements(plotArea);
|
|
478
|
+
const categoryAxes = axes.filter(isCategoryAxis);
|
|
479
|
+
const valueAxisElements = axes.filter(axis => localName(axis) === 'valAx');
|
|
480
|
+
const boundAxes = (element) => {
|
|
481
|
+
const ids = childrenByLocal(element, 'axId').map(id => id.getAttribute('val'));
|
|
482
|
+
return {
|
|
483
|
+
category: categoryAxes.find(axis => ids.includes(axisId(axis) || '')),
|
|
484
|
+
value: valueAxisElements.find(axis => ids.includes(axisId(axis) || '')),
|
|
485
|
+
};
|
|
486
|
+
};
|
|
487
|
+
const primaryAxes = boundAxes(chartEntry.element);
|
|
488
|
+
const categoryAxis = primaryAxes.category || categoryAxes[0];
|
|
489
|
+
const valueAxis = primaryAxes.value || valueAxisElements[0];
|
|
490
|
+
const dateEpoch = firstChildByLocal(root, 'date1904');
|
|
491
|
+
const date1904 = dateEpoch ? xmlBoolean((_a = dateEpoch.getAttribute('val')) !== null && _a !== void 0 ? _a : '1') : workbookDate1904;
|
|
492
|
+
const blankMode = ((_b = firstChildByLocal(chart, 'dispBlanksAs')) === null || _b === void 0 ? void 0 : _b.getAttribute('val')) || 'gap';
|
|
493
|
+
const series = [];
|
|
494
|
+
for (const entry of supportsCombination ? chartEntries : [chartEntry]) {
|
|
495
|
+
const binding = boundAxes(entry.element);
|
|
496
|
+
const entries = parseSeries(entry.element, workbook, blankMode, binding.category || categoryAxis, date1904, series.length);
|
|
497
|
+
for (const item of entries) {
|
|
498
|
+
if (supportsCombination) {
|
|
499
|
+
item.type = entry.type;
|
|
500
|
+
item.categoryAxisId = axisId(binding.category);
|
|
501
|
+
item.valueAxisId = axisId(binding.value || valueAxis);
|
|
502
|
+
}
|
|
503
|
+
series.push(item);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const valueAxes = supportsCombination ? valueAxisElements.flatMap((axis) => {
|
|
507
|
+
var _a, _b;
|
|
508
|
+
const id = axisId(axis);
|
|
509
|
+
if (!id || !series.some(item => item.valueAxisId === id))
|
|
510
|
+
return [];
|
|
511
|
+
return [{
|
|
512
|
+
id,
|
|
513
|
+
position: ((_a = firstChildByLocal(axis, 'axPos')) === null || _a === void 0 ? void 0 : _a.getAttribute('val')) === 'r' ? 'right' : 'left',
|
|
514
|
+
hidden: xmlBoolean((_b = firstChildByLocal(axis, 'delete')) === null || _b === void 0 ? void 0 : _b.getAttribute('val')),
|
|
515
|
+
fontSize: parseAxisFontSize(axis, root),
|
|
516
|
+
title: chartText(firstChildByLocal(axis, 'title'), workbook) || undefined
|
|
517
|
+
}];
|
|
518
|
+
}) : undefined;
|
|
413
519
|
return {
|
|
414
520
|
type: chartEntry.type,
|
|
415
521
|
title: chartText(firstChildByLocal(chart, 'title'), workbook) || undefined,
|
|
416
522
|
categoryAxisTitle: chartText(firstChildByLocal(categoryAxis, 'title'), workbook) || undefined,
|
|
523
|
+
categoryAxisId: supportsCombination ? axisId(categoryAxis) : undefined,
|
|
524
|
+
categoryAxisDate: categoryAxis && localName(categoryAxis) === 'dateAx' ? true : undefined,
|
|
417
525
|
valueAxisTitle: chartText(firstChildByLocal(valueAxis, 'title'), workbook) || undefined,
|
|
418
|
-
|
|
526
|
+
valueAxes,
|
|
527
|
+
barDirection: direction(chartEntry.element),
|
|
419
528
|
categoryAxisFontSize: parseAxisFontSize(categoryAxis, root),
|
|
420
529
|
valueAxisFontSize: parseAxisFontSize(valueAxis, root),
|
|
421
530
|
grouping: ((_c = firstChildByLocal(chartEntry.element, 'grouping')) === null || _c === void 0 ? void 0 : _c.getAttribute('val')) || undefined,
|
|
422
531
|
scatterStyle: ((_d = firstChildByLocal(chartEntry.element, 'scatterStyle')) === null || _d === void 0 ? void 0 : _d.getAttribute('val')) || undefined,
|
|
423
|
-
legendPosition:
|
|
424
|
-
series
|
|
532
|
+
legendPosition: legendPosition(firstChildByLocal(chart, 'legend')),
|
|
533
|
+
series
|
|
425
534
|
};
|
|
426
535
|
};
|
|
427
|
-
const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
536
|
+
const parseDrawingCharts = async (zip, drawingPart, workbook, date1904) => {
|
|
428
537
|
const [document, relationships] = await Promise.all([
|
|
429
538
|
loadXml(zip, drawingPart),
|
|
430
539
|
loadRelationships(zip, drawingPart)
|
|
@@ -441,10 +550,11 @@ const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
|
441
550
|
return null;
|
|
442
551
|
}
|
|
443
552
|
const chartDocument = await loadXml(zip, chartRelationship.target);
|
|
444
|
-
const chart = chartDocument ? parseChart(chartDocument, workbook) : null;
|
|
553
|
+
const chart = chartDocument ? parseChart(chartDocument, workbook, date1904) : null;
|
|
445
554
|
if (!chart || !chart.series.length || chart.series.every((series) => !series.values.length)) {
|
|
446
555
|
return null;
|
|
447
556
|
}
|
|
557
|
+
const sourceAnchor = readSourceAnchor(anchor, chartReference, 'chart');
|
|
448
558
|
const from = parseMarker(firstChildByLocal(anchor, 'from')) || {
|
|
449
559
|
row: 0,
|
|
450
560
|
col: 0,
|
|
@@ -456,6 +566,8 @@ const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
|
456
566
|
const name = (_a = firstByLocal(anchor, 'cNvPr')) === null || _a === void 0 ? void 0 : _a.getAttribute('name');
|
|
457
567
|
return {
|
|
458
568
|
...chart,
|
|
569
|
+
sourceAnchor,
|
|
570
|
+
rowAnchor: createDrawingRowAnchor(localName(anchor), anchor.getAttribute('editAs'), from, to),
|
|
459
571
|
id: name || chartRelationship.target || `chart-${index + 1}`,
|
|
460
572
|
from,
|
|
461
573
|
to,
|
|
@@ -470,7 +582,7 @@ const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
|
470
582
|
return charts.filter((chart) => chart !== null);
|
|
471
583
|
};
|
|
472
584
|
export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
473
|
-
var _a;
|
|
585
|
+
var _a, _b;
|
|
474
586
|
const zip = await JSZip.loadAsync(data);
|
|
475
587
|
const workbookPart = 'xl/workbook.xml';
|
|
476
588
|
const [workbookDocument, workbookRelationships] = await Promise.all([
|
|
@@ -482,25 +594,27 @@ export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
|
482
594
|
return result;
|
|
483
595
|
}
|
|
484
596
|
const maximumDigitWidth = readWorkbookDigitWidth(workbook);
|
|
597
|
+
const date1904 = xmlBoolean((_a = firstChildByLocal(workbookDocument.documentElement, 'workbookPr')) === null || _a === void 0 ? void 0 : _a.getAttribute('date1904'));
|
|
485
598
|
for (const sheet of elementsByLocal(workbookDocument.documentElement, 'sheet')) {
|
|
486
599
|
const name = sheet.getAttribute('name') || '';
|
|
487
600
|
const worksheetRelationship = relationById(workbookRelationships, relationshipId(sheet));
|
|
488
601
|
if (!name || !(worksheetRelationship === null || worksheetRelationship === void 0 ? void 0 : worksheetRelationship.type.endsWith(WORKSHEET_RELATIONSHIP_SUFFIX))) {
|
|
489
602
|
continue;
|
|
490
603
|
}
|
|
491
|
-
const worksheet = (
|
|
604
|
+
const worksheet = (_b = workbook === null || workbook === void 0 ? void 0 : workbook.Sheets) === null || _b === void 0 ? void 0 : _b[name];
|
|
492
605
|
if (worksheet) {
|
|
493
606
|
worksheet['!fileViewerLayout'] = {};
|
|
494
607
|
worksheet['!fileViewerLayout'] = {
|
|
495
608
|
...await readStoredSheetLayout(zip, worksheetRelationship.target),
|
|
496
|
-
maximumDigitWidth
|
|
609
|
+
maximumDigitWidth,
|
|
610
|
+
rowSizing: await readStoredRowSizing(zip, worksheetRelationship.target)
|
|
497
611
|
};
|
|
498
612
|
}
|
|
499
613
|
const worksheetRelationships = await loadRelationships(zip, worksheetRelationship.target);
|
|
500
614
|
const drawingParts = Array.from(new Set(worksheetRelationships
|
|
501
615
|
.filter((relationship) => relationship.type.endsWith(DRAWING_RELATIONSHIP_SUFFIX))
|
|
502
616
|
.map((relationship) => relationship.target)));
|
|
503
|
-
const charts = (await Promise.all(drawingParts.map((part) => parseDrawingCharts(zip, part, workbook)))).flat();
|
|
617
|
+
const charts = (await Promise.all(drawingParts.map((part) => parseDrawingCharts(zip, part, workbook, date1904)))).flat();
|
|
504
618
|
if (charts.length) {
|
|
505
619
|
result[name] = charts;
|
|
506
620
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type WorkBook } from 'styled-exceljs';
|
|
2
|
+
import type { SheetComment } from '../type.js';
|
|
3
|
+
export declare const readWorkbookNotes: (workbook: WorkBook) => Record<string, SheetComment[]>;
|
|
4
|
+
export declare const parseSpreadsheetComments: (data: ArrayBuffer, workbook: WorkBook) => Promise<{
|
|
5
|
+
comments: Record<string, SheetComment[]>;
|
|
6
|
+
hasError: boolean;
|
|
7
|
+
}>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
import { utils } from 'styled-exceljs';
|
|
3
|
+
import { childElements, elementsByLocal, loadRelationships, loadXml, localName, relationById, relationshipId, } from './chartParser.js';
|
|
4
|
+
const MAX_COMMENT_PART_BYTES = 4 * 1024 * 1024;
|
|
5
|
+
const MAX_COMMENT_XML_BYTES = 16 * 1024 * 1024;
|
|
6
|
+
const decodeText = (value) => value.replace(/_x([0-9a-f]{4})_/gi, (_, code) => String.fromCharCode(Number.parseInt(code, 16)));
|
|
7
|
+
const text = (node) => decodeText((node === null || node === void 0 ? void 0 : node.textContent) || '');
|
|
8
|
+
const direct = (node, name) => childElements(node).filter((item) => localName(item) === name);
|
|
9
|
+
const noteText = (node) => childElements(node)
|
|
10
|
+
.flatMap((child) => {
|
|
11
|
+
if (localName(child) === 't')
|
|
12
|
+
return [text(child)];
|
|
13
|
+
return localName(child) === 'r' ? direct(child, 't').map(text) : [];
|
|
14
|
+
})
|
|
15
|
+
.join('');
|
|
16
|
+
const addressOf = (value) => {
|
|
17
|
+
if (!value || !/^[A-Z]{1,3}[1-9][0-9]{0,6}$/i.test(value))
|
|
18
|
+
return null;
|
|
19
|
+
const cell = utils.decode_cell(value.toUpperCase());
|
|
20
|
+
return cell.r < 1048576 && cell.c < 16384
|
|
21
|
+
? { address: utils.encode_cell(cell), row: cell.r, col: cell.c }
|
|
22
|
+
: null;
|
|
23
|
+
};
|
|
24
|
+
const worksheetCells = (sheet) => {
|
|
25
|
+
const rows = Array.isArray(sheet) ? sheet : sheet === null || sheet === void 0 ? void 0 : sheet['!data'];
|
|
26
|
+
if (Array.isArray(rows)) {
|
|
27
|
+
return rows.flatMap((row, r) => Array.isArray(row)
|
|
28
|
+
? row.map((cell, c) => [
|
|
29
|
+
utils.encode_cell({ r, c }),
|
|
30
|
+
cell,
|
|
31
|
+
])
|
|
32
|
+
: []);
|
|
33
|
+
}
|
|
34
|
+
return Object.entries(sheet || {}).filter(([address]) => addressOf(address));
|
|
35
|
+
};
|
|
36
|
+
const legacyMessages = (cell) => {
|
|
37
|
+
if (!cell ||
|
|
38
|
+
typeof cell !== 'object' ||
|
|
39
|
+
!('c' in cell) ||
|
|
40
|
+
!Array.isArray(cell.c))
|
|
41
|
+
return [];
|
|
42
|
+
return cell.c.flatMap((note) => {
|
|
43
|
+
if (!note ||
|
|
44
|
+
typeof note !== 'object' ||
|
|
45
|
+
!('t' in note) ||
|
|
46
|
+
typeof note.t !== 'string' ||
|
|
47
|
+
('T' in note && note.T))
|
|
48
|
+
return [];
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
id: '',
|
|
52
|
+
parentId: null,
|
|
53
|
+
text: note.t,
|
|
54
|
+
createdAt: null,
|
|
55
|
+
author: 'a' in note && typeof note.a === 'string' ? note.a : '',
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
export const readWorkbookNotes = (workbook) => Object.fromEntries(workbook.SheetNames.map((name) => [
|
|
61
|
+
name,
|
|
62
|
+
worksheetCells(workbook.Sheets[name]).flatMap(([address, cell]) => {
|
|
63
|
+
const location = addressOf(address);
|
|
64
|
+
const messages = legacyMessages(cell);
|
|
65
|
+
return location && messages.length
|
|
66
|
+
? [{ ...location, kind: 'note', resolved: false, messages }]
|
|
67
|
+
: [];
|
|
68
|
+
}),
|
|
69
|
+
]));
|
|
70
|
+
export const parseSpreadsheetComments = async (data, workbook) => {
|
|
71
|
+
const comments = readWorkbookNotes(workbook);
|
|
72
|
+
if (data.byteLength < 2 || new DataView(data).getUint16(0, false) !== 0x504b)
|
|
73
|
+
return { comments, hasError: false };
|
|
74
|
+
const zip = await JSZip.loadAsync(data);
|
|
75
|
+
if (!zip.file('xl/workbook.xml'))
|
|
76
|
+
return { comments, hasError: false };
|
|
77
|
+
let hasError = false;
|
|
78
|
+
const budget = {
|
|
79
|
+
maxPartBytes: MAX_COMMENT_PART_BYTES,
|
|
80
|
+
remainingBytes: MAX_COMMENT_XML_BYTES,
|
|
81
|
+
};
|
|
82
|
+
const cache = new Map();
|
|
83
|
+
const xml = async (path, required = true) => {
|
|
84
|
+
if (cache.has(path))
|
|
85
|
+
return cache.get(path) || null;
|
|
86
|
+
try {
|
|
87
|
+
const part = zip.file(path);
|
|
88
|
+
if (!part) {
|
|
89
|
+
if (required)
|
|
90
|
+
hasError = true;
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const document = await loadXml(zip, path, budget);
|
|
94
|
+
cache.set(path, document);
|
|
95
|
+
return document;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
hasError = true;
|
|
99
|
+
cache.set(path, null);
|
|
100
|
+
console.warn('[file-viewer] Spreadsheet comment part could not be read.', error);
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const relationshipsFor = (path) => loadRelationships(zip, path, (part) => xml(part, false));
|
|
105
|
+
const workbookXml = await xml('xl/workbook.xml');
|
|
106
|
+
const relationships = await relationshipsFor('xl/workbook.xml');
|
|
107
|
+
const people = new Map();
|
|
108
|
+
for (const relationship of relationships.filter((item) => item.type.toLowerCase().endsWith('/person'))) {
|
|
109
|
+
const persons = await xml(relationship.target);
|
|
110
|
+
for (const person of elementsByLocal(persons === null || persons === void 0 ? void 0 : persons.documentElement, 'person')) {
|
|
111
|
+
const id = person.getAttribute('id');
|
|
112
|
+
if (id)
|
|
113
|
+
people.set(id, person.getAttribute('displayName') ||
|
|
114
|
+
person.getAttribute('userId') ||
|
|
115
|
+
'');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (const sheet of elementsByLocal(workbookXml === null || workbookXml === void 0 ? void 0 : workbookXml.documentElement, 'sheet')) {
|
|
119
|
+
const name = sheet.getAttribute('name');
|
|
120
|
+
const relation = relationById(relationships, relationshipId(sheet));
|
|
121
|
+
if (!name || !relation || !relation.type.endsWith('/worksheet'))
|
|
122
|
+
continue;
|
|
123
|
+
const sheetRelationships = await relationshipsFor(relation.target);
|
|
124
|
+
const cells = new Map((comments[name] || []).map((item) => [item.address, item]));
|
|
125
|
+
for (const part of sheetRelationships.filter((item) => item.type.toLowerCase().endsWith('/comments'))) {
|
|
126
|
+
const document = await xml(part.target);
|
|
127
|
+
const authors = elementsByLocal(document === null || document === void 0 ? void 0 : document.documentElement, 'author').map(text);
|
|
128
|
+
for (const comment of elementsByLocal(document === null || document === void 0 ? void 0 : document.documentElement, 'comment')) {
|
|
129
|
+
const location = addressOf(comment.getAttribute('ref'));
|
|
130
|
+
if (!location) {
|
|
131
|
+
hasError = true;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
cells.set(location.address, {
|
|
135
|
+
...location,
|
|
136
|
+
kind: 'note',
|
|
137
|
+
resolved: false,
|
|
138
|
+
messages: [
|
|
139
|
+
{
|
|
140
|
+
id: '',
|
|
141
|
+
parentId: null,
|
|
142
|
+
author: authors[Number(comment.getAttribute('authorId'))] || '',
|
|
143
|
+
text: noteText(direct(comment, 'text')[0]),
|
|
144
|
+
createdAt: null,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const messages = [];
|
|
151
|
+
for (const part of sheetRelationships.filter((item) => item.type.toLowerCase().endsWith('/threadedcomment'))) {
|
|
152
|
+
const document = await xml(part.target);
|
|
153
|
+
for (const comment of elementsByLocal(document === null || document === void 0 ? void 0 : document.documentElement, 'threadedComment')) {
|
|
154
|
+
const location = addressOf(comment.getAttribute('ref'));
|
|
155
|
+
if (!location) {
|
|
156
|
+
hasError = true;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const timestamp = comment.getAttribute('dT') || '';
|
|
160
|
+
messages.push({
|
|
161
|
+
...location,
|
|
162
|
+
id: comment.getAttribute('id') || '',
|
|
163
|
+
parentId: comment.getAttribute('parentId') || null,
|
|
164
|
+
author: people.get(comment.getAttribute('personId') || '') || '',
|
|
165
|
+
text: text(direct(comment, 'text')[0]),
|
|
166
|
+
createdAt: timestamp && Number.isFinite(Date.parse(timestamp))
|
|
167
|
+
? timestamp
|
|
168
|
+
: null,
|
|
169
|
+
resolved: ['1', 'true'].includes(comment.getAttribute('done') || ''),
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const byId = new Map(messages.filter((item) => item.id).map((item) => [item.id, item]));
|
|
174
|
+
const threads = new Map();
|
|
175
|
+
const roots = new Map();
|
|
176
|
+
for (const message of messages) {
|
|
177
|
+
let root = message;
|
|
178
|
+
const path = [];
|
|
179
|
+
const visited = new Set();
|
|
180
|
+
while (root.parentId) {
|
|
181
|
+
const cached = roots.get(root.id);
|
|
182
|
+
if (cached) {
|
|
183
|
+
root = cached;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (visited.has(root.id)) {
|
|
187
|
+
hasError = true;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
visited.add(root.id);
|
|
191
|
+
path.push(root);
|
|
192
|
+
const parent = byId.get(root.parentId);
|
|
193
|
+
if (!parent || parent.address !== message.address) {
|
|
194
|
+
hasError = true;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
root = parent;
|
|
198
|
+
}
|
|
199
|
+
for (const item of path)
|
|
200
|
+
if (item.id)
|
|
201
|
+
roots.set(item.id, root);
|
|
202
|
+
if (root.id)
|
|
203
|
+
roots.set(root.id, root);
|
|
204
|
+
const thread = threads.get(root.address) || {
|
|
205
|
+
address: root.address,
|
|
206
|
+
row: root.row,
|
|
207
|
+
col: root.col,
|
|
208
|
+
kind: 'thread',
|
|
209
|
+
resolved: root.resolved,
|
|
210
|
+
messages: [],
|
|
211
|
+
};
|
|
212
|
+
thread.messages.push({
|
|
213
|
+
id: message.id,
|
|
214
|
+
parentId: message.parentId,
|
|
215
|
+
author: message.author,
|
|
216
|
+
text: message.text,
|
|
217
|
+
createdAt: message.createdAt,
|
|
218
|
+
});
|
|
219
|
+
threads.set(root.address, thread);
|
|
220
|
+
}
|
|
221
|
+
for (const [address, thread] of threads) {
|
|
222
|
+
thread.messages.sort((a, b) => a.parentId === null && b.parentId !== null
|
|
223
|
+
? -1
|
|
224
|
+
: b.parentId === null && a.parentId !== null
|
|
225
|
+
? 1
|
|
226
|
+
: (a.createdAt || '').localeCompare(b.createdAt || ''));
|
|
227
|
+
cells.set(address, thread);
|
|
228
|
+
}
|
|
229
|
+
comments[name] = [...cells.values()].sort((a, b) => a.row - b.row || a.col - b.col);
|
|
230
|
+
}
|
|
231
|
+
return { comments, hasError };
|
|
232
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DrawingImage } from 'styled-exceljs';
|
|
2
|
+
import type { SheetDrawingMarker, SheetRowAnchor } from '../type.js';
|
|
3
|
+
export declare const createDrawingRowAnchor: (type: string, editAs: string | null, from?: SheetDrawingMarker, to?: SheetDrawingMarker) => SheetRowAnchor | undefined;
|
|
4
|
+
export declare const readImageRowAnchors: (raw: unknown, images: DrawingImage[]) => Map<DrawingImage, SheetRowAnchor>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { DOMParser } from '@xmldom/xmldom';
|
|
2
|
+
export const createDrawingRowAnchor = (type, editAs, from, to) => {
|
|
3
|
+
if (type === 'absoluteAnchor')
|
|
4
|
+
return { mode: 'absolute', fromRow: 0, fromOffset: 0 };
|
|
5
|
+
if (type !== 'oneCellAnchor' && type !== 'twoCellAnchor')
|
|
6
|
+
return undefined;
|
|
7
|
+
if (!from ||
|
|
8
|
+
!Number.isSafeInteger(from.row) ||
|
|
9
|
+
from.row < 0 ||
|
|
10
|
+
!Number.isFinite(from.rowOff))
|
|
11
|
+
return undefined;
|
|
12
|
+
if (type === 'twoCellAnchor' &&
|
|
13
|
+
editAs &&
|
|
14
|
+
!['absolute', 'oneCell', 'twoCell'].includes(editAs))
|
|
15
|
+
return undefined;
|
|
16
|
+
const mode = type === 'oneCellAnchor' ? 'oneCell' : editAs || 'twoCell';
|
|
17
|
+
if (mode === 'twoCell' &&
|
|
18
|
+
(!to ||
|
|
19
|
+
!Number.isSafeInteger(to.row) ||
|
|
20
|
+
to.row < from.row ||
|
|
21
|
+
!Number.isFinite(to.rowOff)))
|
|
22
|
+
return undefined;
|
|
23
|
+
return {
|
|
24
|
+
mode: mode,
|
|
25
|
+
fromRow: from.row,
|
|
26
|
+
fromOffset: from.rowOff,
|
|
27
|
+
...(to ? { toRow: to.row, toOffset: to.rowOff } : {}),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export const readImageRowAnchors = (raw, images) => {
|
|
31
|
+
const result = new Map();
|
|
32
|
+
if (typeof raw !== 'string' || raw.length > 8 * 1024 * 1024)
|
|
33
|
+
return result;
|
|
34
|
+
let document;
|
|
35
|
+
try {
|
|
36
|
+
document = new DOMParser({ onError: () => { } }).parseFromString(raw, 'application/xml');
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
const local = (node) => { var _a; return node.localName || ((_a = node.nodeName) === null || _a === void 0 ? void 0 : _a.split(':').pop()) || ''; };
|
|
42
|
+
const children = (node) => Array.from((node === null || node === void 0 ? void 0 : node.childNodes) || []).filter((child) => child.nodeType === 1);
|
|
43
|
+
const descendants = (node) => Array.from((node === null || node === void 0 ? void 0 : node.getElementsByTagName('*')) || []);
|
|
44
|
+
const marker = (node) => {
|
|
45
|
+
if (!node)
|
|
46
|
+
return undefined;
|
|
47
|
+
const number = (name) => {
|
|
48
|
+
var _a;
|
|
49
|
+
return Number(((_a = children(node).find((child) => local(child) === name)) === null || _a === void 0 ? void 0 : _a.textContent) || 0);
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
row: number('row'),
|
|
53
|
+
col: number('col'),
|
|
54
|
+
rowOff: number('rowOff'),
|
|
55
|
+
colOff: number('colOff'),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const candidates = children(document.documentElement).flatMap((anchor) => {
|
|
59
|
+
const type = local(anchor), from = marker(children(anchor).find((child) => local(child) === 'from'));
|
|
60
|
+
const to = marker(children(anchor).find((child) => local(child) === 'to'));
|
|
61
|
+
const rowAnchor = createDrawingRowAnchor(type, anchor.getAttribute('editAs'), from, to);
|
|
62
|
+
if (!rowAnchor)
|
|
63
|
+
return [];
|
|
64
|
+
return descendants(anchor)
|
|
65
|
+
.filter((node) => local(node) === 'blip')
|
|
66
|
+
.map((blip) => ({
|
|
67
|
+
type,
|
|
68
|
+
from,
|
|
69
|
+
to,
|
|
70
|
+
rowAnchor,
|
|
71
|
+
id: blip.getAttributeNS('http://schemas.openxmlformats.org/officeDocument/2006/relationships', 'embed') || blip.getAttribute('r:embed'),
|
|
72
|
+
}));
|
|
73
|
+
});
|
|
74
|
+
const sameMarker = (a, b) => (!a && !b) ||
|
|
75
|
+
(!!a &&
|
|
76
|
+
!!b &&
|
|
77
|
+
['row', 'col', 'rowOff', 'colOff'].every((key) => a[key] === b[key]));
|
|
78
|
+
for (const image of images) {
|
|
79
|
+
const matches = candidates.filter((candidate) => {
|
|
80
|
+
var _a, _b, _c;
|
|
81
|
+
return candidate.id === image.id &&
|
|
82
|
+
candidate.type === ((_a = image.anchor) === null || _a === void 0 ? void 0 : _a.type) &&
|
|
83
|
+
sameMarker(candidate.from, (_b = image.anchor) === null || _b === void 0 ? void 0 : _b.from) &&
|
|
84
|
+
sameMarker(candidate.to, (_c = image.anchor) === null || _c === void 0 ? void 0 : _c.to);
|
|
85
|
+
});
|
|
86
|
+
if (matches.length === 1)
|
|
87
|
+
result.set(image, matches[0].rowAnchor);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
};
|