I'm trying to make an onEdit trigger to send an email. I know that it doesn't work by default, as described here:
Because simple triggers fire automatically, without asking the user for authorization, they are subject to several restrictions: They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.
But I thought I could make onEdit event to programmatically create another, time-driven trigger, which in turn will send the email. I know that both (1) my onEdit trigger works on its' own, and (2) manually running the function to programmatically create time-driven trigger to send email, work on its' own. But when I put the 2 inside 1, it doesn't work.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssName = ss.getName();
var budgetLeft = ss.getRange("Test!B1").getValue();
var emailAddress = ss.getRange("Test!B4").getValue();
if (budgetLeft <= 4860) {
function SendEmailOnTrigger() {
MailApp.sendEmail({
to: emailAddress,
subject: "Warning: " + ssName,
htmlBody: "Sample Message",
});
}
}
function onEdit(e){
function createTimeDrivenTriggersTest() {
// Trigger TEST.
ScriptApp.newTrigger('SendEmailOnTrigger')
.timeBased()
.everyMinutes(1)
.create();
}
}
Does it "knows" that the time-driven trigger that would be created will send email, which is not acceptable when initiated with simple trigger? That's why it doesn't work? Thanks in advance for any help.
createTimeDrivenTriggersTest
and simply create the trigger withinonEdit
. 2. Move yourif (budgetLeft <= 4860)
statement inside yourSendEmailOnTrigger
function declaration. – Eric Dauenhauer