(core) Strip invalid characters from table name in excel import

Summary: Add sanitizeWorksheetName function, pass result to library function addWorksheet where error was raised.

Test Plan: Added unit test for sanitizeWorksheetName function, updated a fixture document to use a messy table name.

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D3072
pull/115/head
Alex Hall 3 years ago
parent a64fb105e3
commit 9d1cc89dc9

@ -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]+$/, '');
}

Loading…
Cancel
Save