Google Script - 适用于多个工作表的一个Run-on-Edit脚本

时间:2017-08-28 00:34:02

标签: google-sheets google-sheets-api

我有这个完美的脚本 -

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

因此,如果在B94中将“Y”写为表1,则会触发脚本1.如果在表2 B54中写入,则会触发脚本2。

我想为此添加更多工作表和更多脚本。

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

这不起作用。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您的第二个脚本不再遵循三元或条件运算符的正确语法,即

  

变量==条件? value1:value2

要测试几个条件,语法将是例如:

  

变量== condition1? value1:变量== condition2? value2:变量== condition3? value3:value4;

或者在你的例子中:

  

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

继续尝试这个工作示例。为了便于阅读,我现在开始了每条新条件。

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