I\'m trying to produce a Google Shopping feed of 30,000+ items in NetSuite, a CRM system that runs server-side JavaScript that it calls Suitescript 2.0. Essentially, it\'s j
I use this simple function to convert an string[][]
to a csv file. It quotes the cell, if it contains a "
, a ,
or other whitespace (except blanks):
/**
* Takes an array of arrays and returns a `,` sparated csv file.
* @param {string[][]} table
* @returns {string}
*/
export function toCSV(table: string[][]) {
return table
.map(row =>
row
.map(cell => {
// We remove blanks and check if the column contains
// other whitespace,`,` or `"`.
// In that case, we need to quote the column.
if (cell.replace(/ /g, '').match(/[\s,"]/)) {
return '"' + cell.replace(/"/g, '""') + '"';
}
return cell;
})
.join(',')
)
.join('\n');
}