Need Help creating GMAIL Pub/Sub Notification service to SpreadsheetApp (Google Appscript)

大憨熊 提交于 2019-12-04 15:12:23

For anyone like me who needed to create a monitoring service in Google Appscript and needs something quick, I came up with a quick script to just check emails by a specific label. With the GmailApp, you can check messages by subject, too. Here is the code below:

var EMAILID = Session.getActiveUser().getEmail();
function getMessages() {
  var expLabel = GmailApp.getUserLabelByName('Episode Expiration');
  var threads = expLabel.getThreads();
  
  for(var t = 0; t < threads.length; t++)
  {
    var messages = threads[t].getMessages();
    for(var m = 0; m < messages.length; m++)
    {
      var email = {from: messages[m].getFrom().toString(),
                   body: messages[m].getPlainBody().toString(),
                   subject: messages[m].getSubject().toString()};
      
      Logger.log(email.subject.indexOf('Expiration'));
      if(/*(email.from.toString().includes('info@mydomain.com'))&&(*/email.subject.indexOf('Expiration') > 0)//)
      {
        var startInd = email.body.indexOf('Start')+12;
        var endInd = email.body.indexOf('End')+12;
        var pasInd = email.body.indexOf('Number')+8;
        var qualInd = email.subject.indexOf('Q');
        
        var qualco = email.subject.substring(qualInd, qualInd+17);
        var warning = email.body.substring(email.body.indexOf('Episode Expires in'),email.body.indexOf('days')+4);
        var startDate = email.body.substring(startInd, startInd+12);
        var endDate = email.body.substring(endInd , endInd+11);
        var priorAuthNumber = email.body.substring(pasInd , pasInd+13);
        
        var ss = SpreadsheetApp.openById('mysheetID').getSheets()[0];
        ss.appendRow([qualco, priorAuthNumber, startDate, endDate, warning]);  
      }
      messages[m].moveToTrash();
    }
  }
}

At this point, I don't actually need the PubSub to run anymore, but I'll leave this thread open if the developers want to document a response to the original problem. Thanks, guys!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!