Home > Blockchain >  Cannot read property 'getMessages' - Google script to import csv attachment from Gmail to
Cannot read property 'getMessages' - Google script to import csv attachment from Gmail to

Time:02-24

I've been using this solution for quite some time to import data from Gmail to Google Sheets. Now I want to apply the same solution to a different email thread. I only changed the label filter from Gmail however, I receive the following error, whatever I do:

TypeError: Cannot read property 'getMessages' of undefined
importReport    @ import revenue product.gs:3

The solution I'm using is:

function importProduct() {
  var threads = GmailApp.search("ENTER LABEL HERE"); 
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];
  var sheet = SpreadsheetApp.openById("ENTER GOOGLE SHEETS ID HERE").getSheetByName("ENTER GOOGLE SHEETS NAME HERE");
  var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
   sheet.clearContents().clearFormats();
   sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

Does anybody have any idea how this is possible and how I can solve this issue? Thanks!

Execution log
2:33:28 PM  Notice  Execution started
2:33:29 PM  Error   
TypeError: Cannot read property 'getMessages' of undefined
importProduct   @ import revenue product.gs:3

CodePudding user response:

Try this

function importProduct() {
  var requete ="{label:yourLabelHere}"
  var threads = GmailApp.search(requete);
  for (var i = 0; i < threads.length; i  ) {
    var messages = threads[i].getMessages();
    Logger.log(messages)
  }  
}

CodePudding user response:

Thanks for all of your help! I finally noticed where it went wrong... I used a different email box which made the script unable to read the right email... Happy to finally found the error in this setup :)

  • Related