Darthvazor
Darthvazor

Reputation: 53

Why sendEmail function is not working?

I am trying to figure out what's went wrong with my Google Apps Script. I am trying to send the email when any of cells in column 2 is no longer have "-" in it while the next cell is "No". Apparently, sendEmail function is not working for some reason.

I make little example of small spreadsheet below. I want to send the email when third row is matched.

   1  2  3
1  00 - Yes
2  00 - No
3  00 x No

Here is my code:

function onEdit() {
     var s = SpreadsheetApp.getActiveSheet();
     if( s.getName() == "Sheet4" ) { //checks that we're on the correct sheet
       var r = s.getActiveCell();
       var nextCell = r.offset(0, 1);
       if(( r.getColumn() == 2 ) && ( r.getValue() !== '-' ) && ( nextCell.getValue() === 'No' )){ //checks the cell
           MailApp.sendEmail('[email protected]', 'test email from Google Spreadsheet', 'Let me know if this came through ok');    
       }
     }
    }

Upvotes: 0

Views: 1427

Answers (2)

Mogsdad
Mogsdad

Reputation: 45750

The onEdit() function is an example of a Simple Trigger Function. As described in Understanding Triggers, simple triggers cannot access services that require authentication. That is why MailApp and GmailApp are not available for sending mail.

Upvotes: 1

Srik
Srik

Reputation: 7965

First, the condition to check inequality is !=. So try the modified code below. Second, you are sending the email to [email protected]. I assume the actual code sends the email to a real address.

function onEdit() {
     var s = SpreadsheetApp.getActiveSheet();
     if( s.getName() == "Sheet4" ) { //checks that we're on the correct sheet
       var r = s.getActiveCell();
       var nextCell = r.offset(0, 1);
       if(( r.getColumn() == 2 ) && ( r.getValue() != '-' ) && ( nextCell.getValue() == 'No' )){ //checks the cell
           MailApp.sendEmail('[email protected]', 'test email from Google Spreadsheet', 'Let me know if this came through ok');    
       }
     }
    }

Upvotes: 1

Related Questions