Google Apps Script

Class GmailLabel

A user-created label in a user's Gmail account.

Methods

MethodReturn typeBrief description
addToThread(thread)GmailLabelAdds this label to the given thread and forces the thread to refresh (GmailThread.refresh()).
addToThreads(threads)GmailLabelAdds this label to the given threads and forces the threads to refresh.
deleteLabel()voidDeletes this label.
getName()StringGets the name of this label.
getThreads()GmailThread[]Gets the threads that are marked with this label.
getThreads(start, max)GmailThread[]Gets a range of threads marked with this label.
getUnreadCount()IntegerGets the number of unread threads tagged with this label.
removeFromThread(thread)GmailLabelRemoves this label from the given thread and forces the thread to refresh.
removeFromThreads(threads)GmailLabelRemoves this label from the given threads and forces the threads to refresh.

Detailed documentation

addToThread(thread)

Adds this label to the given thread and forces the thread to refresh (GmailThread.refresh()).

 
// label the first thread in the inbox with the label MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 var firstThread = GmailApp.getInboxThreads(0,1)[0];
 label.addToThread(firstThread);
 

Parameters

NameTypeDescription
threadGmailThreadthe thread to be labeled

Return

GmailLabel — this GmailLabel, useful for chaining

See also


addToThreads(threads)

Adds this label to the given threads and forces the threads to refresh.

 
// label the first three threads in the inbox with the label MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 var threads = GmailApp.getInboxThreads(0,3);
 label.addToThreads(threads);
 

Parameters

NameTypeDescription
threadsGmailThread[]an array of threads to be labeled

Return

GmailLabel — this GmailLabel, useful for chaining

See also


deleteLabel()

Deletes this label.

 
var label = GmailApp.getUserLabelByName("MyLabel");
 label.deleteLabel();
 

See also


getName()

Gets the name of this label.

 
var label = GmailApp.getUserLabelByName("MyLabel");
 Logger.log(label.getName()); //logs MyLabel
 

Return

String — the name of the label


getThreads()

Gets the threads that are marked with this label. This call will fail when the size of all threads is too large for the system to handle. Where the thread size is unknown, and potentially very large, please use getThreads(start, max) and specify ranges of the threads to retrieve in each call.

 
// Log the subject lines of the threads labeled with MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 var threads = label.getThreads();
 for (var i = 0; i < threads.length; i++) {
   Logger.log(threads[i].getFirstMessageSubject());
 }
 

Return

GmailThread[] — an array of threads marked with this Label


getThreads(start, max)

Gets a range of threads marked with this label.

 
// log the subject lines of up to the first 30 threads with the label MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 var threads = label.getThreads(0, 30);
 for (var i = 0; i < threads.length; i++) {
   Logger.log(threads[i].getFirstMessageSubject());
 }
 

Parameters

NameTypeDescription
startIntegerthe index of the starting thread
maxIntegerthe maximum number of threads to return

Return

GmailThread[] — an array of threads marked with this label


getUnreadCount()

Gets the number of unread threads tagged with this label.

 
// log the number of unread threads labeled with MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 Logger.log(label.getUnreadCount());
 

Return

Integer — the number of unread labeled threads


removeFromThread(thread)

Removes this label from the given thread and forces the thread to refresh.

 
// remove the label MyLabel from the first thread in the inbox
 var label = GmailApp.getUserLabelByName("MyLabel");
 var firstThread = GmailApp.getInboxThreads(0,1)[0];
 label.removeFromThread(firstThread);
 

Parameters

NameTypeDescription
threadGmailThreadthe thread be unlabeled

Return

GmailLabel — this GmailLabel, useful for chaining

See also


removeFromThreads(threads)

Removes this label from the given threads and forces the threads to refresh.

 
// remove the label MyLabel from the first three threads in the inbox
 var label = GmailApp.getUserLabelByName("MyLabel");
 var threads = GmailApp.getInboxThreads(0,3);
 label.removeFromThreads(threads);
 

Parameters

NameTypeDescription
threadsGmailThread[]an array of threads to be unlabeled

Return

GmailLabel — this GmailLabel, useful for chaining

See also

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.