Search code examples
google-apps-scriptgoogle-apps

Google Apps Script Email Notification Not Functioning Correctly


I could really use some help, and I'd really appreciate any pointers anyone can give. I know there are some other similar questions out there, but from what I can find, no one else has this specific issue/request.

I have a Google Sheet that three different people collaborate on. I have this notification script that will notify specific users via email about any changes to any of the cells in the F column. Importantly, this email notification also contains the value of the A cell in that row that has had it's F cell value changed.

However, what I'm really trying to achieve is a notification script that will email a specified person only when any cell in column F has a new value of "Yes" in Sheet2 of the master sheet.

The code I have so far is (that sends a notification when any change happens in the F column):

function sendNotification(e) {
  var received = e.value; //Gets the value of the edited cell
};
    function sendNotification() {
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var sheet = ss.getSheetByName("Sheet 2");
          var cell = ss.getActiveCell().getA1Notation();
          var row = sheet.getActiveRange().getRow();
          var cellvalue = ss.getActiveCell().getValue().toString();
          var received = ss.getRange('F2:F').getValue();
          var recipients = "example@gmail.com";
          var message = '';
          if(received==="Yes"){ 
            message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
            var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
            var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
           MailApp.sendEmail(recipients, subject, body)};
        Logger.log('received value: ' + received);
        };

Solution

  • If it helps, below is a code that works in my tests : (set up an installable trigger to make it work)

    note : I added a couple of Logger.log here and there to check the variables and a Browser message to avoid sending mails while testing.

    function sendNotification() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("Sheet 2");
      var cell = ss.getActiveCell();
      var row = sheet.getActiveRange().getRow();
      var col = cell.getA1Notation().replace(/[0-9]/g,'').toLowerCase();
      Logger.log('col = '+col);
      var cellValue = cell.getValue().toString().toLowerCase();
      Logger.log(cellValue);
      var recipients = "example@gmail.com";
      var message = '';
      Logger.log(cellValue=="yes" && col=='f');
      if(cellValue=="yes" && col=='f' ){ 
        message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
        var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
        var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellValue + '». For message: «' + message + '»';
    //    MailApp.sendEmail(recipients, subject, body)
        Browser.msgBox('mail sent');
      }
        Logger.log('message body: ' + body);
    };