Skip to content

Commit

Permalink
Fix CSV export with values containing \n, , or " (#2179)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis <dvdstelt@gmail.com>
  • Loading branch information
ramonsmits and dvdstelt authored Dec 3, 2024
1 parent 2313b3e commit 843c0b6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Frontend/src/components/failedmessages/FailedMessages.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,23 @@ async function retrySelected() {
selectedMessages.forEach((m) => (m.retryInProgress = true));
}
//TODO: this function doesn't work correctly, since any commas in the exception trace breaks the CSV.
//Not attempting to use explicit types correctly since this will need to change eventually anyway
function exportSelected() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function toCSV(array: any[]) {
const delimiter = ",";
const keys = Object.keys(array[0]);
let result = keys.join("\t") + "\n";
let result = keys.join(delimiter) + "\n";
array.forEach((obj) => {
result += keys.map((k) => obj[k]).join(",") + "\n";
result +=
keys
.map((k) => {
let v = String(obj[k]);
v = v.replaceAll('"', '""'); // Escape all double quotes
if (v.search(/([",\n])/g) >= 0) v = `"${v}"`; // Quote all values to deal with CR characters
return v;
})
.join(delimiter) + "\n";
});
return result;
Expand Down

0 comments on commit 843c0b6

Please sign in to comment.