在谷歌脚本中的特定工作表上使用 onedit() 触发器用于谷歌工作表 [英] use onedit() trigger on a specific sheet within google scripts for google sheets

查看:23
本文介绍了在谷歌脚本中的特定工作表上使用 onedit() 触发器用于谷歌工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个由 onedit() 触发的脚本运行到只有一张纸上.

I need to run a script triggered by an onedit() to only one sheet of many.

我已经尝试了以下操作,但目前我无法让脚本仅在所需的工作表(库存")上工作,我相信这对于知道的人来说非常简单:

I have tried the following, but currently I can't get the script to work on just the desired sheet ("Inventory") Im sure this will be very simple for someone that knows:

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");
        }
    }
}

谢谢!

推荐答案

尝试这样的事情,看看它是否有效:

try something like this and see if it works:

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");
}

现在,如果活动工作表不是库存"或编辑的值不是通知",脚本将退出.

Now the script will exit if the active Sheet is not 'Inventory' or the edited value is not 'notify'.

这篇关于在谷歌脚本中的特定工作表上使用 onedit() 触发器用于谷歌工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆