问题
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 id
s 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