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