mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
(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
This commit is contained in:
parent
a64fb105e3
commit
9d1cc89dc9
@ -88,7 +88,7 @@ async function convertToExcel(tables: ExportData[], testDates: boolean) {
|
|||||||
};
|
};
|
||||||
for (const table of tables) {
|
for (const table of tables) {
|
||||||
const { columns, rowIds, access, tableName } = table;
|
const { columns, rowIds, access, tableName } = table;
|
||||||
const ws = wb.addWorksheet(tableName);
|
const ws = wb.addWorksheet(sanitizeWorksheetName(tableName));
|
||||||
// Build excel formatters.
|
// Build excel formatters.
|
||||||
const formatters = columns.map(col => createExcelFormatter(col.type, col.widgetOptions));
|
const formatters = columns.map(col => createExcelFormatter(col.type, col.widgetOptions));
|
||||||
// Generate headers for all columns with correct styles for whole column.
|
// 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();
|
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…
Reference in New Issue
Block a user