Labeling Gmail message (not the whole thread) with Google Apps Script

♀尐吖头ヾ 提交于 2019-12-13 16:04:15

问题


Is it possible to search to messages with the label 'Apps script queue' and give just these specific messages (not the whole thread) a new label?

When I use GmailApp.search('label:Apps script queue') I get the requested messages but when I assign a new label to these messages, all the other messages of the thread (on other places in the mailbox) will get the same label. And that is not what I want.


回答1:


This code does not return an error while adding a label to a specific message in a thread and if you use thread list method you'll see that it is only placed in the specific messageID(treated separately). But once your UI(Gmail site) is in conversation mode, it will be viewable in both labels.

function searchMail(){
  var threads = GmailApp.search("SOME SEARCH");
  Logger.log(threads.length);
  listLabel('me');
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    Logger.log(messages.length);

    for (var j = 0; j < messages.length; j++){
      if (messages[j].isInInbox()){
        Logger.log('me' + 'id msg: ' + messages[j].getId());
        //Add label to the first reply
        addLabel('me',messages[1].getId());
      }
      else{
       Logger.log('me' + 'id msg: ' + messages[j].getId() +" not in inbox");
      }
    }
  }
}

function addLabel(userId, messageId){

  var resource = {addLabelIds: ["Label_6"]}

  Gmail.Users.Messages.modify(resource, userId, messageId);

}



回答2:


In Gmail, labels are applied to a thread and cannot be applied to a single email message of a thread.

You can however apply stars / colors to individual messages.



来源:https://stackoverflow.com/questions/43069307/labeling-gmail-message-not-the-whole-thread-with-google-apps-script

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