使用 Google Apps 脚本,您如何从电子邮件中删除内置的 Gmail 类别标签? [英] Using Google Apps Script, how do you remove built in Gmail category labels from an email?

查看:54
本文介绍了使用 Google Apps 脚本,您如何从电子邮件中删除内置的 Gmail 类别标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全撤消任何 Gmail 内置的类别标签.这是我的尝试.

I'd like to completely undo any of Gmails built in category labels. This was my attempt.

function removeBuiltInLabels() {
  var updatesLabel = GmailApp.getUserLabelByName("updates");
  var socialLabel = GmailApp.getUserLabelByName("social");
  var forumsLabel = GmailApp.getUserLabelByName("forums");
  var promotionsLabel = GmailApp.getUserLabelByName("promotions");

  var inboxThreads = GmailApp.search('in:inbox');
  for (var i = 0; i < inboxThreads.length; i++) {
    updatesLabel.removeFromThreads(inboxThreads[i]);
    socialLabel.removeFromThreads(inboxThreads[i]);
    forumsLabel.removeFromThreads(inboxThreads[i]);
    promotionsLabel.removeFromThreads(inboxThreads[i]);
  }

}

然而,这会引发......

However, this throws....

TypeError:无法调用 null 的方法removeFromThreads".

TypeError: Cannot call method "removeFromThreads" of null.

即使您可以在 Gmail 搜索框中成功搜索 label:updates 并获得正确的结果,但您似乎无法通过这种方式访问​​内置标签.

It seems you can't access the built in labels in this way even though you can successfully search for label:updates in the Gmail search box and get the correct results.

问题...

如何访问 Google Apps 脚本中内置的 Gmail 类别标签并将其从电子邮件/线程/线程中删除?

谢谢.

推荐答案

INBOX"和CATEGORY_SOCIAL"等其他系统标签可以使用高级 Gmail 服务删除.在脚本编辑器中,转至资源 -> 高级 Google 服务并启用 Gmail 服务.有关 Gmail 中系统标签命名约定的更多详细信息,请参见此处Gmail API - 管理标签

'INBOX' and other system labels like 'CATEGORY_SOCIAL' can be removed using Advanced Gmail Service. In the Script Editor, go to Resources -> Advanced Google services and enable the Gmail service. More details about naming conventions for system labels in Gmail can be found here Gmail API - Managing Labels

通过调用线程集合的 list() 方法检索标有CATEGORY_SOCIAL"的线程:

Retrieve the threads labeled with 'CATEGORY_SOCIAL' by calling the list() method of the threads collection:

 var threads = Gmail.Users.Threads.list("me", {labels: ["CATEGORY_SOCIAL"]});
 var threads = threads.threads;
 var nextPageToken  = threads.nextPageToken;

请注意,您将需要存储nextPageToken"以遍历整个线程集合.请参阅此答案.获得所有线程 id 后,可以对它们调用 Threads 集合的 'modify()' 方法:

Note that you are going to need to store the 'nextPageToken' to iterate over the entire collection of threads. See this answer. When you get all thread ids, you can call the 'modify()' method of the Threads collection on them:

 threads.forEach(function(thread){
         var resource = {
        "addLabelIds": [],
        "removeLabelIds":["CATEGORY_SOCIAL"]
      };
      Gmail.Users.Threads.modify(resource, "me", threadId);
});

如果您的收件箱中有很多线程,您可能仍需要多次调用 'modify()' 方法并在两次调用之间保存状态.

If you have lots of threads in your inbox, you may still need to call the 'modify()' method several times and save state between calls.

这篇关于使用 Google Apps 脚本,您如何从电子邮件中删除内置的 Gmail 类别标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆