电子表格中特定工作表的 onEdit 触发器? [英] onEdit trigger for specific sheets in a spreadsheet?
问题描述
我无法找到有关如何设置 onEdit 触发器的好资源.我有一个功能,我只想在编辑特定工作表时运行.例如说我有 Sheet1、Sheet2、Sheet3、Sheet4、Sheet5.我的脚本从工作表 2、3、4 中提取数据并填充工作表 1.我只希望在有人编辑工作表 2、3 或 4 时运行我的脚本.我将如何设置此触发器?
I'm having trouble with finding good resources on how to setup onEdit triggers. I have a function that I only want to run when specific sheets are edited. For example say I have Sheet1, Sheet2, Sheet3, Sheet4, Sheet5. My script pulls data from sheets 2, 3, 4 and populates sheet 1. I only want my script ran when someone edits sheets 2, 3, or 4. How would I setup this trigger?
推荐答案
有多种方法可以实现这一点,每种方法都有自己的优点.您对每种情况都有特定的设置吗?您是否为每种情况运行不同的功能?等
There are multiple ways to accomplish this, each having its own merits. Do you have specific setup for each case? Do you run different functions for each case? etc.
- 使用
if
语句检查每个工作表的名称if (name === "name 2" || name === "name 3" || ...) {/* 通用代码 */...
- Use an
if
statement to check the name for each sheetif (name === "name 2" || name === "name 3" || ...) { /* common code */ ...
switch (name) { case "name 2": ...
var names = ["sheet 2 name", "sheet 3 name" ... ];if (names.indexOf(name) { ...
在所有情况下,您都希望从事件对象中获取编辑过的工作表的名称:
In all cases, you will want to grab the edited sheet's name from the event object:
// My simple-trigger function that runs on edit: function onEdit(eventObject) { if (!eventObject) { /* the function was run from the script editor */ return; } var editedSheetName = eventObject.range.getSheet().getName(); /* perform some sort of comparison to figure out if 'editedSheetName' is one we want to work on, and if so, do so. */ ...
您应该查看 Javascript 语言参考(例如 Mozilla Developer Network 上的参考)以熟悉执行流程控制.
You should review a Javascript language reference (such as the one on Mozilla Developer Network) to become familiar with execution flow control.
您可以在 Apps 脚本触发器文档中查看事件对象中可用的属性:https://developers.google.com/apps-script/guides/triggers/events
You can review what properties are available in the event object in the Apps Script trigger documentation: https://developers.google.com/apps-script/guides/triggers/events
这篇关于电子表格中特定工作表的 onEdit 触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!