- I am new to
Apps Scriptand I am trying to implement where I am taking an input throughGoogle Sheetsby usingApps Script. The user gets prompted with a HTML form through which the user uploads aPDFfile. - The file is further saved in a google drive and further used for processing the
PDF. - One way of importing
csvfile intoGoogle Sheetsis uploading the output to theGoogle Driveand then importing theCSVto a new sheets. I am able to achieve this. - My question is can I cut the
middle-wareofGoogle Driveand directly givebase-64string as an input maybe by usingcallbackurl to the Google Sheets and the output is generated?
1 Answer
Something like this?
function myFunction() {
var csv_string = 'aa,bb,cc\n11,22,33'; // original csv string
var base64data = Utilities.base64Encode(csv_string); // encoded string
console.log(base64data);
var decoded = Utilities.base64Decode(base64data); // decoded string
console.log(decoded);
var string = Utilities.newBlob(decoded).getDataAsString(); // csv string
console.log({string});
var array = Utilities.parseCsv(string) // 2d array
console.log(array)
}
Reference: https://developers.google.com/apps-script/reference/utilities/utilities?hl=en#parseCsv(String)
Update
It works fine with your CSV:
function myFunction() {
var csv_string = `Transaction_Date,Particulars,Cr_Dr,Amount,Balance
01-APR-2017,B/F,,,"3,99,068.72"
03-Apr-2017,NEFT-MAHENDRA HARDWARE,Cr,"1,31,427.00","2,67,641.72"
05-Apr-2017,NEFT-INTER DECOR,Cr,"1,01,607.00","1,66,034.72"
07-Apr-2017,INSPECTION CHARGES,Dr,721.00,"1,66,755.72"
10-Apr-2017,BY CLG-- 5484,Cr,"90,115.00","76,640.72"
11-Apr-2017,INDORE MAIN 387 GOVT,Dr,299.00,"76,939.72"
11-Apr-2017,INDORE MAIN 387 GOVT,Dr,"42,278.00","1,19,217.72"
12-Apr-2017,BY CLG-259- 50754,Cr,"68,842.00","50,375.72"
12-Apr-2017,BY CLG-COB- 251639,Cr,"30,000.00","20,375.72"
12-Apr-2017,DILIP AJMERA S/O JA,Dr,"2,89,868.00","3,10,243.72"`;
var base64data = Utilities.base64Encode(csv_string); // encoded string
var decoded = Utilities.base64Decode(base64data); // decoded string
var string = Utilities.newBlob(decoded).getDataAsString(); // csv string
var array = Utilities.parseCsv(string) // 2d array
SpreadsheetApp.getActiveSheet().getRange(1,1,array.length,array[0].length).setValues(array);
}
Here is my sheet.
4 Comments
Lav Sharma
having issue while parsing csv, facing the error Error Exception: Could not parse text. as my sample csv looks like this
Yuri Khristich
Looks like there is a problem with how you get the CVS. It doesn't look well at some places. It contains a number of uncommon symbols like
^, etc. But when I copy it from your Google Doc directly into the script the native Google csv parser get me no errors as you can see. All the 3000+ rows are parsed fine.Yuri Khristich
And one more thing. Your CSV is quite big. Over 3000 rows. It takes about 200 kilobytes as txt. Make sure that it doesn't exceed the limitation on the size of a string.
Lav Sharma
Thankyou for the detailed answer it worked, I will take the size into consideration.

UrlFetchAppdoes about this thing: developers.google.com/apps-script/reference/url-fetch/…apilink as far as I have understood. I am talking about making acallbackto theapps scriptcontaining just thebase-64string.Utilities.parseCsv(Utilities.newBlob(Utilities.base64decode(base64String)).getDataAsString());Error Exception: Could not parse text.. For the method there is just a little typo it isbase64Decodeand notbase64decode.