javascript - 使用 IF 语句触发 onOpen
问题描述
我有一个脚本,我想在仅打开某些工作表时运行 onOpen,而不是脚本绑定到的电子表格中的其他工作表。所需的工作表都有名称“代理报告 - 'NameOfAgent'”。每个代理报告还包含某些单元格,可用于 IF 条件语句。从逻辑上讲,我想要这个:
如果活动工作表的名称包含“代理报告”-> 运行脚本 ELSE 什么也不做
或者,如果在活动工作表单元格 A2=="Role" -> 运行脚本 ELSE 什么也不做
感谢您的任何帮助。
解决方案
onOpen
将在所有者或编辑者打开电子表格时执行,无论 url 参数中引用了哪个工作表,但您可以if
在 i 上包含一条语句来控制满足条件时执行的操作。
/**
* This will be executed always that the owner or an editor opens the spreadsheet
*
*/
function onOpen(e){
if(SpreadsheetApp.getActiveSheet().getName() === 'Agent report'){
// This will be executen when the condition is true
doSomething();
} else {
// This will be executen when the condition is false
return; // This is actually not necessary as there isn't any statement after it.
}
}
/**
* Function to be called when the condition is met
*
*/
function doSomething(){
// do something
}
有关的
推荐阅读
- mongodb - 如何查找任何字段是否包含特定值
- c# - 在代码中设置 RowDefitions 并覆盖现有的
- liskov-substitution-principle - LSP - Liskov 替换原则特例
- azure-devops - $(Build.BuildId) 未替换 Azure 构建管道中的 terraform tf 文件
- r - R - 在向量中创建特定元素的总和
- r - 在散点图中按名称更改颜色
- mysql - 缓冲池实例未在 MySql 8 中更新
- azure - 参数“storage_connection_string”是必需的,但没有找到定义
- json - 从 api 获取数据并在 react 中重新排列
- sorting - 二进制值的排序网络