背景: 我每隔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);
}
答案 0 :(得分:0)
答案:由于gmail标签在使用&#34;会话视图&#34;时应用于邮件的方式,因此没有非常简单的方法。我提出的解决方法涉及从新电子邮件消息中创建数据数组(不幸的是,还包括来自其余线程的数据,以及其他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