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")