Google 用于 Gmail 删除*仅*指定标签的电子邮件的脚本

Google Script for Gmail to Delete Emails With *Only* the Specified Label

我正在尝试找到一种方法来定位只有一个标签的电子邮件。我想删除具有特定标签且超过 2 周的邮件。但是,我希望脚本忽略除指定标签外还有其他标签的所有电子邮件。

例如,一封带有标签 'Subscriptions' 的电子邮件被删除,但是一封带有标签 'Subscriptions' 的电子邮件被删除 AND 'Keep!'不会.

我当前的版本将删除任何带有指定标签的电子邮件,而不管是否应用了任何其他标签。

我使用公开可用的 Gmail Cleaning Robot script by Fred Mouniguet 作为我的基础。

这是我目前的情况:

function gmailCleaningRobot() {

var delayDays = 14; // will only impact emails more than 2 weeks old
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?

// Get all the threads labeled 'Subscriptions'
var label = GmailApp.getUserLabelByName("Subscriptions");
var threads = label.getThreads(0, 100);

// we archive all the threads if they're unread AND older than the limit we set in delayDays
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate && threads[i].isUnread())
{
threads[i].moveToTrash();
}
}
}

如有任何帮助,我们将不胜感激!

您可以通过在 if 语句的线程实例上使用 getLabels 来解决这个问题。

尝试以下操作。

if (threads[i].getLastMessageDate()<maxDate && threads[i].isUnread() 
    && threads.getLabels().length == 1)