使脚本仅适用于1个特定的Google工作表

时间:2016-09-17 16:37:46

标签: google-sheets

我创建了这个脚本,仅适用于名称为" jan"的工作表。 - 无论我如何尝试,它都可以在所有工作表上运行。谁能帮我检查一下我做错了什么?

function onEdit() { 

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("jan");
    var r = sheet.getActiveCell();
    if( r.getColumn() == 3 ) {
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' )
        var time = new Date();
        time = Utilities.formatDate(time, "GMT", "dd/MM");
        nextCell.setValue(time);
   };
  }; 

2 个答案:

答案 0 :(得分:0)

尝试:

function onEdit() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  if (sheet.getName() == "jan") {
    // DO SOMETHING TO "jan"
    Logger.log("working on sheet jan...");
  }
} 

答案 1 :(得分:0)

将上述内容与您正在做的事情放在一起:

function onEdit(e) { 
var activeSheet = e.source.getActiveSheet();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("jan");
    var r = sheet.getActiveCell();
    if( r.getColumn() == 3 && activeSheet.getName()=="jan") {
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' )
        var time = new Date();
        time = Utilities.formatDate(time, "GMT", "dd/MM");
        nextCell.setValue(time);
   };
  };