以编程方式在 Google 表格中的脚本中设置触发器



我正在尝试让Google Apps脚本在编辑特定单元格时触发。现在,我已经使用Google表格中内置的运行时触发器选项实现了此目的。但是,当我制作副本时,我丢失了运行时触发器。 因此,我设法从各种来源找到了一个可编程触发器并将一些东西修补在一起,但它不起作用。我没有编程知识,所以我无法理解我哪里出错了。

我的目标是在用户编辑命名范围"monthfilter"时运行脚本。

使用脚本触发器创建

function HideColumns() {
//open the current spreadsheet and get the value of the named range and trigger project
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("monthfilter").forSpreadsheet(ss).onedit() 
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();

我以前的功能:

function HideColumns() {
//open the current spreadsheet and get the value of the named range
var ss = SpreadsheetApp.getActive();
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();
//show or hide February
if (namevalue == "February") {
var sheet = ss.getSheetByName("MIS");
sheet.hideColumns(30);
/** other stuff */

不确定您的"运行时触发器"是什么,有三种触发器:

  • 简单触发
  • 可安装触发器
  • 时间驱动触发器

onEdit(e)只是一个简单的触发器。

让我们看一下以下情况:

yourMainSpreadsheet -   (You are the owner)
|
binding script functions: - 1. function makeCopy( ) {...}
2. function monthFilter(e) {...}
3. function onEdit(e) { monthFilter(e); }

|
|  After copying, new spreadsheet remains all functions and simple trigger.
V

newSpreadsheet-   (You are the owner, maybe share with other editors.)
|
binding script functions: - 1. function makeCopy() {...}
2. function monthFilter(e) {...}
3. function onEdit(e) { monthFilter(e); }

无论您使用哪种触发器或只是在yourMainSpreadsheet中手动运行makeCopy()onEdit(e)也会复制到新的电子表格中,并且当您或其他编辑者进行编辑时它可以工作,您无需执行任何操作:

function makeCopy(){
var mainSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSpreadsheet = mainSpreadsheet.copy('newSheet');
}

function monthFilter(e) {
var thisSheet = e.source.getActiveSheet();
var name = thisSheet.getRangeByName("rangeMonthfilter");
var nameValue = name.getValue();
//...
}
function onEdit(e) {
monthFilter(e);
}

您所做的是为新电子表格设置可安装的触发器。

有关它们之间差异的文档突出显示。

然而,可安装的触发器提供了比简单更大的灵活性 触发器:他们可以调用需要授权的服务...

例如:

function monthFilter(e) {
var thisSheet = e.source.getActiveSheet();
var name = thisSheet.getRangeByName("rangeMonthfilter");
var nameValue = name.getValue();
// If you have operation like:
var onlyYouHavePermission = SpreadsheetApp.openById('xxxxxxxxx');
/* This is not allowed if you just use Simpler trigger onEdit(e) to 
drive monthFilter(). */
}

为此,现在是使用可安装触发器的时候了:

function makeCopy(){
var mainSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSpreadsheet = mainSpreadsheet.copy('newSheet');

ScriptApp.newTrigger('monthFilter')
.forSpreadsheet(newSpreadsheet)
.onEdit()
.create();
/* The host of this trigger is you, it's like you 
deploy a trigger to the newSpreadsheet, and make other editors 
available to do something on "onlyYouHavePermission" through it.*/
}

更新

如果你只想用你提供HideColumns(),你只需要添加一个新函数,这是一个在脚本编辑器中更简单的触发器:

function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();
if (namevalue == "February") {
/* If you copy the spreadsheet, of course including the sheets 
inside it, and you share the permission of the new spreadsheet to 
other editors, because they own the permission, authorization 
is not required here.
*/
var sheet = ss.getSheetByName("MIS");
sheet.hideColumns(30);
}
} 

最新更新