Skip to content

Instantly share code, notes, and snippets.

@tanaikech tanaikech/submit.md
Last active Feb 3, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.