Reputation: 802
How do you get the Label ID by Label Name?
I can get the Label ID by getting the list of Labels and parsing the response. But that looks a pretty inefficient way of doing it when I'm only interested in a particular label.
Is it possible to get Label ID directly be passing the Label Name?
Upvotes: 10
Views: 1995
Reputation: 83
I created this function for just that purpose, hope it helps:
function getLabelIdByName(labelName) {
var me = Session.getEffectiveUser().getEmail();
var labels = Gmail.Users.Labels.list(me, {q: ""});
for (var l=labels['labels'].length-1; l>=0; l--) {
if (labels['labels'][l]['name'] === labelName) {
return labels['labels'][l]['id'];
}
}
}
NOTE: For this function to run correctly you must enable the Advanced "Gmail Service" in your script (pretty easy), learn more here: https://developers.google.com/apps-script/advanced/gmail
Upvotes: 1
Reputation: 112787
No, you cannot. However, listing your labels is one of the least expensive API calls you can do, so no worries!
Upvotes: 4