Question

I have a javascript program to write values to a csv file. I have a value that includes comma within. How to do this? I am newbie to javascript. can you please help me?

I tried double quotes

content += record.number +","+ record.category +","+ record.status +","+ record.approval_status +","+ record.requested_by +","+ record.assigned_to +","+record.assign_dept +","+ record.coordinator +","+ record.coord_phone +","+ record.planned_start +","+ record.planned_end +","+record.reason +","+ record.risk_assessment +","+ record.logical_name +","+ "record.assets" +","+ record.request_date +","+ record.subcategory +","+ record.priority_code +","+ record.impact +","+  record.sched_outage_start +","+ record.sched_outage_end  + "\n";

record.assets is the field that will have comma within.

Was it helpful?

Solution

Close them in quotes and escape double quotes?

http://en.wikipedia.org/wiki/Comma-separated_values

1999,Chevy,"Venture ""Extended Edition""",4900.00

in your case:

"," + "record.assets" + ","

should be

"," + '"' + record.assets.replace(/"/g,'""') + '"' + ","

OTHER TIPS

You can put your text between quotes.
You are not obliged to use a comma , you can use a ";" too (dutch/german format but works well)
--> http://en.wikipedia.org/wiki/Comma-separated_values
1997;Ford;E350;2,34

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top