1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user1407668
  • 587
  • 5
  • 20
  • 35

2 Answers2

6

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,'""') + '"' + ","
daghan
  • 948
  • 10
  • 18
0

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

Azrael_404
  • 426
  • 1
  • 6
  • 17
  • I dont have control on that field, which has a comma within – user1407668 Mar 03 '14 at 10:39
  • 1
    you can use an ascii char and make an str replace after. ie : 1997,Ford,E350,"Super% luxurious truck" and make a str_replace(yourString,"%",",") It's working with all other chars (in case of you'll need to pass % in your data) – Azrael_404 Mar 03 '14 at 11:30
  • Thanks Azrael_404. It worked. I replaced comma with semicolon & inserted to csv – user1407668 Mar 03 '14 at 11:58