From 69ece617502039fde96f0a193d83dabe2c9ec2ca Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 25 Feb 2026 11:29:35 -0500 Subject: [PATCH] fix: preserve newlines in CSV export for readable multi-line fields Newlines within quoted CSV cells render correctly in Excel and Google Sheets. Previously all newlines were collapsed to spaces, making bullet lists and paragraphs unreadable. Co-Authored-By: Claude Opus 4.6 --- backend/src/services/csvExportService.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/src/services/csvExportService.ts b/backend/src/services/csvExportService.ts index 7f7cea9..bf380df 100644 --- a/backend/src/services/csvExportService.ts +++ b/backend/src/services/csvExportService.ts @@ -215,15 +215,16 @@ class CSVExportService { */ private static escapeCSVValue(value: string): string { if (!value) return ''; - - // Replace newlines with spaces and trim - const cleanValue = value.replace(/\n/g, ' ').replace(/\r/g, ' ').trim(); - + + // Normalize line endings but preserve newlines for readability in spreadsheet cells + const cleanValue = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n').trim(); + // If value contains comma, quote, or newline, wrap in quotes and escape internal quotes + // Excel/Google Sheets render newlines within quoted cells correctly if (cleanValue.includes(',') || cleanValue.includes('"') || cleanValue.includes('\n')) { return `"${cleanValue.replace(/"/g, '""')}"`; } - + return cleanValue; }