I added specific messages (and not the whole thread) to a label to_process
with these steps:
Turn
Conversation Mode
off in Gmail settingsApply the label
to_process
to specific messagesWhen displaying the messages, I can confirm that only the specific messages have been added. For example, another message which is in the same thread doesn't have this label. This is good.
Now I'd like to loop on all these messages from Google Apps Script. But the problem is that the API can only give access to threads attached to a certain label:
var threads = GmailApp.search('label:to_process');
for (var i = 0; i < threads.length; i++) {
// problem: here I cannot access to messages but only threads
}
or
var label = GmailApp.getUserLabelByName("to_process");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
// problem: here I cannot access to messages but only threads
}
How to loop on messages (and not threads) associated to a label?
Beginning of a solution but I don't know how to continue:
var threads = GmailApp.search('label:to_process');
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
// pseudo code here because getMessageLabels doesn't exist
//if ("to_process" is in message.getMessageLabels()) {
//}
}
}
I'd go with Advanced Gmail Service and the Gmail API list(), then getMessageById() :