Search code examples
javascriptregexgoogle-sheetsgmailmessage-passing

how to run .js file with gmail


function parseEmailMessages(start) {

  start = start || 0;

  var threads = GmailApp.getInboxThreads(start, 100);
  var sheet = SpreadsheetApp.getActiveSheet();

  for (var i = 0; i < threads.length; i++) {

    // Get the first email message of a threads
    var tmp,
      message = threads[i].getMessages()[0],
      subject = message.getSubject(),
      content = message.getPlainBody();


    if (content) {

      tmp = content.match(/Firt Name:\s*([A-Za-z0-9\s]+)/);
      var fname = (tmp && tmp[1]) ? tmp[1].trim() : 'No fname';

      tmp = content.match(/Last Name:\s*([A-Za-z0-9\s]+)/);
      var lname = (tmp && tmp[1]) ? tmp[1].trim() : 'No lname';

      tmp = content.match(/Address:\s*([A-Za-z0-9\s]+)/);
      var address = (tmp && tmp[1]) ? tmp[1].trim() : 'No address';

      tmp = content.match(/City:\s*(^[a-zA-Z]+)/);
      var city = (tmp && tmp[1]) ? tmp[1].trim() : 'No city';

      tmp = content.match(/State:\s*(^[a-zA-Z]+)*$/);
      var state = (tmp && tmp[1]) ? tmp[1].trim() : 'No state';

       tmp = content.match(/Zip:\s*([0-9]+)/);
      var zip = (tmp && tmp[1]) ? tmp[1].trim() : 'No zip';}}}

I am parsing email data and saving it to spread sheet.I have created .js file that contains above code for parsing gmail data. I don't know how to test it. How would i add my js file to gmail. Or there is another way of doing this thing ? Thanks for your help.


Solution

  • In google Drive you can create a "Google Apps Scripts" then you can run it manually or triggered by time intervals.