From 51fedb2b1ecc2d39131caf032bfbfb7f152bd43e Mon Sep 17 00:00:00 2001 From: allennatang Date: Fri, 3 Jan 2025 23:50:55 -0500 Subject: [PATCH] Changed export format back to TSV to avoid additional CSV format inconsistencies (handling quotes and newlines). Escapes tabs in free-form responses for proper TSV parsing. #1000 --- src/features/Search/Search.tsx | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/features/Search/Search.tsx b/src/features/Search/Search.tsx index 9794d66f..15022b42 100644 --- a/src/features/Search/Search.tsx +++ b/src/features/Search/Search.tsx @@ -276,19 +276,42 @@ class SearchContainer extends React.Component<{}, ISearchState> { headers.push({label: CONSTANTS.DIETARY_RESTRICTIONS_LABEL, key: 'accountId.dietaryRestrictions'}); headers.push({label: 'Authorize MLH to send emails', key: 'application.other.sendEmail'}) } + const tempHeaders: string[] = []; headers.forEach((header) => { tempHeaders.push(header.label); }); - const csvData: string[] = [tempHeaders.join('\t')]; // actually in tsv format + + const csvData: string[] = [tempHeaders.join(',')]; + this.filter().forEach((result) => { if (result.selected) { const row: string[] = []; headers.forEach((header) => { let value; + let free_response = false; + if (header.key.indexOf('.') >= 0) { + + // Check if the field is a free-form response + if (header.key == "application.shortAnswer.question1" || header.key == "application.shortAnswer.question2" || header.key == "application.shortAnswer.comments") { + free_response = true + } + + // Get the value of the field by navigating the nested structure of the hacker object, + // using the header key to determine the path const nestedAttr = header.key.split('.'); value = getNestedAttr(result.hacker, nestedAttr); + + // Format free responses to be properly parsed as tab-separated value (TSV) + if (free_response == true){ + + // If the value contains a tab, wrap it in double quotes + if (value.includes('\t')) { + value = `"${value}"`; + } + } + } else { value = result.hacker[header.key]; } @@ -297,6 +320,7 @@ class SearchContainer extends React.Component<{}, ISearchState> { csvData.push(row.join('\t')); } }); + fileDownload(csvData.join('\n'), 'hackerData.tsv', 'text/tsv'); }