WebApp shared: Plotly partial bundle, card actions, print report, export module
Plotly partial bundle (bar, scatter, box, pie, sankey, contour) replacing the cartesian dist; card-actions (published Measure definitions, settings, Volver); Descargar reporte with a print stylesheet; export module: xlsx with frozen header, autofilter, Parámetros sheet and 5,000-row cap, pdfmake documents with header/footer, allRows following next links, file names without patient identifiers; lazy-loaded export libraries. Checks on milkpc (node:22): npm ci, ng build (initial 567 kB / 134 kB transfer), ng test 7 files passed.
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import type { Cell, Row } from 'write-excel-file/browser';
|
||||
import { validateName } from './safe-name';
|
||||
|
||||
export const MAX_ROWS = 5000;
|
||||
|
||||
export type Sheet = {
|
||||
name: string;
|
||||
columns: { header: string; key: string; type?: 'string' | 'number' | 'date' }[];
|
||||
rows: Record<string, unknown>[];
|
||||
};
|
||||
|
||||
function cell(value: unknown, type?: Sheet['columns'][number]['type']): Cell {
|
||||
if (value == null || value === '') return null;
|
||||
if (type === 'date' || (!type && value instanceof Date)) {
|
||||
const date = value instanceof Date ? value : new Date(String(value));
|
||||
return Number.isFinite(date.getTime()) ? { value: date, format: 'yyyy-mm-dd' } : null;
|
||||
}
|
||||
if (type === 'number') {
|
||||
const number = typeof value === 'string' && value.trim() ? Number(value) : value;
|
||||
return typeof number === 'number' && Number.isFinite(number) ? number : null;
|
||||
}
|
||||
if (!type && typeof value === 'number') return Number.isFinite(value) ? value : null;
|
||||
if (!type && typeof value === 'boolean') return value;
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function columnName(count: number): string {
|
||||
let name = '';
|
||||
while (count > 0) {
|
||||
count--;
|
||||
name = String.fromCharCode(65 + (count % 26)) + name;
|
||||
count = Math.floor(count / 26);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
function width(value: Cell): number {
|
||||
if (value == null) return 0;
|
||||
if (typeof value === 'object' && 'value' in value)
|
||||
return value.value instanceof Date ? 10 : String(value.value).length;
|
||||
return String(value).length;
|
||||
}
|
||||
|
||||
export async function downloadXlsx(
|
||||
name: string,
|
||||
sheets: Sheet[],
|
||||
parameters: Record<string, string>,
|
||||
): Promise<void> {
|
||||
const filename = `${validateName(name)}.xlsx`;
|
||||
const { default: writeExcelFile } = await import('write-excel-file/browser');
|
||||
const parameterSheet: Sheet = {
|
||||
name: 'Parámetros',
|
||||
columns: [
|
||||
{ header: 'Parámetro', key: 'parameter' },
|
||||
{ header: 'Valor', key: 'value' },
|
||||
],
|
||||
rows: Object.entries(parameters).map(([parameter, value]) => ({ parameter, value })),
|
||||
};
|
||||
const filterRanges: (string | undefined)[] = [];
|
||||
const workbook = [...sheets, parameterSheet].map(({ name, columns, rows }, index) => {
|
||||
const data: Row[] = [columns.map(({ header }) => ({ value: header, fontWeight: 'bold' }))];
|
||||
for (const row of rows.slice(0, MAX_ROWS)) {
|
||||
data.push(columns.map(({ key, type }) => cell(row[key], type)));
|
||||
}
|
||||
if (index < sheets.length && columns.length) {
|
||||
filterRanges[index] = `A1:${columnName(columns.length)}${data.length}`;
|
||||
}
|
||||
if (rows.length >= MAX_ROWS) {
|
||||
data.push([`Límite de exportación: ${MAX_ROWS} filas.`]);
|
||||
}
|
||||
return {
|
||||
sheet: name,
|
||||
data,
|
||||
stickyRowsCount: 1,
|
||||
columns: columns.map((_, column) => ({
|
||||
width: Math.min(80, Math.max(10, ...data.map((row) => width(row[column]))) + 2),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
await writeExcelFile(workbook, {
|
||||
features: [
|
||||
{
|
||||
files: {
|
||||
transform: {
|
||||
'xl/worksheets/sheet{id}.xml': {
|
||||
transform: (xml, _, { sheetIndex }) => {
|
||||
const ref = filterRanges[sheetIndex];
|
||||
return ref
|
||||
? xml.replace('</sheetData>', `</sheetData><autoFilter ref="${ref}"/>`)
|
||||
: xml;
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}).toFile(filename);
|
||||
}
|
||||
Reference in New Issue
Block a user