17

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 protected]";
            MailApp.sendEmail(emailAddress, subject, message);
            range.offset(0,1).setValue("notified");
        }
    }
}

Thanks!

2 Answers 2

24

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 protected]";
    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'.

12
  • 3
    Although this is a onEdit script, you will have to use an installable trigger (add a trigger > on edit of the spreadsheet) because of the emailing.
    – JPV
    Nov 24, 2014 at 19:05
  • Thanks JPV, unfortunately this didn't work. It doesn't seem to be recognising the sheet. The rest of the function works fine when used on any sheet as I have already installed the onedit. I just cant seem to get it working on just this one! Thanks for any suggestions! Nov 24, 2014 at 20:15
  • 1
    Did you check if the name in tab doesn't contain extra spaces ?
    – JPV
    Nov 24, 2014 at 21:24
  • Yeah just checked that, still doesn't seem to work. Gets caught on that first statement still. Any other suggestions? Nov 24, 2014 at 21:34
  • 1
    I just tested the exact script above and it works fine. please dubbelcheck the tab name and also change the function name to something other than onEdit to avoid internal conflict with the simple onEdit. For example installableOnEdit(e) Nov 24, 2014 at 21:38
9

I suggest that you try this:

range.getSheet().getSheetName() == "Inventory"
1
  • While I don’t know whether this worked in 2014, however, today it works. You can simply use e.range.getSheet().getSheetName() to get the name of the sheet name where the change occurred. Feb 16 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.