0

I have code wherein when I select a checkbox in column 'N', it should send an email. It goes through the pass1, pass2 and pass3 alerts but after that, nothing. It worked when I tried to run the script in the script editor by replacing the selRow and selCol variables with a hardcoded number value.

function onEdit(e) {

  var selRow = e.range.getRow();
  var selCol = e.range.getColumn();

  var activeWS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Active');
  var closeWS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Closed');

  if(activeWS.getRange(selRow, selCol).getValue() == true && selCol == 14){

    SpreadsheetApp.getUi().alert('pass 1');

    // Fetch the email address
    var emailAddress = 'sample@gmail.com';
    var subjPart = activeWS.getRange(selRow, 2).getValue();

    SpreadsheetApp.getUi().alert('pass2');

    // Send Alert Email.
    var message = 'Please visit www.sample.com to view the latest project ready for Cap Out';
    var subject = 'Roofing Project ' + subjPart + 'is ready for Cap Out';
    SpreadsheetApp.getUi().alert('pass3');
    MailApp.sendEmail(emailAddress , subject, message);

    SpreadsheetApp.getUi().alert('Email sent');

  }

}

Any ideas?

Vince
  • 3
  • 1
  • 1
    Although I'm not sure whether my understanding of your situation is correct, I think that if you try to run `onEdit` by the simple trigger, an error occurs at `MailApp.sendEmail`. Because in this case, it is required to authorize. So how about using the installable OnEdit event trigger and testing it again? [Ref](https://developers.google.com/apps-script/guides/triggers/installable) At that time, please rename the function name for avoiding the duplicate execution of the function. If this was not the direct solution of your issue, I apologize. – Tanaike Apr 04 '20 at 12:52
  • 1
    As Tanaike said, are you using a simple or installed trigger? [Triggers](https://stackoverflow.com/questions/45155847/google-script-project-trigger-not-running/45157034#45157034) – James D Apr 04 '20 at 13:55
  • 1
    Tanaike's solution is right. I switched to installable trigger and change the name of my onEdit function. It worked! Thanks Tanaike. How can i mark this question as solved? – Vince Apr 05 '20 at 13:04

1 Answers1

0

In order to resolve your issue, I would like to propose to use the installable OnEdit event trigger. When you run the function of onEdit as the simple trigger, an error occurs at MailApp.sendEmail, because in this case, it is required to authorize.

As an important point, please rename the function name of onEdit to others. By this, the duplicate execution of the function can be avoided. For example, when onEdit is installed as the installable OnEdit event trigger, when a cell is edited, onEdit is run by both the simple trigger and the installable trigger. So in this case, it is required to rename the function name from onEdit to others. This can be seen at this report.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165