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 <noreply@anthropic.com>
This commit is contained in:
admin
2026-02-25 11:29:35 -05:00
parent 9007c4b270
commit 69ece61750

View File

@@ -216,10 +216,11 @@ class CSVExportService {
private static escapeCSVValue(value: string): string { private static escapeCSVValue(value: string): string {
if (!value) return ''; if (!value) return '';
// Replace newlines with spaces and trim // Normalize line endings but preserve newlines for readability in spreadsheet cells
const cleanValue = value.replace(/\n/g, ' ').replace(/\r/g, ' ').trim(); 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 // 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')) { if (cleanValue.includes(',') || cleanValue.includes('"') || cleanValue.includes('\n')) {
return `"${cleanValue.replace(/"/g, '""')}"`; return `"${cleanValue.replace(/"/g, '""')}"`;
} }