Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV exports containing newlines, commas, or quotes are incorrectly formatted #2179

Merged
merged 7 commits into from
Dec 3, 2024
Merged
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