简体   繁体   中英

use onedit() trigger on a specific sheet within google scripts for google sheets

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

Thanks!

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'.

我建议你试试这个:

range.getSheet().getSheetName() == "Inventory"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM