diff --git a/apps/web/src/hooks/useInstrumentVisualization.ts b/apps/web/src/hooks/useInstrumentVisualization.ts index 48dad2e90..8246a7475 100644 --- a/apps/web/src/hooks/useInstrumentVisualization.ts +++ b/apps/web/src/hooks/useInstrumentVisualization.ts @@ -160,12 +160,12 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio } case 'Excel': { const rows = makeWideRows(); - downloadSubjectTableExcel(`${baseFilename}.xlsx`, rows); + downloadSubjectTableExcel(`${baseFilename}.xlsx`, rows, removeSubjectIdScope(params.subjectId)); break; } case 'Excel Long': { const rows = makeLongRows(); - downloadSubjectTableExcel(`${baseFilename}.xlsx`, rows); + downloadSubjectTableExcel(`${baseFilename}.xlsx`, rows, removeSubjectIdScope(params.subjectId)); break; } case 'JSON': { diff --git a/apps/web/src/utils/excel.ts b/apps/web/src/utils/excel.ts index 5d320b34a..74fa3a92f 100644 --- a/apps/web/src/utils/excel.ts +++ b/apps/web/src/utils/excel.ts @@ -7,8 +7,15 @@ export function downloadExcel(filename: string, recordsExport: InstrumentRecords writeFileXLSX(workbook, filename); } -export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[]) { +export function downloadSubjectTableExcel(filename: string, records: { [key: string]: any }[], name: string) { + const sanitizedName = + name + .replace(/[\\/?*[\]:]/g, '_') // Replace invalid chars + .slice(0, 31) // Max 31 chars + .replace(/^'|'$/g, '') // Remove leading/trailing apostrophes + .trim() || 'Subject'; // Fallback if empty const workbook = utils.book_new(); - utils.book_append_sheet(workbook, utils.json_to_sheet(records), 'ULTRA_LONG'); + utils.book_append_sheet(workbook, utils.json_to_sheet(records), sanitizedName); + utils.book_append_sheet(workbook, utils.json_to_sheet(records), name); writeFileXLSX(workbook, filename); }