我需要运行由 onedit() 触发的脚本,仅对其中的一张进行操作。
我已经尝试了以下方法,但目前我无法让脚本仅在所需的工作表(“库存”)上工作,我确信这对于知道的人来说会非常简单:
function onEdit(e) {
var range = e.range;
if(range.getSheetName() == "Inventory") {
if(range.getValue() == "notify") {
range.setBackgroundColor('red');
var productname = range.offset(0,-3).getValue();
var productinventory = range.offset(0,-2).getValue();
var message = "Product variant " + productname + " has dropped to " + productinventory;
var subject = "Low Stock Notification";
var emailAddress = "email@email.com";
MailApp.sendEmail(emailAddress, subject, message);
range.offset(0,1).setValue("notified");
}
}
}
谢谢!
最佳答案
尝试这样的事情,看看它是否有效:
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== "Inventory" || e.value !== "notify") return;
range.setBackgroundColor('red');
var productname = range.offset(0, -3).getValue();
var productinventory = range.offset(0, -2).getValue();
var message = "Product variant " + productname + " has dropped to " + productinventory;
var subject = "Low Stock Notification";
var emailAddress = "email@email.com";
MailApp.sendEmail(emailAddress, subject, message);
range.offset(0, 1).setValue("notified");
}
现在,如果事件工作表不是“Inventory”或编辑的值不是“notify”,脚本将退出。
关于function - 在 Google 表格的 Google 脚本中的特定表格上使用 onedit() 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107903/