1

I want my Google script to parse all emails within a label. But just once. The emails come from my bank. They store transaction details. Because I use thread conversation view I worry that the script will either - process one message more than once or miss a message in a case I mark the thread with a label let's say "done" once the email message was processed.

The transaction details are coming with the same subject, so all of them will be part of one thread. I do not want to store message ID or something like outside Google Script.

Could you think of any solution?

I was thinking that I can add email ID to the thread but the I would end up with too many labels.

I might be able to use Gmail API as suggested here

but I do not know how to check if a particular message got specific label.

Radek
  • 13,813
  • 52
  • 161
  • 255
  • Do you use star for anything else? – Cooper Oct 19 '20 at 15:34
  • @Cooper, I do use stars for quick access of very important emails but I thought of using starts. Once the thread is fully processed I might remove the stars. – Radek Oct 19 '20 at 18:42
  • Can I ask you about your goal? About `I do not know how to check if a particular message got specific label.`, in this case, you want to retrieve the label from a message? – Tanaike Oct 19 '20 at 22:22
  • my goal is to process all emails but to make sure that every single one is processed only once. When thinking how to accomplish I thought I might use labels. It seems to me that I can add a label to a message not the whole thread. Now I need to know how I can easily check of a message got particular label. Right now I need to know how to check if a message got label. – Radek Oct 20 '20 at 08:19

1 Answers1

2

You can query for messages containing a certain label with Gmail.Users.Messages.list specifying the id of a label

Sample:

  var starredMessages = Gmail.Users.Messages.list("me", {"labelIds":"Label_981438383934575828"}).messages;
  var starredIds = [];
  starredMessages.forEach(function(message){starredIds.push(message.id)});
  • Once you have this, you can retrieve all inbox messages, push their ids into an array and filter them to remove the already labelled messages from the array:
  var AllIds = [];
  var allMessages = GmailApp.getInboxThreads().forEach(function(thread){thread.getMessages().forEach(function(message){AllIds.push(message.getId())})}); 
  var filteredIds= AllIds.filter(function (id) { return starredIds.indexOf(id) == -1;});
  • As a result, you obtain an array with message Ids that do not have the specified label (yet).

  • Now, do with those messages what you desire, e.g. in a loop and subsequently add the label to them with e.g.

filteredIds.forEach(function(id){Gmail.Users.Messages.modify({'addLabelIds': [LabelId]}, 'me', id)});

UPDATE

Another way to retrieve messages without the label is with the query parameter q.

Thereby, the query should be specified as -label:MY LABEL :

var NotStarredMessages = Gmail.Users.Messages.list("me", {"q":"-label:MY LABEL"}).messages;

HOWEVER, KEEP IN MIND THAT IF ONE MESSAGE OF A THREAD HAS THE LABEL, THIS SECOND METHOD WILL NOT EXCLUDE ALL MESSAGES OF THE CORRESPONDING THREAD FROM THE LIST.

So in your case the second method is not recommended.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • this looks like what I need. So this part `var filteredIds= AllIds.filter(function (id) { return starredIds.indexOf(id) == -1;});` removes labelled messages (starred ones) from the array? – Radek Oct 20 '20 at 08:49
  • Yes, that's exactly what it does! – ziganotschka Oct 20 '20 at 09:22
  • I was thinking and that is why I created this question if there is a easier way how to find whether a particular post got a label. If not then I got a solution :-) – Radek Oct 20 '20 at 09:53
  • It might be faster to do it with a query, I will update my answer to show how. – ziganotschka Oct 20 '20 at 10:18
  • so the update .. using q will return a thread not messages? – Radek Oct 20 '20 at 10:43
  • 1
    It will return messages (and messageIds), but the query condition seems to apply for the whole thread, so in your case, I would say it does not return you reliably all the correct messages and thus you need to use the first option (even it involves more steps). – ziganotschka Oct 20 '20 at 10:59