我有代码,当我选中“ N”列中的复选框时,它应该发送一封电子邮件。它通过pass1,pass2和pass3警报,但此后什么也没有。当我尝试用硬编码数字值替换selRow和selCol变量,从而在脚本编辑器中运行脚本时,此方法起作用了。
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');
}
}
有什么想法吗?
答案 0 :(得分:0)
为了解决您的问题,我建议使用可安装的OnEdit事件触发器。当您将onEdit
函数作为简单触发器运行时,MailApp.sendEmail
会发生错误,因为在这种情况下,需要进行授权。
重要的是,请将onEdit
的函数名重命名为其他函数。这样,可以避免功能的重复执行。例如,将onEdit
安装为可安装的OnEdit事件触发器时,在编辑单元格时,简单触发器和可安装触发器都将运行onEdit
。因此,在这种情况下,需要将功能名称从onEdit
重命名为其他名称。可以在this report上看到。