Search code examples
javascriptgoogle-apps-scriptgmailgoogle-inbox

getInboxThreads() for specific inbox folder. Google Apps Script gmail


I am trying to retrieve email threads from only a specific folder which I named 'Approval_needed'. I have found a way to get all of my inbox threads like so from the Google Apps Script Reference page:

var threads = GmailApp.getInboxThreads();
 for (var i = 0; i < threads.length; i++) {
   Logger.log(threads[i].getFirstMessageSubject());
 }

Is it possible to do something like getInboxThreads for folder 'Approval_needed'?
I have searched around and have not found an answer for this. I have found other methods such as getPriorityInboxThreads() and getStarredInboxThreads(), but nothing like getInboxThreads(string).


Solution

  • What you are calling "folder" is actually a "label" in gmail, you can use the getThreads() method on the Label class.

    Example from the documentation :

    // Log the subject lines of the threads labeled with MyLabel
     var label = GmailApp.getUserLabelByName("MyLabel");
     var threads = label.getThreads();
     for (var i = 0; i < threads.length; i++) {
       Logger.log(threads[i].getFirstMessageSubject());
     }