Hi all, I wrote a scheduled script to create a cs...
# suitescript
m
Hi all, I wrote a scheduled script to create a csv file. But having a issue with comma separated field. I want the comma separated field in one column with comma in it. Now it showing in csv without comma. How can I handle it ? Thanks
s
papaparse
👍 1
m
Copy code
function 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");
}
If you want to avoid an extra library, this works pretty well by just wrapping every string in quotes (and escaping quotation marks) instead of worrying about which fields contain commas etc..
👍 1