Skip to content

Commit 843c0b6

Browse files
ramonsmitsdvdstelt
andauthored
Fix CSV export with values containing \n, , or " (#2179)
Co-authored-by: Dennis <[email protected]>
1 parent 2313b3e commit 843c0b6

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Frontend/src/components/failedmessages/FailedMessages.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,23 @@ async function retrySelected() {
129129
selectedMessages.forEach((m) => (m.retryInProgress = true));
130130
}
131131
132-
//TODO: this function doesn't work correctly, since any commas in the exception trace breaks the CSV.
133132
//Not attempting to use explicit types correctly since this will need to change eventually anyway
134133
function exportSelected() {
135134
// eslint-disable-next-line @typescript-eslint/no-explicit-any
136135
function toCSV(array: any[]) {
136+
const delimiter = ",";
137137
const keys = Object.keys(array[0]);
138-
let result = keys.join("\t") + "\n";
138+
let result = keys.join(delimiter) + "\n";
139139
array.forEach((obj) => {
140-
result += keys.map((k) => obj[k]).join(",") + "\n";
140+
result +=
141+
keys
142+
.map((k) => {
143+
let v = String(obj[k]);
144+
v = v.replaceAll('"', '""'); // Escape all double quotes
145+
if (v.search(/([",\n])/g) >= 0) v = `"${v}"`; // Quote all values to deal with CR characters
146+
return v;
147+
})
148+
.join(delimiter) + "\n";
141149
});
142150
143151
return result;

0 commit comments

Comments
 (0)