Search/replace labels in specific messages (not the whole thread) with Google Apps Script

北慕城南 提交于 2020-01-17 07:31:25

问题


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'.

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);
  }

回答1:


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.

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.



来源:https://stackoverflow.com/questions/43166342/search-replace-labels-in-specific-messages-not-the-whole-thread-with-google-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!