单个onEdit函数有效 - 多个相同的函数不

时间:2018-03-09 18:18:57

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

我正在使用Google表格,我有两个功能 onEdit onCommsEdit ,它们运行良好,并为各自的表单添加时间戳。如果我完全复制并粘贴其中一个并给出一个新的函数名称 - 附加功能似乎不起作用。

我订的东西不正确吗?或者我应该在同一个函数中命名所有工作表? (我创建了重复项,因为列的写入因列而异。)

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{name:"Last Row", functionName:"goto_last"}];
  sheet.addMenu("Shortcuts", entries);
   myFunction();
};

function goto_last() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mysheet = ss.getActiveSheet();

  var lastrow = mysheet.getLastRow();

  mysheet.setActiveCell(mysheet.getDataRange().offset(lastrow-1, 0, 1, 1));
}; 

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Primary Log" ) { 
  var r = s.getActiveCell();
  var user = Session.getActiveUser().getEmail();
  if( r.getColumn() != 13 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-08:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange('M' + row.toString()).setValue(time);
    SpreadsheetApp.getActiveSheet().getRange("N" + row.toString()).setValue(user);
  }
  };
 };

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mysheet = ss.getActiveSheet();

  var lastrow = mysheet.getLastRow();

  mysheet.setActiveCell(mysheet.getDataRange().offset(lastrow-1, 0, 1, 1));
}; 

function onCommsEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Received Report" ) { 
  var r = s.getActiveCell();
  var user = Session.getActiveUser().getEmail();
  if( r.getColumn() != 11 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-08:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange('K' + row.toString()).setValue(time);
    SpreadsheetApp.getActiveSheet().getRange("L" + row.toString()).setValue(user);
  }
  };
 };

1 个答案:

答案 0 :(得分:1)

您必须创建a trigger才能自动调用函数。

建议不要使用onEdit条件调用多个函数,因为执行顺序没有很好地定义。我也不建议命名一个需要installed trigger的函数,使其与相应的simple trigger匹配 - 换句话说,一个需要授权范围的函数,并且在修改电子表格值后运行不应该调用onEdit(e),因为这是激活简单触发器的函数名称。如果您的函数不需要授权范围,那么您可以使用简单触发器。

就您的程序设计而言,您应该使用流量控制。而不是多个功能,只需分支即可:

function calledOnEdit(e) {
  if(!e) { throw new Error("This function requires an event object"); }

  // Use the event object rather than lookups that assume the active cell was the edited cell:
  var s = e.range.getSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  if( s.getName() == "Primary Log" && col != 13 ) { //checks the column
    logTimestamp(s, "M" + row + ":N" + row);
  } else if( s.getName() == "Received Report" && col != 11 ) { //checks the column
    logTimestamp(s, "K" + row + ":L" + row);
  }
}
function logTimestamp(sheet, a1ref) {
  var user = Session.getActiveUser().getEmail();
  var time = Utilities.formatDate(new Date(), "GMT-08:00", "yyyy-MM-dd, hh:mm:ss");
  sheet.getRange(a1ref).setValues([[time, user]]);
}