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:
@@ -215,15 +215,16 @@ 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, '""')}"`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cleanValue;
|
return cleanValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user