Skip to content
Advertisement

How to write a value that contains a comma to a csv file?

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.

Advertisement

Answer

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,'""') + '"' + ","
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement