javascript - Extract CSV from ZIP file in gmail thread and write data to a google sheets -


i'm working on script following. looks specific label in gmail, "test". find first message , attachment (which zip, containing single csv) extract csv , write google spreadsheet.

i've managed csv attachment, not zip csv inside.

here's i'm @ far. i'm sure it's simple i'm missing, seems reading zip , writing weird chars rather reading csv inside.

    function getcsv() {  var mylabel = gmailapp.getuserlabelbyname("test");  var threads = mylabel.getthreads(0,1);  var msgs = gmailapp.getmessagesforthreads(threads);  var attachments = msgs[0][0].getattachments();   logger.log(attachments);  var csv =  attachments[0];  logger.log(csv);   var extracted = utilities.unzip(csv);   logger.log("unzipped data "+ extracted);  var tostring = extracted.getblob.getdataasstring();   logger.log(tostring);   var data = utilities.parsecsv(tostring);  logger.log(data);   //var ss = spreadsheetapp.getactivespreadsheet(); //var sheet = ss.getsheetbyname("ticketdump"); //sheet.clearcontents(); //var range = sheet.getrange(1,1, data.length,data[0].length); //range.setvalues(data); //   } 

in case sheds light on problem, here script made import csv attachments google sheets, works.

function getcsv() {     var mylabel = gmailapp.getuserlabelbyname("test");  var threads = mylabel.getthreads(0,1);  var msgs = gmailapp.getmessagesforthreads(threads);  var attachments = msgs[0][0].getattachments();   var csv =  attachments[0].getdataasstring();  var data = utilities.parsecsv(csv);    var = data.length ; var b = data[0].length;    var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheetbyname("ticketdump"); var super = sheet.getrange("e1").getformula(); var ticketlink = sheet.getrange("f2:f").getformulas(); sheet.clearcontents(); var range = sheet.getrange(sheet.getlastrow()+1,5, a, b); var range = sheet.getrange(1,1, a, b); range.setvalues(data); sheet.getrange("e1").setformula(super); sheet.getrange("f1").setvalue("header name"); sheet.getrange("f2:f").setformulas(ticketlink);   } 

found our going wrong.

it tostring part of code. didn't tell entry

this part;

var tostring = extracted.getblob.getdataasstring(); 

should like

var tostring = extracted[0].getdataasstring(); 

final code looks

    function getcsv() {  var mylabel = gmailapp.getuserlabelbyname("test");  var threads = mylabel.getthreads(0,1);  var msgs = gmailapp.getmessagesforthreads(threads);  var attachments = msgs[0][0].getattachments();    var csv =  attachments[0]; var extracted = utilities.unzip(csv); var string = extracted[0].getdataasstring(); var data = utilities.parsecsv(string);     var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheetbyname("ticketdump"); sheet.clearcontents(); var range = sheet.getrange(1,1, data.length,data[0].length); range.setvalues(data);   } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -