javascript - Google 脚本 - 用于多张工作表的一个运行编辑脚本

标签 javascript if-statement google-apps-script google-sheets

我有这个完美运行的脚本 -

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/

相关文章:

javascript - Typescript - 对象的条件属性

javascript - 以编程方式选择 jqGrid 中的所有行?

javascript - 将 JSON 对象转换为数字字符串,反之亦然

java - 我无法导航到我的 Selenium 代码的其他部分,总是失败

linux - 如果命令 "cat/dev/net/tun"结果 $string 那么

javascript - 带有 PASTE_COLUMN_WIDTHS 选项的 Google 脚本复制功能不起作用

javascript - chartjs 在折线图中显示 24 小时

java - 我正在尝试使用 if 语句构建一个基本计算器

google-apps-script - Google Script PropertiesService 在哪里存储数据?

javascript - 无法设置行中单元格的值,因为它已转换为没有方法的对象或数组