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

时间:2018-03-06 12:07:31

标签: google-apps-script google-sheets triggers

我正在尝试使用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 */

1 个答案:

答案 0 :(得分:1)

不确定什么是'运行时触发器'你的意思是,有三种触发器:

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

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); }

无论您在makeCopy()中使用任何类型的触发器还是手动运行yourMainSpreadsheetonEdit(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);
}

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

documentation突出显示它们之间的区别。

  

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

例如:

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 new spreadsheet to 
         other editors, cause that they own the permission, authorization 
         is not required here.
      */
      var sheet = ss.getSheetByName("MIS");
      sheet.hideColumns(30);
   }
}