我有很多标签,我将安排每周删除它们。
我用谷歌搜索,发现这个函数删除一个标签:
function Delete_old_mail() {const threads = GmailApp.search("older_than:1w label:test-lable1");for (const thread of threads) {thread.moveToTrash();}};
但我将每周删除多个标签:
test-label1
test-label2
test-label3
这是我在项目中添加的:
function Delete_old_mail() {const threads = GmailApp.search("older_than:1w label:test-lable1");for (const thread of threads) {thread.moveToTrash();}};
function Delete_old_mail() {const threads = GmailApp.search("older_than:1w label:test-lable2");for (const thread of threads) {thread.moveToTrash();}};
function Delete_old_mail() {const threads = GmailApp.search("older_than:1w label:test-lable3");for (const thread of threads) {thread.moveToTrash();}};
但我知道这是不正确的,因为当我要添加一个新触发器时,它会检测到一次
Delete_old_mail
功能。
怎样才能有一个功能来删除多个标签?
在现实世界中,我有大约 100 个标签,定义 100 个触发器并不容易,我认为在一个函数下管理它们会更容易,因为对于这 100 个标签,我不需要超过一周的电子邮件。
下面的脚本在标签数组上循环,以搜索超过 7 天或 1 周的电子邮件。
function Delete_old_mail() {
var labels = ["test-lable1", "test-lable2", "test-lable3"]
labels.forEach(x => {
const threads = GmailApp.search(`older_than:7d label: ${x}`);
for (const thread of threads) {
thread.moveToTrash();
}
})
}
参考资料: