1

I've been working to automatically pull data from an automated Gmail message. There are multiple daily emails that come through with the same label, so ideally I would like to loop through each email, and extract some of the data. I've set it up to use a few regex to grab the data, and it works for the first email. However, it won't loop correctly to find the next email with the label. Here is the code I have so far:

function parseEmailMessages (start) {
var label = GmailApp.getUserLabelByName("Bounce");
var threads = label.getThreads();
var sheet = SpreadsheetApp.getActiveSheet();
var tmp = [];
var messages = GmailApp.getMessagesForThreads(threads);
var bodies = [];

for (var i =0; i < threads.length; i++) {
  var bodies = [];
   for(k in threads[i].getMessages()) {
   bodies.push(threads[i].getMessages()[i].getPlainBody());


    var content = bodies.toString();
    if (content) {
        tmp = content.match(/[\n\r].*First Name\s*:\s*([^\n\r]*)/);
        var firstname = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';

        tmp = content.match(/[\n\r].*Last Name\s*:\s*([^\n\r]*)/);
        var lastname = (tmp && tmp[1]) ? tmp[1].trim() : 'No Lastname';

        tmp = content.match(/[\n\r].*Customer ID\s*:\s*([^\n\r]*)/);
        var customerID = (tmp) ? tmp[1].trim() : 'No CustomerID';

        tmp = content.match(/[\n\r].*Invoice\s*:\s*([^\n\r]*)/);
        var invoice = (tmp) ? tmp[1].trim() : 'No Invoice';

        sheet.appendRow([firstname, lastname, customerID, invoice]);
        Logger.log([firstname,lastname, customerID, invoice]);
      } 

       }


}

};

It loops through correctly the first time, and then gives me an error: TypeError: Cannot call method "getPlainBody" of undefined.

Any help would be greatly appreciated!

2 Answers 2

1

You are seeing that error because you should use k variable in the for loop to get each message of that label. Check this line below:

threads[i].getMessages()[k].getPlainBody()

Tried changing this line in the for loop and its working for me.

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the note, it did get rid of the error. However, the loop is not finding the second email with that label. In my inbox, I have two emails with the label "Bounce". However, my spreadsheet is populating two rows of the same data. Any thoughts?
0

After simplifying my code, I was able to get the script to loop correctly through my emails. This is the code that worked for me:

function processInboxToSheet() {

// Have to get data separate to avoid google app script limit!

//var start = 0;
var label = GmailApp.getUserLabelByName("Bounce");
var threads = label.getThreads();
var sheet = SpreadsheetApp.getActiveSheet();
var result = [];
var newLabel = GmailApp.getUserLabelByName("Done");
var oldLabel = GmailApp.getUserLabelByName("Bounce");


for (var i = 0; i < threads.length; i++) {
  var messages = threads[i].getMessages();

  var content = messages[0].getPlainBody();

  // implement your own parsing rule inside
 if (content) {
    var tmp;
    tmp = content.match(/[\n\r].*First Name\s*:\s*([^\n\r]*)/);
        var firstname = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';

        tmp = content.match(/[\n\r].*Last Name\s*:\s*([^\n\r]*)/);
        var lastname = (tmp && tmp[1]) ? tmp[1].trim() : 'No Lastname';

        tmp = content.match(/[\n\r].*Customer ID\s*:\s*([^\n\r]*)/);
        var customerID = (tmp) ? tmp[1].trim() : 'No CustomerID';

        tmp = content.match(/[\n\r].*Invoice\s*:\s*([^\n\r]*)/);
        var invoice = (tmp) ? tmp[1].trim() : 'No Invoice';

        sheet.appendRow([firstname, lastname, customerID, invoice]);
      } 

    Utilities.sleep(500);
    threads[i].addLabel(newLabel).removeLabel(oldLabel).refresh();
  }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.