You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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));
}

脚本逻辑说明

  1. 定义主标签customer1和目标子标签customer1/Unread customer1的名称
  2. 搜索所有带有主标签的未读邮件,为未添加子标签的邮件批量添加子标签
  3. 搜索所有带有目标子标签的已读邮件,批量移除子标签,确保子标签仅标记主标签下的未读邮件

内容的提问来源于stack exchange,提问作者Maarouf Alsheikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:18:13