Send an e-mail for an address on column L only if

2019-09-08 03:51发布

问题:

Could you guys give me some help? I'm a total noob, so I would really appreciate your help :)

I'm trying to create a script for a spreadsheet that sends a message to an e-mail address on column L every time, on the same row, column G is Y.

For example:

If G9 is edited to Y, then an e-mail is sent to address specified on L9.

Currently, I'm trying the script below but it is not working:

function onEditTrigger(e){
   var range = e.range;
   var intCol = range.getColumn();

   if (intCol == 7){
      var intRow = range.getRow();                              
      var sheet = SpreadsheetApp.getActiveSheet();
      var dataRange = sheet.getRange(intRow, 12, 11, 8);          
      var dataValues = dataRange.getValues();           

      MailApp.sendEmail(dataValues[0][0], "Notification", "Value in column C: " 
                                                             + dataValues[0][2]);
   }
}

Error:

"TypeError: Cannot read property "range" from undefined. (line 2, file "send e-mail")"

回答1:

Your code works fine but I can see a few issues.

  1. You don't say but when you get the error are you using the debugger? Note that the event object (e) only receives information when the trigger is fired. i.e. if you try to use the debugger to test an onEdit(e) function no information is passed to (e) as the trigger hasn't been fired.

  2. Have a read of the Google Documentation in particular the section about restrictions. It say's

They [simple triggers] cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

So for your script to work you need to go to Edit -> Current project's triggers and create an onEdit trigger