My sheet runs some calculations on a chart of numbers which updates automatically from a time-driven script. Within that script I put at its very end a function which compares two of the resulting values corresponding to some price ratios, and according to which value is bigger it checks whether a certain cell contains the phrase "PRICES UP" or "PRICES DOWN". If the phrase contained corresponds to the opposite ratio, the function should send me an email notifying me of the prices crossing up or down and then change the contained phrase to the appropriate one.
var currentStatus = calcSheet.getRange("H18").getValue();
var currentGreen = calcSheet.getRange("H3").getValue() * (-1);
var currentRed = calcSheet.getRange("H5").getValue();
if(currentGreen > currentRed){
if(currentStatus == "PRICES UP"){
MailApp.sendEmail("myemail", "Prices Crossed Down", "");
calcSheet.getRange("H18").setValue("PRICES DOWN");
}
else{
return;
}
}
else if(currentRed > currentGreen){
if(currentStatus == "PRICES DOWN"){
MailApp.sendEmail("myemail", "Prices Crossed Up", "")
calcSheet.getRange("H18").setValue("PRICES UP");
}
else{
return;
}
}
But the issue is that whenever the ifs return true and it tries to send an email, it results in the error "Exception: You do not have permission to call MailApp.sendEmail. Required permissions: https://www.googleapis.com/auth/script.send_mail". I don't know if triggered scripts should even encounter such errors, but I tried adding the "script.send_email" permission to the appsscript.json file as suggested elsewhere and that didn't solve it.
Any idea what is wrong and how to fix it?