How to deal with Commas in CSV using Javascript
Asked Answered
F

7

15

I am having some data which is facing issue while exporting to csv if the data contains "," with in it.

Hereis the code:

  CSVData.push('"' + item.OrgId+ '","' + item.Name + '","' + item.OrgDescriptionString + '","' + itam.OrgDetailsString + '","' + item.Active+ '"');

if orgDetaisl string contains "," with in it, the data is getting overlapped and moving to next column.

How to escape "," .I tried using double quotes """ but not sure how can i achieve.

CSVData.push('"' + item.OrgId+ '","' + item.Name + '","\""' + item.OrgDescriptionString + '"\"","\""' + item.OrgDetailsString + ' \"","' + item.Active+ '"');

Please correct the string and help me to escape "," with in the data.

Far answered 22/5, 2017 at 11:21 Comment(2)
When you create the CSV use a different column separatorSasaki
@Gerard! Then it is not a CSVGalipot
A
13

The proper way to escape separators inside fields is to enclose the fields in double quotes:

Id,Description
123,"Monitor, Samsung"

But then quotes inside the field have to be escaped with another quote:

Id,Description
123,"Monitor 24"", Samsung"

Of course, a different delimiter like ; could be used, but then you'd still have to check if those appear in the data.

Accelerometer answered 22/5, 2017 at 18:47 Comment(1)
What you suggested didn't work. Please provide fiddler for itWobbling
F
13

I tried to with the below code and it worked for me

item.OrgDescriptionString.replace(/\"/g, "\"\"");

I tried to escape single quotes in the string and it worked.

Far answered 23/5, 2017 at 8:56 Comment(0)
J
6

This is how I have achieved the reported functionality.

In brief: Requirement was to fetch the comma-separated string as it is in the CSV Export.

Assuming that we have a comma-separated string which needs to be exported in the CSV as it is.

For example String value which comes from the data source as:

'StackOverflow', 'Awesome! work by StackOverflow team, cheers to them.'

Solution:

['stackoverflow', 'Awesome! work by stackoverflow team, cheers to them.'].map(string => string === null ? '' : `\"${string}\"`);

Explanation: So if we quote the comma-separated values and try exporting than the CSV will not consider the comma(,) as a field separator and due to which in CSV export it won't split into multiple columns with respect to numbers of available commas. And the content will appear in the same column.

Ref wiki link: Comma-separated_values

Hope this helps. Cheers!!

Jamila answered 29/8, 2019 at 2:48 Comment(1)
if you have a value with quotes already this solution wont work. It works fine for other scenarios.Philia
D
2

Use double-quotes enclosed string and escape double-quotes in string:

csvData.push( "\\"" + strData.replace( /\\"/g , "\\"\\"" ) + "\\"" )
Demagogy answered 27/6, 2021 at 11:49 Comment(0)
J
1

One of the probable solution could be to read the file and then replace the read data with ""

Something like this:

readData = readData.replace(/,/g, "");
Jasminjasmina answered 22/5, 2017 at 11:28 Comment(0)
I
1

I had a similar situation, here is the string that needs to be printed in the csv file:

'"P1", "Graded Reader": "The Three", Bears 15 Jun 2023'

then i solved it by use Template literals (Template strings):

const exampleString = '"P1", "Graded Reader": "The Three", Bears';
const time = '15 Jun 2023';

const finalString = `"${exampleString?.replace(/"/g, '""')}" ${time}`;
Insociable answered 8/8, 2023 at 17:55 Comment(0)
T
0

It is very simple, suppose an address variable containing the commas,, then you should enclosed it in double quotes.

data.push('"'+address+'"');

or

in my case it is working fine!

Tanganyika answered 1/12, 2023 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.