Object reference
Gmail label
A Gmail label is a tag you can apply to conversations. A script can look one up by name, and can add or remove it from threads.
GmailApp.GmailLabelStart here
Get this object first
Look up a label by its exact name. If it does not exist yet, create it so the rest of the script always has a label to use.
function getOrCreateProcessedLabel() {
const labelName = 'Processed by script';
let label = GmailApp.getUserLabelByName(labelName);
if (!label) {
label = GmailApp.createLabel(labelName);
}
Logger.log(label.getName());
} Then use it
Common actions
Choose an action, then open its example only when you need the code.
Count the threads carrying a label
This counts the conversations tagged with the label and does not change any of them.
GmailApp.GmailLabelShow example code
function logProcessedLabelThreadCount() {
const label = GmailApp.getUserLabelByName('Processed by script');
if (!label) throw new Error('Create a Gmail label named Processed by script first.');
Logger.log(label.getThreads().length);
} Add the label to a thread (this changes data)
This adds the label to the newest matching conversation. It changes that conversation, but it does not delete or send anything.
GmailApp.GmailLabelGmailApp.GmailThreadShow example code
function addProcessedLabelToLatestThread() {
const label = GmailApp.getUserLabelByName('Processed by script');
if (!label) throw new Error('Create a Gmail label named Processed by script first.');
const threads = GmailApp.search('newer_than:1d', 0, 1);
if (!threads.length) return;
label.addToThread(threads[0]);
Logger.log('Label added to: ' + threads[0].getFirstMessageSubject());
} List all of your label names
This lists the names of every label in your Gmail account and does not change any of them.
GmailApp.GmailLabelShow example code
function logAllLabelNames() {
const names = GmailApp.getUserLabels().map((label) => label.getName());
Logger.log(names.join(', ') || 'No labels');
} Technical reference
Exact names and signatures
Generated runtime and signature facts stay separate from the beginner actions above. Open method details only when you need them.
At a glance
- Service
- GmailApp
- Reference symbol
GmailApp.GmailLabel- Object
GmailLabel- Runtime
- From signature data
Methods and signatures
Browse 9 methods
addToThread
- addToThread(thread: GmailApp.GmailThread): GmailApp.GmailLabel
addToThreads
- addToThreads(threads: GmailApp.GmailThread[]): GmailApp.GmailLabel
deleteLabel
- deleteLabel(): void
getId
- getId(): string
getName
- getName(): string
getThreads
- getThreads(): GmailApp.GmailThread[]
- getThreads(start: number, max: number): GmailApp.GmailThread[]
getUnreadCount
- getUnreadCount(): number
removeFromThread
- removeFromThread(thread: GmailApp.GmailThread): GmailApp.GmailLabel
removeFromThreads
- removeFromThreads(threads: GmailApp.GmailThread[]): GmailApp.GmailLabel
Need the whole workflow?
Return to a task guide
Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.
Browse Gmail tasks