diff --git a/app/server/lib/ExportXLSX.ts b/app/server/lib/ExportXLSX.ts index fa111a02..8100c858 100644 --- a/app/server/lib/ExportXLSX.ts +++ b/app/server/lib/ExportXLSX.ts @@ -88,7 +88,7 @@ async function convertToExcel(tables: ExportData[], testDates: boolean) { }; for (const table of tables) { const { columns, rowIds, access, tableName } = table; - const ws = wb.addWorksheet(tableName); + const ws = wb.addWorksheet(sanitizeWorksheetName(tableName)); // Build excel formatters. const formatters = columns.map(col => createExcelFormatter(col.type, col.widgetOptions)); // Generate headers for all columns with correct styles for whole column. @@ -120,3 +120,19 @@ async function convertToExcel(tables: ExportData[], testDates: boolean) { return await wb.xlsx.writeBuffer(); } + +/** + * Removes invalid characters, see https://github.com/exceljs/exceljs/pull/1484 + */ +export function sanitizeWorksheetName(tableName: string): string { + return tableName + // Convert invalid characters to spaces + .replace(/[*?:/\\[\]]/g, ' ') + + // Collapse multiple spaces into one + .replace(/\s+/g, ' ') + + // Trim spaces and single quotes from the ends + .replace(/^['\s]+/, '') + .replace(/['\s]+$/, ''); +}