使用Google Apps脚本搜索/替换特定邮件(而不是整个主题)中的标签 [英] Search/replace labels in specific messages (not the whole thread) with Google Apps Script

查看:156
本文介绍了使用Google Apps脚本搜索/替换特定邮件(而不是整个主题)中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Gmail中使用的会话视图处于关闭状态,我需要搜索带有"Oldlabel"的特定邮件,并且我只想将这些带有Google脚本的特定邮件的标签更改为"Newlabel".

I am working in Gmail with conversation view Off and I need to search for specific messages with 'Oldlabel' and I want only to change the labels of these specific messages with Google script to 'Newlabel'.

我现在只能设法找到消息的标签名称,并获得整个线程的所有消息,即使这些单独的消息不包含此标签也是如此.因此,它们也将重命名为"NewLabel",这是不必要的.

I now can only manage to find label names of messages and get all the messages of the whole thread, even if those individual messages don't contain this label. So they will be renamed to 'NewLabel' too and that is unwanted.

这是我现在所拥有的代码.

Here is the code I have untill now.

var oldlabel = GmailApp.getUserLabelByName("Oldlabel");
var newlabel = GmailApp.getUserLabelByName("Newlabel");
var threads = GmailApp.search("label:Oldlabel")
  for (var i=0; i< threads.length; i++) {
    threads[i].removeLabel(oldlabel);
    threads[i].addLabel(newlabel);
  }

推荐答案

此处澄清.使用 GmailThread 类具有getLabels().同样, GmailLabel 类具有addToThread()等,但是它没有与消息相关的方法.如果官方文档中没有某些内容,则说明该内容不存在.

Clarification here. Whether conversation view is on or off, or whether you know the ids of messages or not, with Gmail Service you cannot manipulate individual messages's label because GmailMessage class has no methods related to labels. Only GmailThread class has getLabels(). Also GmailLabel class has addToThread(), addToThreads(), etc, but it has no methods related to messages. If something is not in the official documentation, it does not exist.

所以我建议您另一个答案,它使用了 Gmail API .我还没有亲自测试过它,但是它看起来像一样工作.请自己尝试.

So I've proposed you another answer, which used Gmail API. I have not tested it myself, but it looks like working. Please try it yourself.

这篇关于使用Google Apps脚本搜索/替换特定邮件(而不是整个主题)中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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