title: Extract info from email body with Google Scripts
body: <p>I am trying to extract specific info from email in one of my labels in Gmail. I've hacked (my scripting knowledge is very limited) the following together based on a script from <a href="https://gist.github.com/Ferrari/9678772" rel="nofollow">https://gist.github.com/Ferrari/9678772</a>. I am getting an error though: "Cannot convert Array to Gmail Thread - Line 5"</p>
  
  <p>Any help will be greatly appreciated.</p>
  
  <pre><code>/* Based on https://gist.github.com/Ferrari/9678772 */
  function parseEmailMessages(start) {
  
    /* var threads = GmailApp.getInboxThreads(start, 100); */
    var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));
    var sheet = SpreadsheetApp.getActiveSheet();
  
    var tmp, result = [];
  
    for (var i = 0; i &lt; threads.length; i++) {
  
     // Get the first email message of a threads
      var message = threads[i].getMessages()[0];
  
     // Get the plain text body of the email message
     // You may also use getRawContent() for parsing HTML
      var content = messages[0].getPlainBody();
  
  
     // Implement Parsing rules using regular expressions
      if (content) {
  
        tmp = content.match(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
        var username = (tmp &amp;&amp; tmp[1]) ? tmp[1].trim() : 'No username';
  
        tmp = content.match(/Phone Number:\n([\s\S]+)/);
        var phone = (tmp &amp;&amp; tmp[1]) ? tmp[1] : 'No phone';
  
        tmp = content.match(/Email Address:\n([A-Za-z0-9@.]+)/);
        var email = (tmp &amp;&amp; tmp[1]) ? tmp[1].trim() : 'No email';
  
        tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
        var comment = (tmp &amp;&amp; tmp[1]) ? tmp[1] : 'No office';
  
  
  
        sheet.appendRow([username, phone, email, comment]);
  
      }
    }
  };
  </code></pre>