The below App Script fetches the first message of the first Gmail Inbox thread and checks the "From:" header against a regular expression.
Based on the outcome, we want to use addLabel() to set a Gmail label.
It performs the fetch and test fine, but fails when attempting to set the label - Cannot find method addLabel(string). (line 15, file "Code")
function myFunction() {
// Get first thread in Inbox.
var thread = GmailApp.getInboxThreads(0,1)[0];
// Get the first message in the thread.
var message = thread.getMessages()[0];
// Get a message header
var headerstring = message.getHeader("From");
// Check header for "rob" test string, apply label
if ( headerstring.match(/rob/g) ) {
thread.addLabel("MyLabel");
Logger.log("Matched rob");
} else {
thread.addLabel("AnotherLabel"); // failing, not class scope?
Logger.log("No match");
}
}
It feels like the existence of addLabel within the if clause has robbed it of the application of GmailApp, since I have had addLabel functioning outside of one - am I right? This is my first script.
How can I overcome this?
addLabel(string)- TheMaster