Google脚本正在重新处理电子邮件 [英] Google script is reprocessing emails

查看:39
本文介绍了Google脚本正在重新处理电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我每6个小时会收到一封电子邮件,其中每小时都有日志记录,我希望将每个小时的日志记录到同一电子表格中,这样我就可以长时间长时间快速地进行每小时一小时的扫描.我还会收到随机的警报"电子邮件,我想将其添加到该电子表格中.我所做的:当这些电子邮件进入时,我在我的工作gmail帐户中应用了两个过滤器之一.一种用于警报,另一种用于日志.过滤器将存档并将两个标签之一添加到电子邮件中.我有一个电子表格保存在团队驱动器中,该电子表格中嵌入了两个脚本.每分钟运行一个脚本,查找带有警报标签的电子邮件.另一个脚本每6小时运行一次,以查找带有日志标签的电子邮件.这两个脚本都会处理所有找到的电子邮件,进行处理,然后删除标签并添加已处理"标签.

Background: I get an email every 6 hours that has hourly logs in the body and I want to put each hourly log into the same spreadsheet so I can scan hour to hour quickly over long periods. I also get random "alarm" emails, which I want to add to that spreadsheet. What I've done: When those emails come in, I apply one of two filters in my work gmail account. One for the alarms, and one for the logs. The filters archive and adds one of two labels to the emails. I have a spreadsheet saved in my team drive, which has a couple of scripts embedded in it. One script runs every minute, looking for the emails with the alarm label. The other script runs every 6 hours, looking for the emails with the log label. Both scripts take any found emails, processes them, removes the label and adds a "processed" label.

问题:这两个脚本似乎都在2到4次之间随机处理电子邮件.当我手动运行它们时,它们永远不会这样做.

PROBLEM: Both scripts seem to be randomly processing the emails between 2 and 4 times. They never do this when I run them manually.

问题:为什么会这样?
(编辑):我认为它的行为方式是因为当收到一封新电子邮件时,它会与过滤器匹配,并将标签应用于整个线程,这将导致脚本处理整个线程,而不仅仅是最新的电子邮件.

QUESTION: Why is it behaving this way?
(EDIT): I think it's behaving this way because when a new email comes in, it matches the filter and it applies the label to the entire thread, which then causes the script to process the entire thread, instead of just the newest email.

新问题:如何在不必删除旧电子邮件的情况下停止此操作?

NEW QUESTION: How do I stop this without having to delete the old email messages?

我的问题不同于这个问题: Google Apps脚本Gmail获取消息而无需先前的对话在这个问题上,他们正在谈论当前消息的正文中的先前消息的正文.在我的,没有重复.每封电子邮件都是以前所有消息中的全新消息,不包括以前的消息的一部分.它们都具有相同的发件人,主题和标签.

My problem is different than this one: Google apps script Gmail get message without previous conversation In that question, they are talking about bodies of previous messages in the bodies of current messages. In mine, there is no repeat. Each email message is completely new from all previous messages, with no part of previous messages included. They just all have the same sender, subject, and therefor label.

这是我的代码:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName ="Current Month";
var s = ss.getSheetByName(sheetName);
var myLabel = "tower123"; 
var newLabel = "Processed"; 
var regExp = new RegExp(/(Snapshot taken[\S\s]*?Probe: \d+ \*F)/gmi); 
var keys = ["Snapshot taken at: ","Meters: Power Output","Status: Generator ",": ATS to ",": Tower Lights ","Chassis Temperature: ","Temperature: Rack Probe: "]; 

function myFunction() {
  var label = GmailApp.getUserLabelByName(myLabel);
  var label2 = GmailApp.getUserLabelByName(newLabel);
  var threads = label.getThreads();

  // get all the email threads matching myLabel
  for (var i = 0; i < threads.length; i++) {
    var messages = GmailApp.getMessagesForThread(threads[i]);

    // move thread from label to label2
    label2.addToThread(threads[i]);
    label.removeFromThread(threads[i]);

    // get each individual email from the threads
    for (var j = 0; j < messages.length; j++) {
      var bodyText = messages[j].getPlainBody();

      //"move thread" code was here before the noon 2/16 email, moved above for the noon email.  email threads were still processed when they shouldn't have been

      // split the email body into individual "paragraph" strings based on the regExp variable
      while (matches = regExp.exec( bodyText )){
        var logdata = matches[1];        
        for (k in keys){logdata = logdata.replace(keys[k],"");}

        // split out each "paragraph" string into an array
        var lines = logdata.split(/[\r\n]+/);
        for (l in lines){lines[l] = lines[l].trim();}
        for (l in lines){lines[l] = lines[l].replace(/^(\:\s)/,"");}

        // Turn the first element in the array into a date element, format it, and put it back
        lines[0] = Utilities.formatDate(new Date(lines[0]), "MST", "M/d/yy HH:mm");

        // Write the created array to a new row at the end of the 's' sheet
        Logger.log(lines);
        s.appendRow(lines);
      }
    }
  }
  var sheet = ss.getSheets()[0];
  var range = sheet.getRange("A2:Z");
  range.sort(1);
  // Set the date format of column A, from A2 onward
  // This forces the edge case of hour 00:00 to display
  // var column = s.getRange("A2:A");
  // column.setNumberFormat("M/d/yy HH:mm");
}

function alarmFunction(){
  var label = GmailApp.getUserLabelByName("toweralarms123");
  //Logger.log(label);
  var label2 = GmailApp.getUserLabelByName("Processed");
  if(label) {
    Logger.log("Yes label: "+label);
    var threads = label.getThreads();
    var a=[];

    for (var i = 0; i < threads.length; i++) {
      var messages = GmailApp.getMessagesForThread(threads[i]);
      for (var j = 0; j < messages.length; j++) {
        label2.addToThread(threads[i]);
        label.removeFromThread(threads[i]); 
        var date = [messages[j].getDate()]; // date/time
        a[j]=parseMail(messages[j].getPlainBody(),date);
      }
    }
  }
  else{Logger.log("No label: "+label);}
}

function parseMail(body,date) {
 if(body == "" || body == undefined){
  var body = document.getElementById("input").value
  }
  var a=[];
  var alarmKeys = "This is an email alarm";
  var keys=alarmKeys.split(",");
  var i,p,r;
  for (i in keys)  {
    p=alarmKeys+"[\r\n]*([^\r^\n]*)[\r\n]";
    r=new RegExp(p,"m");
    a[i]=body.match(p)[1];
  }
  date.push(a.toString());
  s.appendRow(date);
}

推荐答案

答案:由于使用对话视图"时将gmail标签应用于邮件的方式,因此没有真正简单的方法.我想出的解决方法是从新的电子邮件中创建数据数组(不幸的是,该数据还包括来自线程其余部分的数据,最多包括99条其他消息).然后,创建第二个数组,其中包含电子表格中的所有数据.这两个数组会相互比较,只有唯一的项目会保留并最终添加到电子表格中.

Answer: There is no really simple way to do this due to the way that gmail labels are applied to messages when using "Conversation View". The work-around I came up with involves creating an array of the data from the new email messages (which also unfortunately includes the data from the rest of the thread, upwards of 99 other messages). Then a second array is created containing all of the data from the spreadsheet. These two arrays are compared to each other and only the unique items are retained and ultimately added to the spreadsheet.

var ss = SpreadsheetApp.getActiveSpreadsheet();
// var s = ss.getActiveSheet();
var sheetName ="Current Month";
var s = ss.getSheetByName(sheetName);
var myLabel = "To Process"; // Name of current label being processed, this label is set via a filter in Gmail
var newLabel = "Processed"; // Name of "New" filter, this label needs to be created in Gmail first
var regExp = new RegExp(/(Snapshot taken[\S\s]*?Probe: \d+ \*F)/gmi); // regular expression used to find each "paragraph"
var keys = ["Snapshot taken at: ","Status: Generator ",": ATS to ",": Tower Lights ","Chassis Temperature: ","Temperature: Rack Probe: "]; // array keys to match on inside paragraphs

function myFunction() {
  var label = GmailApp.getUserLabelByName(myLabel);
  var label2 = GmailApp.getUserLabelByName(newLabel);
  var threads = label.getThreads();
  var data2 = [];
  var newData = [];

  // get all the email threads matching myLabel
  for (var i = 0; i < threads.length; i++) {
    var messages = GmailApp.getMessagesForThread(threads[i]);

    // move thread from label to label2
    label2.addToThread(threads[i]);
    label.removeFromThread(threads[i]);

    // get each individual email from the threads
    for (var j = 0; j < messages.length; j++) {
      var bodyText = messages[j].getPlainBody();

      // split the email body into individual "paragraph" strings based on the regExp variable
      while (matches = regExp.exec( bodyText )){
        var logdata = matches[1];        
        for (k in keys){logdata = logdata.replace(keys[k],"");}

        // split out each "paragraph" string into an array
        var lines = logdata.split(/[\r\n]+/);
        for (l in lines){lines[l] = lines[l].trim();}
        for (l in lines){lines[l] = lines[l].replace(/^(\:\s)/,"");}

        // Turn the first element in the array into a date element, format it, and put it back
        lines[0] = Utilities.formatDate(new Date(lines[0]), "MST", "M/d/yy HH:mm");

        // Put the array to a new item in the data2 array for further processing
        data2.push(lines);
      }
    }
  }
  // Compare the information in the data2 array to existing information in the sheet
  var data = s.getRange("A2:G").getValues(); //Change this to fit your data ranges
  for(i in data2){
    var row = data2[i];
    var duplicate = false;
    for(j in data){
      data[j][0] = Utilities.formatDate(new Date(data[j][0]), "MST", "M/d/yy HH:mm");
      if(row.join() == data[j].join()){
       duplicate = true;
      }
    }
    if(!duplicate){
      newData.push(row);
    }
  }
  if (newData.length){  // runs the below code only if there is newData, this stops an error when newData is empty
    s.getRange(s.getLastRow()+1, 1, newData.length, newData[0].length).setValues(newData); //writes newData to end of sheet
    s.getRange("A2:Z").sort(1); //sorts the sheet
  }
}

下面包含的是将要处理的电子邮件的片段:

And included below is a snippet of the emails that would be processed:

This is an email update

Snapshot taken at: 7:00:00 2/6/2018
        Status: Generator OFF
              : ATS to SRP
              : Tower Lights ON
        Chassis Temperature: 73.77 *F
        Temperature: Rack Probe: 78 *F

Snapshot taken at: 8:00:00 2/6/2018
        Status: Generator OFF
              : ATS to SRP
              : Tower Lights OFF
        Chassis Temperature: 71.32 *F
        Temperature: Rack Probe: 78 *F

这篇关于Google脚本正在重新处理电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆