gpt4 book ai didi

google-apps-script - Google 脚本正在重新处理电子邮件

转载 作者:行者123 更新时间:2023-12-05 05:17:45 27 4
gpt4 key购买 nike

背景:我每 6 小时收到一封电子邮件,正文中包含每小时日志,我想将每个每小时日志放入同一个电子表格中,这样我就可以长时间快速扫描一小时又一小时。我还收到随机的“警报”电子邮件,我想将其添加到该电子表格中。我做了什么:当收到这些电子邮件时,我会在我的工作 gmail 帐户中应用两个过滤器之一。一个用于警报,一个用于日志。过滤器存档并向电子邮件添加两个标签之一。我在我的团队驱动器中保存了一个电子表格,其中嵌入了几个脚本。一个脚本每分钟运行一次,查找带有警报标签的电子邮件。另一个脚本每 6 小时运行一次,查找带有日志标签的电子邮件。这两个脚本都会获取任何找到的电子邮件,对其进行处理,删除标签并添加“已处理”标签。

问题:两个脚本似乎随机处理电子邮件 2 到 4 次。当我手动运行它们时,它们从不这样做。

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

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

我的问题与这个不同: Google apps script Gmail get message without previous conversation在那个问题中,他们谈论的是当前消息正文中先前消息的正文。在我的,没有重复。每封电子邮件都是全新的所有以前的邮件,不包括以前的邮件的任何部分。它们都具有相同的发件人、主题和标签。

这是我的代码:

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);
}

最佳答案

回答:由于在使用“对话 View ”时将 gmail 标签应用于邮件的方式,因此没有真正简单的方法可以做到这一点。我想出的解决方法涉及从新电子邮件消息创建一个数据数组(不幸的是,它还包括来自线程其余部分的数据,超过 99 条其他消息)。然后创建第二个数组,其中包含电子表格中的所有数据。这两个数组相互比较,只有唯一的项目被保留并最终添加到电子表格中。

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
}
}

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

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-apps-script - Google 脚本正在重新处理电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com