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

Fixed export to csv, properly parses data with special characters #1027

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Changed to csv. Fixed parsing errors, now will parse fields with comm…
…as or other special characters properly. #1000
  • Loading branch information
allennatang committed Jan 4, 2025
commit 6299d6f4d839ff37a0a31ac54456a8a71973206f
33 changes: 30 additions & 3 deletions src/features/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -276,28 +276,55 @@ 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 comma-separated value (CSV)
if (free_response == true){

// If the value contains any quotes, newlines, tabs, or commas, wrap it in double quotes
if (/['"/n/t,]/.test(value)) {
value = `"${value}"`
}
}

} else {
value = result.hacker[header.key];
}
row.push(value);
});
csvData.push(row.join('\t'));
csvData.push(row.join(','));
}
});
fileDownload(csvData.join('\n'), 'hackerData.tsv', 'text/tsv');

// Make sure that file is encoded as UTF-8 so that all characters are properly displayed
const bom = '\uFEFF'; // UTF-8 BOM
fileDownload(bom + csvData.join('\n'), 'hackerData.csv', 'text/csv');

}

private async triggerSearch(): Promise<void> {