0

Solution: https://riptutorial.com/google-apps-script/example/20730/get-csv-file-attached-to-a-mail

Google Apps Script Novice trying to set up a script that will run on a time-bound trigger to find the most recent email (will be auto-labeled based on unique sender email) and then auto-transfer the single CSV file's single tab of data to a defined Google Sheet.

Here's a list of how-tos and walkthroughs I've tried:

https://ctrlq.org/code/20279-import-csv-into-google-spreadsheet Google Apps Script - GMail to Google Sheets - Get Oldest Unread Email in Thread's .csv Attachment Retrieve csv attachment file from gmail and place the data in a google spreadsheet How do I convert jpg or png to blob on google app script?

I've watched videos that run through the basics of using Google Apps Scripts on Youtube, signed up for and have completed the first 1/3 of the "Complete Java Certification Course" by Imtiaz Ahmad on Udemy, read through various documentation re Classes and Methods used in Google Apps Scripts and still cannot get this to work. I receive an "TypeError: cannot read property 'length' of undefined...", and it appears my problem stems from the attachment being "undefined".

function test(){

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data =  ss.getRange(1, 1,6,2).getValues();

  var threads = GmailApp.search("from:XXXXSENDERSEMAIL@GMAIL.COM");
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];
  var ss1 = SpreadsheetApp.getActive().getUrl();
  Logger.log(ss1);

  Logger.log(threads);
  Logger.log(message);  
  Logger.log(attachment);

  Logger.log(data);


  var newarray =  attachment[0];

  ss.getRange(1, 5, newarray.length, newarray[0].length).setValues(newarray);
}

Expected results: finds the csv attachment, gets data from the csv attachment, writes data on a "Master" spreadsheet that is updated daily (clearvalues/setvalues).

Error message: TypeError: Cannot read property 'getMessages' of undefined (line 30, file "Code")

  • Perhaps [this may help](https://riptutorial.com/google-apps-script/example/20730/get-csv-file-attached-to-a-mail) – Cooper Aug 20 '19 at 19:16
  • Possible duplicate of [Retrieve csv attachment file from gmail and place the data in a google spreadsheet](https://stackoverflow.com/questions/20688560/retrieve-csv-attachment-file-from-gmail-and-place-the-data-in-a-google-spreadshe) – Cooper Aug 20 '19 at 19:17
  • I cannot see a line 30 nor 'getMessages' in your code snippet, but as for "TypeError: cannot read property 'length' of undefined...": Think about it - `newarray.length` would retrieve the length of an array called `newarray`, but in your case, you assign to the variable `newarray` a single attachment (element with index 0 of the array `attachement[]`). Thus, newarray is not an array and consequently does not have any length - this is what throws you the error. Btw, attachment does not look like an array neither, so probably there is no element `attachment[0]`. – ziganotschka Aug 21 '19 at 07:35

0 Answers0