@file-viewer/renderer-spreadsheet 3.3.4 → 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/dist/spreadsheet/chartRenderer.js +203 -132
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +3 -0
- package/dist/spreadsheet/worker/sheetjs/chartParser.d.ts +1 -1
- package/dist/spreadsheet/worker/sheetjs/chartParser.js +130 -24
- package/dist/spreadsheet/worker/type.d.ts +13 -0
- package/dist/worker/sheet.worker.js +49 -49
- package/package.json +3 -3
|
@@ -4,6 +4,7 @@ import { readStoredRowSizing } from './rowSizing.js';
|
|
|
4
4
|
import { readBoundedZipText } from './xmlBudget.js';
|
|
5
5
|
import { DOMParser } from '@xmldom/xmldom';
|
|
6
6
|
import JSZip from 'jszip';
|
|
7
|
+
import { SSF } from 'styled-exceljs';
|
|
7
8
|
import { readStoredSheetLayout, readWorkbookDigitWidth } from './sheetLayout.js';
|
|
8
9
|
import { extremaPointIndexes, MAX_TRANSFERRED_LINE_CHART_POINTS } from '../../chartSampling.js';
|
|
9
10
|
const CHART_RELATIONSHIP_SUFFIX = '/chart';
|
|
@@ -331,7 +332,47 @@ const parseSeriesMarker = (series) => {
|
|
|
331
332
|
size: Number.isFinite(sizeValue) && sizeValue > 0 ? sizeValue : undefined
|
|
332
333
|
};
|
|
333
334
|
};
|
|
334
|
-
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) => {
|
|
335
376
|
const chartType = CHART_TYPE_MAP[localName(chartNode)];
|
|
336
377
|
const compactLineSeries = chartType === 'line'
|
|
337
378
|
|| chartType === 'area'
|
|
@@ -341,7 +382,12 @@ const parseSeries = (chartNode, workbook, blankMode = 'gap') => {
|
|
|
341
382
|
const tx = firstChildByLocal(series, 'tx');
|
|
342
383
|
const category = firstChildByLocal(series, 'cat') || firstChildByLocal(series, 'xVal');
|
|
343
384
|
const value = firstChildByLocal(series, 'val') || firstChildByLocal(series, 'yVal');
|
|
344
|
-
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;
|
|
345
391
|
const rawValues = parsePointValues(value, workbook, false);
|
|
346
392
|
const count = Math.max(categories.length, rawValues.length);
|
|
347
393
|
const spanBlankIndexes = [];
|
|
@@ -353,14 +399,17 @@ const parseSeries = (chartNode, workbook, blankMode = 'gap') => {
|
|
|
353
399
|
spanBlankIndexes.push(pointIndex);
|
|
354
400
|
return blankMode === 'zero' ? 0 : Number.NaN;
|
|
355
401
|
}
|
|
356
|
-
const number =
|
|
402
|
+
const number = excelNumericText.test(raw) ? Number(raw) : NaN;
|
|
357
403
|
return Number.isFinite(number) ? number : Number.NaN;
|
|
358
404
|
});
|
|
359
405
|
const result = {
|
|
360
|
-
name: chartText(tx, workbook) || `Series ${index + 1}`,
|
|
406
|
+
name: chartText(tx, workbook) || `Series ${seriesOffset + index + 1}`,
|
|
361
407
|
categories: categories.length
|
|
362
408
|
? Array.from({ length: count }, (_, i) => { var _a; return (_a = categories[i]) !== null && _a !== void 0 ? _a : ''; })
|
|
363
|
-
: values.map((_, valueIndex) =>
|
|
409
|
+
: values.map((_, valueIndex) => {
|
|
410
|
+
const label = `${valueIndex + 1}`;
|
|
411
|
+
return !category && formatDate ? formatDate(label) : label;
|
|
412
|
+
}),
|
|
364
413
|
values,
|
|
365
414
|
...(spanBlankIndexes.length ? { spanBlankIndexes } : {}),
|
|
366
415
|
color: parseSeriesColor(series),
|
|
@@ -396,39 +445,95 @@ const parseAxisFontSize = (axis, chartSpace) => {
|
|
|
396
445
|
};
|
|
397
446
|
return (_a = size(axis)) !== null && _a !== void 0 ? _a : size(chartSpace);
|
|
398
447
|
};
|
|
399
|
-
const
|
|
400
|
-
|
|
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;
|
|
401
464
|
const root = document.documentElement;
|
|
402
465
|
const chart = firstByLocal(root, 'chart');
|
|
403
466
|
const plotArea = firstChildByLocal(chart, 'plotArea') || firstByLocal(chart, 'plotArea');
|
|
404
|
-
const
|
|
467
|
+
const chartEntries = childElements(plotArea)
|
|
405
468
|
.map((element) => ({ element, type: CHART_TYPE_MAP[localName(element)] }))
|
|
406
|
-
.
|
|
469
|
+
.filter((entry) => entry.type);
|
|
470
|
+
const chartEntry = chartEntries[0];
|
|
407
471
|
if (!chartEntry) {
|
|
408
472
|
return null;
|
|
409
473
|
}
|
|
410
|
-
const
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
const
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
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;
|
|
417
519
|
return {
|
|
418
520
|
type: chartEntry.type,
|
|
419
521
|
title: chartText(firstChildByLocal(chart, 'title'), workbook) || undefined,
|
|
420
522
|
categoryAxisTitle: chartText(firstChildByLocal(categoryAxis, 'title'), workbook) || undefined,
|
|
523
|
+
categoryAxisId: supportsCombination ? axisId(categoryAxis) : undefined,
|
|
524
|
+
categoryAxisDate: categoryAxis && localName(categoryAxis) === 'dateAx' ? true : undefined,
|
|
421
525
|
valueAxisTitle: chartText(firstChildByLocal(valueAxis, 'title'), workbook) || undefined,
|
|
422
|
-
|
|
526
|
+
valueAxes,
|
|
527
|
+
barDirection: direction(chartEntry.element),
|
|
423
528
|
categoryAxisFontSize: parseAxisFontSize(categoryAxis, root),
|
|
424
529
|
valueAxisFontSize: parseAxisFontSize(valueAxis, root),
|
|
425
530
|
grouping: ((_c = firstChildByLocal(chartEntry.element, 'grouping')) === null || _c === void 0 ? void 0 : _c.getAttribute('val')) || undefined,
|
|
426
531
|
scatterStyle: ((_d = firstChildByLocal(chartEntry.element, 'scatterStyle')) === null || _d === void 0 ? void 0 : _d.getAttribute('val')) || undefined,
|
|
427
|
-
legendPosition:
|
|
428
|
-
series
|
|
532
|
+
legendPosition: legendPosition(firstChildByLocal(chart, 'legend')),
|
|
533
|
+
series
|
|
429
534
|
};
|
|
430
535
|
};
|
|
431
|
-
const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
536
|
+
const parseDrawingCharts = async (zip, drawingPart, workbook, date1904) => {
|
|
432
537
|
const [document, relationships] = await Promise.all([
|
|
433
538
|
loadXml(zip, drawingPart),
|
|
434
539
|
loadRelationships(zip, drawingPart)
|
|
@@ -445,7 +550,7 @@ const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
|
445
550
|
return null;
|
|
446
551
|
}
|
|
447
552
|
const chartDocument = await loadXml(zip, chartRelationship.target);
|
|
448
|
-
const chart = chartDocument ? parseChart(chartDocument, workbook) : null;
|
|
553
|
+
const chart = chartDocument ? parseChart(chartDocument, workbook, date1904) : null;
|
|
449
554
|
if (!chart || !chart.series.length || chart.series.every((series) => !series.values.length)) {
|
|
450
555
|
return null;
|
|
451
556
|
}
|
|
@@ -477,7 +582,7 @@ const parseDrawingCharts = async (zip, drawingPart, workbook) => {
|
|
|
477
582
|
return charts.filter((chart) => chart !== null);
|
|
478
583
|
};
|
|
479
584
|
export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
480
|
-
var _a;
|
|
585
|
+
var _a, _b;
|
|
481
586
|
const zip = await JSZip.loadAsync(data);
|
|
482
587
|
const workbookPart = 'xl/workbook.xml';
|
|
483
588
|
const [workbookDocument, workbookRelationships] = await Promise.all([
|
|
@@ -489,13 +594,14 @@ export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
|
489
594
|
return result;
|
|
490
595
|
}
|
|
491
596
|
const maximumDigitWidth = readWorkbookDigitWidth(workbook);
|
|
597
|
+
const date1904 = xmlBoolean((_a = firstChildByLocal(workbookDocument.documentElement, 'workbookPr')) === null || _a === void 0 ? void 0 : _a.getAttribute('date1904'));
|
|
492
598
|
for (const sheet of elementsByLocal(workbookDocument.documentElement, 'sheet')) {
|
|
493
599
|
const name = sheet.getAttribute('name') || '';
|
|
494
600
|
const worksheetRelationship = relationById(workbookRelationships, relationshipId(sheet));
|
|
495
601
|
if (!name || !(worksheetRelationship === null || worksheetRelationship === void 0 ? void 0 : worksheetRelationship.type.endsWith(WORKSHEET_RELATIONSHIP_SUFFIX))) {
|
|
496
602
|
continue;
|
|
497
603
|
}
|
|
498
|
-
const worksheet = (
|
|
604
|
+
const worksheet = (_b = workbook === null || workbook === void 0 ? void 0 : workbook.Sheets) === null || _b === void 0 ? void 0 : _b[name];
|
|
499
605
|
if (worksheet) {
|
|
500
606
|
worksheet['!fileViewerLayout'] = {};
|
|
501
607
|
worksheet['!fileViewerLayout'] = {
|
|
@@ -508,7 +614,7 @@ export const parseSpreadsheetCharts = async (data, workbook) => {
|
|
|
508
614
|
const drawingParts = Array.from(new Set(worksheetRelationships
|
|
509
615
|
.filter((relationship) => relationship.type.endsWith(DRAWING_RELATIONSHIP_SUFFIX))
|
|
510
616
|
.map((relationship) => relationship.target)));
|
|
511
|
-
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();
|
|
512
618
|
if (charts.length) {
|
|
513
619
|
result[name] = charts;
|
|
514
620
|
}
|
|
@@ -55,6 +55,9 @@ export interface SheetDrawingMarker {
|
|
|
55
55
|
}
|
|
56
56
|
export interface SheetChartSeries {
|
|
57
57
|
name: string;
|
|
58
|
+
type?: SheetChartType;
|
|
59
|
+
categoryAxisId?: string;
|
|
60
|
+
valueAxisId?: string;
|
|
58
61
|
categories: string[];
|
|
59
62
|
values: number[];
|
|
60
63
|
spanBlankIndexes?: number[];
|
|
@@ -69,6 +72,13 @@ export interface SheetChartSeries {
|
|
|
69
72
|
size?: number;
|
|
70
73
|
};
|
|
71
74
|
}
|
|
75
|
+
export interface SheetChartValueAxis {
|
|
76
|
+
id: string;
|
|
77
|
+
position: 'left' | 'right';
|
|
78
|
+
hidden: boolean;
|
|
79
|
+
fontSize?: number;
|
|
80
|
+
title?: string;
|
|
81
|
+
}
|
|
72
82
|
export interface SheetSourceAnchor {
|
|
73
83
|
status: 'valid' | 'unknown';
|
|
74
84
|
reason?: string;
|
|
@@ -89,9 +99,12 @@ export interface SheetChartDefinition {
|
|
|
89
99
|
type: SheetChartType;
|
|
90
100
|
title?: string;
|
|
91
101
|
categoryAxisTitle?: string;
|
|
102
|
+
categoryAxisId?: string;
|
|
103
|
+
categoryAxisDate?: boolean;
|
|
92
104
|
categoryAxisFontSize?: number;
|
|
93
105
|
valueAxisFontSize?: number;
|
|
94
106
|
valueAxisTitle?: string;
|
|
107
|
+
valueAxes?: SheetChartValueAxis[];
|
|
95
108
|
barDirection?: 'column' | 'bar';
|
|
96
109
|
grouping?: string;
|
|
97
110
|
scatterStyle?: string;
|