如何用Google Apps Script实现Gmail指定标签下未读邮件归类
Gmail标签自动归类解决方案
问题背景
尝试了Gmail筛选器的所有可用选项均未达成预期效果,由于Gmail筛选器存在无法同时选择多个标签或分类的限制,希望通过Google Apps Script实现对应需求。
更新需求
将符合条件的邮件归类到Customer1标签的子标签Unread中。
解决脚本
以下是@Tanaike提供的最终解决方案:
function customer1_unread_label() { const customer1_label1 = "customer1"; // 主标签名称 const unreadCustomer1_label2 = "customer1/Unread customer1"; // 子标签名称 const label2 = GmailApp.getUserLabelByName(unreadCustomer1_label2); const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`); threads1.forEach(thread => { if (!thread.getLabels().find(l => l.getName() == unreadCustomer1_label2)) { thread.addLabel(label2); } }); const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`); threads2.forEach(thread => thread.removeLabel(label2)); }
脚本逻辑说明
- 定义主标签
customer1和目标子标签customer1/Unread customer1的名称 - 搜索所有带有主标签的未读邮件,为未添加子标签的邮件批量添加子标签
- 搜索所有带有目标子标签的已读邮件,批量移除子标签,确保子标签仅标记主标签下的未读邮件
内容的提问来源于stack exchange,提问作者Maarouf Alsheikh
相关产品推荐
相关产品推荐

