2

我有以下脚本

情况:

我有一个包含 10 个工作表和 15 个用户登录并修改它的电子表格。

脚本功能:

在特定列的活动表中插入两天之间的天数,脚本比较来自列 J 和 TodayDay 的日期,并在列 F 中插入差异。

问题:

我无法使用定时触发器运行此脚本。我想为并发工作表每 15 分钟运行一次此脚本。

测试用例:

如果我在电子表格中只有一张工作表,触发器只会随时间运行。如果我有不止一张纸,触发器不会随时间运行。如果我将相同的触发器设置为 OnEdit,则触发器只会运行多张纸。

我需要每 15 分钟为所有工作表或活动工作表运行此脚本,因为我还有另外两个运行 OnEdit 的脚本。当我也在 OnEdit 中设置此脚本时,电子表格会慢慢转动。

    function onOpen() {
      var menuEntries = [ {name: "UpdateAge", functionName: "toTrigger"},
                         ];
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      ss.addMenu("Tools",menuEntries);//
    }

    // create a timer trigger that will call "toTrigger" every 15 minutes

    function toTrigger(){
      var sh = SpreadsheetApp.getActiveSheet();
      var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
      for(var n=0;n<data.length;++n){
      if(typeof(data[n][9])=='object'){
        data[n][5]=dayToToday(data[n][9])
          }
        }
       sh.getRange(1,1,data.length,data[0].length).setValues(data) 
    }

    function dayToToday(x){
      var refcell = x;;// get value in column A to get the reference date
      var refTime = new Date(refcell);
      var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
      var today = new Date();
      var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
      var day = parseInt(TD-ref);// get the difference in days (integer value )
      return day ; // return result that will be in cell
    }

编辑:工作

嗨,斯里克,

非常感谢现在正在工作。

    function onOpen() {
      var menuEntries = [ {name: "UpdateAge", functionName: "shellFunction"},
                         ];
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      ss.addMenu("Tools",menuEntries);//
    }

        function shellFunction(){
        var sheets = ['Sheet1','Sheet2','Sheet3','Sheet4']; 
        for (var s in sheets){
      toTrigger(sheets[s]);
        }
      }

      // create a timer trigger that will call "toTrigger" every 15 minutes

      function toTrigger(sheetName){
      var ss = SpreadsheetApp.openById('SHEET ID');
      var sh = ss.getSheetByName(sheetName);
      var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
      for(var n=0;n<data.length;++n){
      if(typeof(data[n][9])=='object'){
        data[n][5]=dayToToday(data[n][9])
          }
        }
       sh.getRange(1,1,data.length,data[0].length).setValues(data) 
    }

    function dayToToday(x){
      var refcell = x;;// get value in column A to get the reference date
      var refTime = new Date(refcell);
      var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
      var today = new Date();
      var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
      var day = parseInt(TD-ref);// get the difference in days (integer value )
      return day ; // return result that will be in cell
    }
4

1 回答 1

3

当您的函数作为触发器运行时,没有活动工作表。活动工作表仅在用户打开电子表格时适用。修改您的脚本以使用getSheetByName,一切都应该正常工作。

如果您希望它在多张纸上工作,只需从另一个函数调用此函数..

function shellFunction(){
  var sheets = ['sheet1','sheet2',etc]; 
  for (var s in sheets){
    toTrigger(sheets[s]);
  }
}

并更改您的toTrigger函数以接收工作表名称作为参数。

function toTrigger(sheetName){
  var sh = SpreadsheetApp.openById(ID OF SPREADSHEET).getSheetByName(sheetName);
  /* Your regular code */
}
于 2013-06-08T00:46:19.877 回答