0

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
  • 1
    The first most obvious suspect is a typo in the label name. – Yuri Khristich Feb 18 '22 at 14:07
  • 1
    Make sure your query string is correct https://support.google.com/mail/answer/7190?hl=en – TheWizEd Feb 18 '22 at 14:24
  • Thanks for your comments. I also thought the problem lied in a type or the string query. However, I've made sure the query string is correct. It is almost identical to the solution that is working (same set-up just another word): Working query: label:roos-r.-data---revenue Not working query: label:roos-r.-data---product When changing the query of my solution to the first query, it works, but the second doesn't. Eventhough I see both in my Gmail with those exact labels... – roosbot Feb 21 '22 at 15:38

2 Answers2

1

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)
  }  
}
Mike Steelson
  • 14,650
  • 2
  • 5
  • 20
  • Or just `"label:yourLabelHere"` without `{}` – Yuri Khristich Feb 18 '22 at 14:50
  • 1
    yes Yuri, I use brackets to get messages from multiple lables as `var requete ="is:unread {label:Test1 label:Test2}"` – Mike Steelson Feb 18 '22 at 14:52
  • Thanks for your help! I've tried your solution, but I get a different error, namely: TypeError: Cannot read property 'getAttachments' of undefined This error is coming from the next line of code, that is not included in your solution: var attachment = messages.getAttachments()[0] Any ideas how to get the attachment from the message? – roosbot Feb 21 '22 at 15:39
  • try `var attachment = messages[0].getAttachments()[0]` – Mike Steelson Feb 21 '22 at 16:00
0

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