title: Google Apps Scripts - Extract data from gmail into a spreadsheet
body: <p>this is the first script i try to write from scratch. It's been no good up to now so i'm going to ask for some help.</p>
  
  <p>Case: I recieve e-commerce confirmation emails from e-commerce sites no reply email address. In the email's body they send email address from buyers. I want to send an automated mail to the body's email address.</p>
  
  <p>How i plan to do this (any suggetions to eliminate steps will be thanked).</p>
  
  <ol>
  <li><p>Use a rule to tag incoming emails with a unique tag.</p></li>
  <li><p>Use that tag to identify emails in gmail with a script, go one by one and extract the info i need. Use regex with the emails body content to extract the email address i need to send the automated emails. Plan is to get: subject, date, email from body.</p></li>
  <li><p>Write all that info to a spreadsheet.</p></li>
  <li><p>Get rid of unique tag info to prevent duplicate runs.</p></li>
  <li><p>Then use form mule addon to send emails from the spreadsheet.</p></li>
  </ol>
  
  <p>So far, i've dealt with steps 1 (easy), and been stuggling with steps 2 and 3 (im not a coder, i can read, undestrand and hack. writing from scratch is a completely different thing). Ive dealt with 4 before i think this is the best way to deal with it. </p>
  
  <p>With the script i extract info to the spreadsheet, with the addon i use the info from the spreadsheet to send emails.</p>
  
  <p>This is the code ive written so far. I've left the regex part for later cause i cant even write anything into the spreadsheet yet. once i get that working, ill start working in the regex and "remove the label" aspects of the script.</p>
  
  <pre><code>function myFunction() {
    function getemails() {
      var label = GmailApp.getUserLabelByName("Main tag/subtag");
      var threads = label.getThreads();
      for (var i = 0; i &lt; threads.length; i++) { 
      var messages=threads[i].getMessages();  
        for (var j = 0; j &lt; messages.length; j++) {
      var message=messages[j];
      var subject=message.getSubject();
      tosp(message);
        }
       }
    }
  
    function tosp(message){
      var body=message.getBody()
      var date=message.getDate();
      var subject=message.getSubject(); 
      var id= "my spreasheet id";
      var ss = SpreadsheetApp.openById(id);
      var sheet = ss.getActiveSheet();
      sheet.appendRow(subject,date,body);    
  
  }
  } 
  </code></pre>
  
  <p>Any help would be appreciated. </p>
  
  <p>Thanks
  Sebastian</p>