gpt4 book ai didi

javascript - IF 进入 Google 脚本中关于提取电子邮件的循环

转载 作者:行者123 更新时间:2023-12-02 23:05:23 28 4
gpt4 key购买 nike

关于将 G 邮件提取到 Google 电子表格,我该如何添加 IF 以按主题驳回特定电子邮件?例如:电子邮件回复(主题中带有“RE:”)。我不希望这些电子邮件出现在我的电子表格中。

我尝试过使用 LOOP,但它不起作用,因为 LOOP 不断将所有电子邮件发送到我的电子表格,而没有事先进行选择。

代码如下:

function GetEmails() {
var label = GmailApp.getUserLabelByName("MY LABEL");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var from = message.getFrom()
var to = message.getTo();
var subject = message.getSubject();
var RE = subject.startsWith("RE:")
if ([RE != "RE:") {
process1(message);
} else {
process2(message)
}
}
}

function process1(message) {
var body = message.getPlainBody();
var subject = message.getSubject();
var date = message.getDate();
var from = message.getFrom();
var url = "GOOGLE SPREADSHEET"
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getActiveSheet();
sheet.appendRow([date, from, subject, body]);
}

function process2(message) {
var body = message.getPlainBody();
var subject = message.getSubject();
var date = message.getDate();
var from = message.getFrom();
var url = "GOOGLE SPREADSHEET"
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getActiveSheet();
sheet.appendRow([date, from, subject, body]);
}
}

最佳答案

RE 是脚本中的 bool 值。

也要干燥(不要重复自己)

你可能打算这样做

const url = "GOOGLE SPREADSHEET";
const ss = SpreadsheetApp.openByUrl(url);
const sheet = ss.getActiveSheet();

function process(message) {
var subject = message.getSubject();
if (subject.toUpperCase().startsWith("RE:")) {
return; // ignore RE
}
var body = message.getPlainBody();
var date = message.getDate();
var from = message.getFrom();
sheet.appendRow([date, from, subject, body]);
}

function GetEmails() {
var label = GmailApp.getUserLabelByName("MY LABEL");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
process(messages[j]);
}
}
}

关于javascript - IF 进入 Google 脚本中关于提取电子邮件的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609115/

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