megha
09/14/2020, 3:44 PMstalbert
09/14/2020, 4:22 PMmichoel
09/14/2020, 11:14 PMfunction convertToCSV(data) {
const csvHeader =
"Reportable Product Parent,Reportable Product,Period,Subsidiary,Posted Revenue,Planned Revenue,Total Revenue";
const columns = [
"reportable_product_parent",
"reportable_product_text",
"period_text",
"subsidiary_text",
"posted_revenue",
"planned_revenue",
"total_revenue",
];
const csvLines = data.map((row) => {
// wrap each string csv field in quotation marks, and escape quotation marks in value
const formatCSVField = (value) => (typeof value === "string" ? `"${value.replace('"', '""')}"` : value);
const formattedFields = columns.map((column) => formatCSVField(row[column]));
return formattedFields.join(",");
});
return csvHeader + "\n" + csvLines.join("\n");
}
michoel
09/14/2020, 11:16 PM