功能的速度

时间:2016-10-27 18:07:49

标签: google-apps-script

我正在使用多个功能,准确地说是14,与下面的功能类似,由电子表格更改触发:

function principleHours(event){
  var Col = 53; 

  var changedRange = event.source.getActiveRange();
  if (changedRange.getColumn() == Col) {

    var state = changedRange.getValue();
    var adjacent = event.source.getActiveSheet().getRange(changedRange.getRow(),Col);
    var comments = adjacent.getComments();
    var timestamp = Utilities.formatDate(new Date(), "GMT-7", "M/dd/yy', 'h:mm a"); // Get the current time
    // We'll do something different, depending on the selected value
     if (comments == "") {
       comments = "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent(); 
     }
    else if (comments != "") {
     comments = comments + "\n" + "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent();
     } 
  }
}


function principalTravel(event){
  var Col = 66; 

  var changedRange = event.source.getActiveRange();
  if (changedRange.getColumn() == Col) {

    var state = changedRange.getValue();
    var adjacent = event.source.getActiveSheet().getRange(changedRange.getRow(),Col);
    var comments = adjacent.getComments();
    var timestamp = Utilities.formatDate(new Date(), "GMT-7", "M/dd/yy', 'h:mm a"); // Get the current time
    // We'll do something different, depending on the selected value
     if (comments == "") {
       comments = "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent(); 
     }
    else if (comments != "") {
     comments = comments + "\n" + "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent();
     } 
  }
}

将它们组合成一个函数是否有意义?将它们分开是否有优势?

1 个答案:

答案 0 :(得分:0)

我会同意评论者的意见,我很惊讶它没有作为答案提交。

如果可以避免,我建议不要使用“onEdit”触发器调用多个函数。到目前为止,这2个功能似乎或多或少相同。如果你的函数之间唯一的区别是Col变量,那么我建议你做一个小编辑,你有1个函数执行操作并让onEdit调用一个新函数来调用单个函数函数并发送不同的Col变量。这意味着您需要对函数执行的任何调整都将在单个函数上进行。

一般来说,如果它是共享代码库,你应该使用一个函数。所以如果在你的例子中

function foo(Col) {
  var changedRange = event.source.getActiveRange();
    if (changedRange.getColumn() == Col) {

      var state = changedRange.getValue();
      var adjacent = event.source.getActiveSheet().getRange(changedRange.getRow(),Col);
      var comments = adjacent.getComments();
      var timestamp = Utilities.formatDate(new Date(), "GMT-7", "M/dd/yy', 'h:mm a"); // Get the current time
      // We'll do something different, depending on the selected value
       if (comments == "") {
         comments = "(" + timestamp + ") " + state;
         adjacent.setComment(comments);
         changedRange.clearContent(); 
       }
      else if (comments != "") {
       comments = comments + "\n" + "(" + timestamp + ") " + state;
       adjacent.setComment(comments);
       changedRange.clearContent();
      } 
  }
}

是您在所有功能中执行的操作,然后在基本代码中您需要做的就是

function main() {
  foo(53)
  foo(66)
  //as many foo calls as you need
}

尝试将函数用作可以继续重复使用的单个代码,而onEdit将成为main()