我有这个完美运行的脚本 -
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();
}
因此,如果“Y”作为工作表 1 写入 B94 中,则会触发脚本 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() ;
}
这行不通。我错过了什么?
最佳答案
您的第二个脚本不再遵循三元或条件运算符的正确语法,即
variable == condition ? value1 : value2
要测试多种条件,语法如下:
variable == condition1 ? value1 : variable == condition2 ? value2 : variable == condition3 ? value3 : value4;
或者在您的示例中:
variable == 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")
}
关于javascript - Google 脚本 - 用于多张工作表的一个运行编辑脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910509/