title: What needs to be added to this google script to mark the emails as read
body: <p>I'm looking to see how to mark the messages read after executing this script. I've read through <a href="https://developers.google.com/apps-script/reference/gmail/gmail-app#markMessageRead(GmailMessage)" rel="nofollow noreferrer">Class GmailApp</a>, but I am too much of a novice to know what to do. </p>
  
  <p>This script would be successful for me if it pulled the email data into google sheets based on the search criteria and then marked those messages as read. Currently, it will only pull the emails into google sheets.</p>
  
  <p>Thank you for your time. </p>
  
  <pre><code>var SEARCH_QUERY = "label:aggieworks is:unread to:me subject:*WO:*";
  
  // Credit: https://gist.github.com/oshliaer/70e04a67f1f5fd96a708
  
  function getEmails_(q) {
      var emails = [];
      var threads = GmailApp.search(q);
      for (var i in threads) {
          var msgs = threads[i].getMessages();
          for (var j in msgs) {
              emails.push([msgs[j].getBody().replace(/&lt;.*?&gt;/g, '\n')
                  .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')
              ]);
          }
      }
      return emails;
  }
  
  function appendData_(sheet, array2d) {
      sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
  }
  
  function saveEmails() {
      var array2d = getEmails_(SEARCH_QUERY);
      if (array2d) {
          appendData_(SpreadsheetApp.getActiveSheet(), array2d);
      }
  }
  </code></pre>