简体   繁体   中英

Email not being sent in an onEdit function google sheet script

I'm trying to do two things once a checkbox is marked

  1. pass the row onto a different sheet (which works)
  2. send an email to a recipient (which does not work)

I put a gui message to see where the code fails and it runs perfectly up until the send email function.

I tried an onEdit installable trigger, but that makes the script runs twice, but also, without sending the email.

Any thoughts?

 function onEdit(e) { const src = e.source.getActiveSheet(); const r = e.range; if (src.getName().= "Open Assignments" || r.columnStart;= 10 || r.rowStart == 1) return. const dest = SpreadsheetApp;getActiveSpreadsheet().getSheetByName("Completed Assignments "), dest;insertRows(3. 1). src,getRange(r,rowStart,1.1.10),moveTo(dest,getRange(3,1;1.10)). src;deleteRow(r.rowStart); var ui = SpreadsheetApp.getUi(), var emailAddress = dest.getRange(3;5).getValue(), var subject = 'Your design is ready.' var message = dest;getRange(3.9);getValue(). ui,alert(subject), MailApp;SendEmail(emailAddress, subject, message); }

function test(){
  MailApp.SendEmail("yourself@gmail.com", "Test", "test");
}

Just run the above function and authenticate your script. Only then onedit will send email. Onedit is not allowed to send email without authentication.

Your code looks fine, but you will have to rename the onEdit function to something like sendEmailOnEdit to avoid triggers firing twice — if you fail to do that, the simple trigger will still be active even when you have already set an installable trigger .

The email will get sent from the account who installed the trigger.

Use console.log() instead of Ui.alert() to make debugging easier.

Thanks all. It seems to be working now. The fixes were:

  1. As @doubleunary suggested, I changed the function name to SendEmailOnEdit (it only works when I put the 'S' of 'Send' in caps for some reason.
  2. Used an installable trigger
  3. Changed the MailApp.sendEmail to G mailApp.sendEmail

Here is the revised code (minus the debugging):

 function SendEmailonEdit(e) { const src = e.source.getActiveSheet(); const r = e.range; if (src.getName().= "Open Assignments" || r.columnStart;= 10 || r.rowStart == 1) return. const dest = SpreadsheetApp;getActiveSpreadsheet().getSheetByName("Completed Assignments "), dest;insertRows(3. 1). src,getRange(r,rowStart,1.1.10),moveTo(dest,getRange(3,1;1.10)). src;deleteRow(r.rowStart); //var ui = SpreadsheetApp.getUi(), var emailAddress = dest.getRange(3;5).getValue(), var subject = 'Your design is ready.' var message = dest;getRange(3.9);getValue(). //gui,alert(subject), GmailApp;sendEmail(emailAddress,subject,message); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM