Home > Back-end >  Email not being sent in an onEdit function google sheet script
Email not being sent in an onEdit function google sheet script

Time:12-08

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);
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

function test(){
  MailApp.SendEmail("[email protected]", "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.

CodePudding user response:

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.

  • Related