0
votes

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
Where is your code and problem? - Sangbok Lee
I rewrote my question now with the code I had untill sofar - Vincent
Check this. It's exactly the same problem as yours. - Sangbok Lee
I am not sure if this is the solution. I am not concerning that I can add or remove a label to/from a specific message, but I need to know the message-id of the message(s) which I want to alter. I will rewrite my original question to make it more clear - Vincent

1 Answers

0
votes

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.