0

I copied this script from Stackoverflow and changed it a little bit according to my requirements. It is supposed to send an email to an email address in Column D when the checkbox is checked in Column I.

But it is giving this error: "TypeError: Cannot read properties of undefined (reading 'source')" Here is my sample Sheet

`

function onEdit(e){
  var sheet = e.source.getActiveSheet();
  var cell = e.range;

  Logger.log(cell.getColumn());
  Logger.log(cell.isChecked());
  //Check if the checkbox in column I(index 9) was checked
  if(sheet.getName() == "Sheet2" && cell.getColumn() == 9 && cell.isChecked()){

    //get current row values from column A to column G
    var values = sheet.getRange(cell.getRow(),1,1,7).getDisplayValues().flat();
    Logger.log(values);
    var email = values[4];

    
    var message = "Your application is Approved";

    //Send email
    MailApp.sendEmail({
      to: email,
      subject: "Your Application Status ",
      htmlBody: message
    });

    
  }
}

`

I tried a script from Stackoverflow but It gave error.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). See [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/13045193) – doubleunary Dec 16 '22 at 08:02

1 Answers1

1

Do not run the code directly in the script editor. If you do, the event parameter e is not populated, causing the error you mention.

The onEdit(e) function is a simple trigger that is designed to run automatically when you manually edit the spreadsheet. In that context, the event object e is properly populated.

Simple triggers run in a restricted context where the identity of the user at the keyboard is usually not available. "They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions." See Session.getActiveUser() and Session.getEffectiveUser().

Further, you cannot send email from a simple trigger. Use an installable trigger instead. See your script project's execution log to see the errors your script is producing.

doubleunary
  • 13,842
  • 3
  • 18
  • 51