Aaron
Aaron

Reputation: 3325

Exact each gmailapp threads subject line one at a time in google apps script

I want to set the variable "subject" to be the subject line of each email that matches my GmailApp.search criteria. Right now when it searches, it stores every subject line into the "subject" variable and I am unsure how to make it so I can perform subject[0] for the first subject, subject[1] for the second subject, etc

var threads = GmailApp.search("label:bills")

      for (var i = 0; i < threads.length; i++) {

        var messages = threads[i].getMessages();

        for (var m = 0; m < messages.length; m++) {

          var subject = messages[m].getSubject()

        }

      }

If I have 2 emails that match the "bills" label, it will store both subject lines under "var = subject".

Upvotes: 0

Views: 1353

Answers (1)

JWoodall
JWoodall

Reputation: 73

Instead of assigning the subject to a variable in you for loop, you need to push them into an array then you can access them like you wanted to with subject[1].

var subject = new Array();
var threads = GmailApp.search("label:bills")

  for (var i = 0; i < threads.length; i++) {

    var messages = threads[i].getMessages();

    for (var m = 0; m < messages.length; m++) {

      subject.push(messages[m].getSubject());

    }

  }

Upvotes: 2

Related Questions