我有以下代码,当单元格发生变化时发送电子邮件通知,但它总是返回超出范围的单元格引用(第5行,“ Missing Cuts Report”文件)。任何人都可以建议我是否需要更改代码,或者是否有办法使它停止失败并仍然通过发送电子邮件通知。我使用的触发器是OnChange,因为当它在OnEdit上时,它在第17行MailApp.sendEmail(recipients,subject,msgPlain,{htmlBody:body})上调用了错误服务多次。任何帮助将不胜感激。
function sendEnailNotification(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
if(SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName() == "Missing Cuts Report") {
if(cell.indexOf('B')!=-1){
if(cellvalue > "") {
//Define Notification Details
var recipients = "email@email.co.uk;
var subject = "New Missing Cut Added";
var body = 'A new line has been added on the Missing Cuts Report on line <b>' + cell + '</b> - click <a href="' + ss.getUrl() + '">here</a> to view the update';
var msgPlain = body.replace(/(<([^>]+)>)/ig, ""); // clear html tags for plain mail
MailApp.sendEmail(recipients, subject, msgPlain, { htmlBody: body });
}
}
}
}
答案 0 :(得分:0)
嗨,这不是获得所需结果的很好方法。不确定要做什么。但是,在大括号后面,我对带有注释的代码进行了一些调整。这不是您原始问题的答案。只是一些提示。
function sendEmailNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
if(sheet.getName() === "Missing Cuts Report") {
//if(cell.indexOf('B')!== -1){
if(cellvalue !== "") {
//Define Notification Details
var recipients = "email@address.org";
var subject = "New Missing Cut Added";
var body = 'A new line has been added on the Missing Cuts Report on line <b>' + cell + '</b> - click <a href="' + ss.getUrl() + '">here</a> to view the update';
var msgPlain = body.replace(/(<([^>]+)>)/ig, ""); // clear html tags for plain mail
MailApp.sendEmail(recipients, subject, msgPlain, { htmlBody: body });
} // "<" and ">" are not for comparing string values. (At least not in this case.)
//} //IndexOf will always result in -1 if you are looking for just "B" as your result will always be "B1/B2/B3/ect."
} // Already have the sheet object, no need to call again.
}