Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

439
Views
Google Script: How do I get all messages from multiple threads into an array?

My goal is to get all messages with a particular label into an array.

Currently I'm using Gmailapp.search(), which successfully gives me an array of Gmail threads with the label I've specified. Where I'm stuck is - extracting all the messages of the threads into an array. As you can see in the code below, I'm trying to use the getMessagesForThreads() function, but with the code I have below, the number of threads and number of total messages is the same, which can't be right.

function getRelevantMessages() 
{

var queried_Email_Threads = GmailApp.search ("label:expense tracking/Credit Card");
console.log('Number of threads queried:'+ queried_Email_Threads.length)

var messages = GmailApp.getMessagesForThreads(queried_Email_Threads)
console.log('Total number of messages: ' + messages.length)
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When I saw the official document of the method of getMessagesForThreads, the sample script is as follows.

// Log the subject lines of all messages in the first two threads of your inbox
var thread = GmailApp.getInboxThreads(0, 2);
var messages = GmailApp.getMessagesForThreads(thread);
for (var i = 0 ; i < messages.length; i++) {
  for (var j = 0; j < messages[i].length; j++) {
    Logger.log("subject: " + messages[i][j].getSubject());
  }
}

When this sample script is reflected to your script, it becomes as follows.

Modified script:

function getRelevantMessages() {
  var queried_Email_Threads = GmailApp.search("label:expense tracking/Credit Card");
  console.log('Number of threads queried:' + queried_Email_Threads.length)

  var messages = GmailApp.getMessagesForThreads(queried_Email_Threads)
  console.log('Total number of messages: ' + messages.length)

  // I added below script.
  var res = [];
  for (var i = 0; i < messages.length; i++) {
    for (var j = 0; j < messages[i].length; j++) {
      res.push(messages[i][j]);
    }
  }
  console.log(res.length)
}
  • From the official document,

    GmailMessage[][] (from getMessagesForThreads) - an array of arrays of messages, where each item in the outer array corresponds to a thread and the inner array contains the messages in that thread

References:

  • getMessagesForThreads(threads)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!