title: Extract last email with gscript
body: <p>I have the following code with which I have to extract the last email arrived with the label 'AlertGol', then I have to process it by cutting the parts that I do not need and send it back to me by email. Something I'm doing wrong because I get more than 3 emails including one undefinied.</p>
  
  <pre><code> function getEmails() {
    var label = GmailApp.getUserLabelByName("AlertGol");
    var thread = label.getThreads();
    for (var i = 0; i &lt; thread.length; i++) {
    var messages = thread[i].getMessages();
    for (var j=0; j&lt;messages.length; j++)
      {
        var msg = messages[j].getPlainBody();
        var trim = msg.substring(5,15);
        var trim1 = msg.substring(40, 90);
        var tot = trim+trim1;
      }
    GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), 'Subject', tot);
  }
  </code></pre>