Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active February 3, 2018 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/69c7daf910fdad0d6a296ea19f612089 to your computer and use it in GitHub Desktop.
Save tanaikech/69c7daf910fdad0d6a296ea19f612089 to your computer and use it in GitHub Desktop.
Adding a Label to a Message using Message ID for Gmail

Adding a Label to a Message using Message ID for Gmail

These are sample scripts for adding a label to a message using message ID for Gmail.

Sample 1

This sample adds a label to a thread using message ID. In this case, all messages in the thread have the label. Even if it adds a label to a message in the thread using addLabel(), all messages in the thread have the label, becauce addLabel can only be used for the thread.

var messageId = "#####";
var label = "samplelabel";
GmailApp.getMessageById(messageId).getThread().addLabel(GmailApp.getUserLabelByName(label));

Sample 2

If you want to give a label to one of messages in the thread, please use this. This sample adds a label to a message using message ID. In this case, only one message of message ID in the thread has the label.

In order to use this, please enable Gmail API at Advanced Google Services and API console.

var messageId = "#####";
var userId = "me";
var label = "samplelabel";
var labelId = Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id;
Gmail.Users.Messages.modify({"addLabelIds": [labelId]}, userId, messageId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment