【发布时间】:2020-02-11 16:19:05
【问题描述】:
当我向客户发送报价时,我想编写一个脚本,为电子邮件添加标签,很简单。问题是,我只想将标签添加到线程一次,然后再也不添加。我有另一个脚本,它将获取带有该标签的所有电子邮件并添加日历提醒,并将删除标签,因此它不会继续添加事件。我遇到的问题是,当客户回复第一封电子邮件时,标签会被重新应用,这会导致另一个日历事件发生。有没有办法只为消息计数为 1 的线程添加标签?所以在下图的情况下,标签应该只适用于第二个线程,而不是第一个。
Two threads, one with a single email, and one with 5
function QuoteReminder() {
var reminderLabel = "STS Quotes", //Substitute your label here
calendarName = "Quote Follow-ups", ////Substitute your Calendar name here
label = GmailApp.getUserLabelByName(reminderLabel),
threads = label.getThreads();
if (threads.length > 0) {
for (i in threads) {
if (i.getMessageCount() == 1) {
//get calendar by name
var cals = CalendarApp.getCalendarsByName(calendarName);
//This is run the next day around 9am, calendar invite will be created for 13 days form then to account for the next day
var now = new Date().getTime();
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
for (i in threads) {
cals[0].createEvent(reminderLabel + '- '+ i.getFirstMessageSubject(),
new Date(now+(MILLIS_PER_DAY*13)),
new Date(now+(MILLIS_PER_DAY*13)+900000), {description: i.getPermalink()});
}
//Remove the label from the mails to avoid duplicate event creation on next run
label.removeFromThreads(threads);
}
}
}
}
【问题讨论】:
-
请包含您的代码
-
对不起!刚加了。上面的代码假定标签已经存在,并将日历提醒添加到带有标签的任何线程。最初我正在寻找带有标签的线程 = 1 条消息并且遇到了问题。所以我想如果我“提升一个级别”到添加标签的位置可能会更容易,但有同样的问题,我需要查找只有 1 条消息的线程。
标签: google-apps-script gmail-api