title: Google Apps Script Class GmailApp Batch Operations?
body: <p>I've been fooling around with GAS for a month or so now, and I've become fairly familiar with using batch operations to read/write to/from spreadsheets (e.g. getValues(), setValues()). However, I'm currently writing a script that pulls a sizable amount of data out of Gmail using class GmailApp, my code is running very slowly (and even timing out), and I can't seem to figure out how to use batch operations for what I'm trying to do. Here's my code thus far (with the email address and name changed):</p>
  
  <pre><code> function fetchEmails(){
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var threads = GmailApp.search('in: label:searchedLabel');
    var messages = new Array();
  
    function Email(message){
    this.date = new Date(message.getDate());
    this.body = message.getBody();
    }
  
    for(var i=0;i&lt;threads.length;i++){
      for(var j=0;j&lt;threads[i].getMessageCount();j++){
        if(threads[i].getMessages()[j].getFrom()=="firstName lastName &lt;email@domain.com&gt;"){
         var message = new Email(threads[i].getMessages()[j]);
         messages.push(message);
        }
      }
    }  
  }
  </code></pre>
  
  <p>As you can see, I'm querying my email for all threads with the given label,
  making an object constructor for a custom Email object (which will have the body and date of an email as properties). Then I'm looping through each thread and when a given email matches the sender I'm looking for, I create an instance of the Email object for that email and place that Email object into an array. The goal is that in the end I'll have an array of Email objects that are all from my desired sender. However as you've probably noticed the code calls Google's APIs way too often, but I can't seem to figure out batch operations for interfacing with Gmail. Any ideas? Thanks so much.</p>