Running into a situation where when I create a CSV...
# suitescript
s
Running into a situation where when I create a CSV using SS2.0, because there are commas in the names of one of my fields, it messes up the CSV file output. Any suggestion on how folks have overcome it?
s
use papaparse
s
format.parse?
s
s
Pardon my slowness here, but I’m use N/query to convert a dataset to a CSV file so I can email it.
b
papaparse is a csv library
it has built in support for escaping values in csv files
if you want to do the escaping yourself, the normal method is to quote your csv values
s
Got it.
b
and escape any existing quotes in the csv value with a leading quote
s
Copy code
//Creating a string variable that will be used as CSV Content
    var contents='';
      
    for(var z =0; z<content.length;z++){
        contents +=content[z].toString() + '\n';
    }
    log.debug('contents',contents);
    var fileObj = file.create({
        name: 'testsearchcsv.csv',
        fileType: file.Type.CSV,
        contents: contents,
        description: 'This is description',
        folder: '161852'
     });
    fileObj.save();
that’s what I’m doing, and its causing things like Shubs, LLC. to split into multiple. But I’ll try to do the quotes.
s
sigh
s
i know, I’m still learning @stalbert.
s
I feel for ya, that's my sigh. Using a well tested, battle hardened library would be a good idea, perhaps even more so for someone still learning (then again, we're all still learning, I hope!)
the ironic thing is I often find less experienced developers are more likely to want to write everything themselves.
s
Committed to using the library. So for library use, I just download the .js file and put it into a folder and reference it like a module. Missing anything on that front? Second, you would parse the contents variable in the for loop, correct?
b
your custom module use sounds correct
👍 1
otherwise, get started on the documentation
its faq based
s
yes, i was just reading through it.
Appreciate the help!
b
very reasonable, sometimes funny
😂 1
s
agreed, then again, even the library name is a bit tongue-in-cheek?
m
PapaParse is great, but if you want a naive approach that works without a library, this works pretty well
Copy code
var row = ['ABC', 'DEF,HIJ', 'A field with "double quotes"'];

var csvLine = row.map(function(field) {
  return '"' + field.toString().replace(/"/g, '""') + '"';
}).join();
The theory is just to escape every double quote with two double quotes, and then wrap every field in double quotes.
👍 1