Josee
Josee

Reputation: 180

Parse csv with line breaks in a single cell in google app script

I am trying to load some csv data onto spreadsheet. Unfortunately, I keep getting this error when I try to feed my csv data onto Utilities.parseCsv():

Exception: Could not parse text.

The payload for parseCsv looks something like this:

"\"colName1\", \"colName2\"\n, \"<some html>\n<more html>\", \"colVal2\"\n"

Initially, I thought it might be because there are some html stuff inside the csv data. However, after doing some more testing, I realized it's the \n that's screwing things up. That's because when I take out the \n<more html>\" part from that csv string, it's now able to parse my csv data. Is there a way to get around this without removing that portion of the payload?

My code is pretty simple but it looks like this:

 const response = UrlFetchApp.fetch(csvLink);
 const csvData = response.getContentText();
 const parsedCsvData = Utilities.parseCsv(csvData);

where csvLink is a url to the csv that allows users to grab the csv file/contents.

Essentially, I want to be able to parse my csv whilst being able to keep the line breaks present in a single cell.

Upvotes: 1

Views: 538

Answers (1)

Josee
Josee

Reputation: 180

Instead of using parseCsv(), I had to approach this problem in a different way since I couldn't just replace all \n with a different character. I ended up using batchUpdate() instead. This way, I was able to paste my csv data directly onto google spreadsheet in a more efficient manner.

Here is a pretty good github post that talks about different possible ways to write csv data onto google spreadsheet :

https://gist.github.com/tanaikech/030203c695b308606041587e6da269e7

Upvotes: 1

Related Questions