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

雨燕双飞 提交于 2020-01-07 02:59:47

问题


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



来源:https://stackoverflow.com/questions/44100256/send-an-e-mail-for-an-address-on-column-l-only-if-column-g-is-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!