Extract information from email body using google scripts

I am trying to extract certain information from an email on one of my Gmail shortcuts. I hacked (my knowledge of scripting is very limited) the following together based on a script from https://gist.github.com/Ferrari/9678772 . I am getting the error: "Unable to convert array to Gmail Thread - Line 5"

Any help would be greatly appreciated.

/* 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 < 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 && tmp[1]) ? tmp[1].trim() : 'No username';

      tmp = content.match(/Phone Number:\n([\s\S]+)/);
      var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';

      tmp = content.match(/Email Address:\n([A-Za-z0-9@.]+)/);
      var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';

      tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
      var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';



      sheet.appendRow([username, phone, email, comment]);

    }
  }
};

      

+3


source to share


4 answers


Thanks guys .. This did the trick:



// Adapted from https://gist.github.com/Ferrari/9678772
function processInboxToSheet() {

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

  var start = 0;
  var label = GmailApp.getUserLabelByName("yourLabelName");
  var threads = label.getThreads();

  var sheet = SpreadsheetApp.getActiveSheet();
  var result = [];



  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(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
      var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';

      tmp = content.match(/Phone Number:\n([\s\S]+)/);
      var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';

      tmp = content.match(/Email Address:\n([A-Za-z0-9@.]+)/);
      var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';

      tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
      var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';



      sheet.appendRow([username, phone, email, comment]);

      Utilities.sleep(500);
    }
  }
};

      

+1


source


var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));

      

must include an array index as GmailApp.search returns an array even if only one element is found.

var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname")[0]);

      



will work but will be verbose.

var thread_list = GmailApp.search("label:labelname");
var threads = GmailApp.getMessagesForThread(thread_list[0]);

      

IMO, the above is clearer in meaning.

0


source


Hi please, this code doesn't work for me, all I want to receive by email is phon name.

-1


source


Hello, can you please help me? this code doesn't seem to work for me, all I want to extract from the label in my image description here is email is name, email and phone number

information can be seen in the link below

-1


source







All Articles