0

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);
  }
Vincent
  • 81
  • 8
  • Where is your code and problem? – Sangbok Lee Apr 02 '17 at 11:05
  • I rewrote my question now with the code I had untill sofar – Vincent Apr 03 '17 at 09:24
  • Check [this](http://stackoverflow.com/questions/30798530/gmailapp-add-label-to-specific-message-not-the-thread). It's exactly the same problem as yours. – Sangbok Lee Apr 03 '17 at 13:39
  • Possible duplicate of [GmailApp - Add label to specific message, not the thread](http://stackoverflow.com/questions/30798530/gmailapp-add-label-to-specific-message-not-the-thread) – Sangbok Lee Apr 03 '17 at 13:39
  • 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 Apr 04 '17 at 09:56

1 Answers1

0

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.

Community
  • 1
  • 1
Sangbok Lee
  • 2,132
  • 3
  • 15
  • 33
  • Unfortunately this is not what I am looking for. I have done another workaraound but that is also not perfect, but the best I can find now. Gmail and threads are for years a headache, that's for sure. Thanks for the suggestion. – Vincent Apr 08 '17 at 10:33