Chris Norris
Chris Norris

Reputation: 197

Google Script - One Run-on-Edit script for multiple sheets

I have this script which works perfectly -

function runOnEdit(e){
var sheets = ["Sheet 1", "Sheet 2"];  // change sheet names to suit.
var ind = sheets.indexOf(e.source.getActiveSheet().getName());
if(ind == -1 || e.range.getA1Notation() !== "B94" && e.range.getA1Notation() !== "B54"  || e.value.toLowerCase() !== "y") return; 
ind == 0 ? Script1() : Script2(); 
}

So if "Y" is written in B94 as Sheet 1, it fires Script 1. If it is written in Sheet 2 B54, it fires Script 2.

I want to add in more sheets and more scripts to this.

function runOnEdit(e){
var sheets = ["HAR 2 Yelo", "HAR 2 Bambello", "HAR 2 KM5512"];  // change sheet names to suit.
var ind = sheets.indexOf(e.source.getActiveSheet().getName());
if(ind == -1 || e.range.getA1Notation() !== "B94" && e.range.getA1Notation() !== "B54"&& e.range.getA1Notation() !== "B54"  || e.value.toLowerCase() !== "y") return; 
ind == 0 ? CopyYeloPlants() : CopyBambelloPlants() : CopyKM5512Plants() ; 
}

This doesn't work. What am I missing?

Upvotes: 0

Views: 471

Answers (1)

Terence Hoefdraad
Terence Hoefdraad

Reputation: 11

Your second script was no longer following proper syntax of the ternary or conditional operator, which is

variable == condition ? value1 : value2

To test for several conditions the syntax would be for example:

variable == condition1 ? value1 : variable == condition2 ? value2 : variable == condition3 ? value3 : value4;

Or in your example:

variable == condition1 ? doThis() : variable == condition2 ? doThat() : variable == condition3 ? doSomehingElse() : return;

Go ahead and try this working example. I started each now condition on a new line for readability.

function answerTheQuestion() {
//Hint: Correct answer is "A"
var yourAnswer = "A"; //Fill in your answer here and run the script;
yourAnswer == "C" ? Logger.log("Wrong answer!") : 
yourAnswer == "B" ? Logger.log("Try again") : 
yourAnswer == "A" ? Logger.log("That's right!") : 
Logger.log("3 strikes and your out")  
}

Upvotes: 1

Related Questions