mrooney
mrooney

Reputation: 2022

How can I get the labels of multiple messages at once?

I'd like to, for a list of message or thread IDs, grab the labels for each of them without a separate API call for each.

The use-case for this is that I've grabbed all the messages in one label, and I need to do something with any of those which don't have a label in a list of "ignored" labels, without making an API call for each individual message. I guess alternatively I could make some kind of search like "in:label not:(in:x or in:y or in:z)" but I'm not sure how to do that with the REST API.

(Previously I was using the https://mail.google.com/ scope to get raw access to IMAP, but with that being phased out, I'm struggling to do the same things with the REST API)

Upvotes: 1

Views: 93

Answers (1)

Jescanellas
Jescanellas

Reputation: 2608

I don't think you can do that with the Gmail API, as the function to get the labels (GET) needs the id of each thread so you would need to call the API for each message.

However, you are right with the Gmail Search. You have to do it this way, using the GmailApp from Appscripts:

    function getNonlabeled() {
       var threads = GmailApp.search("has:nouserlabels"); //You can put any Gmail search parameter here
       
       for  (var i = 0; i < threads.length; i++){
       Logger.log(threads[i].getId());
       }
    
    }

Upvotes: 2

Related Questions