Reputation: 307
I am trying to extract data from a google sheet and parse it into CSV format. I have most of this working, but it is breaking in certain sections because of line breaks within the cells. There is a notes column within my sheet that usually has a paragraph or two of information within an individual cell, for example:
"This is a cell with information about this row.
This is a line break. Blah Blah Blah"
When I use the below code to parse it, the line break within the cell causes it to return onto a separate line. Does anyone have an idea of how to get around this? Does this column itself need to be programatically cleaned first before converting the whole dataset to csv? If so, does anyone have a suggestion on how to achieve this?
function convertRangeToCsvFile(sheet) {
var activeRange=sheet.getDataRange();
var data=activeRange.getValues();
if (data.length>1) {
var csv="";
for(var row=0;row<data.length;row++) {
for (var col=0;col<data[row].length; col++) {
if (data[row][col].toString().indexOf(",") != -1) {
data[row][col]="\"" + data[row][col] + "\"";
}
}
if (row<data.length-1){
csv += data[row].join(",") + "\r\n";
}else {
csv += data[row];}
}
var testfiles = DriveApp.getFilesByName('testfile.csv');
while (testfiles.hasNext()) {
var testfile = testfiles.next();
}
testfile.setContent(csv);
}
return testfile.getId();
}
Upvotes: 0
Views: 508