【问题标题】:Formula-based cell changes in Google Sheets is not firing onEdit scriptGoogle 表格中基于公式的单元格更改未触发 onEdit 脚本
【发布时间】:2016-09-02 01:55:14
【问题描述】:

我对 Google 脚本编写完全陌生,但我在这里使用了各种帖子来拼凑我需要的东西:当某一列发生变化时,它会为一行添加时间戳。这是我目前使用的:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "test" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 16 ) { //checks the column
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) //is empty?
        nextCell.setValue(new Date());
    }
  }
}

当我手动更改数据时,这非常有效;但是,脚本正在监视的列从另一个工作表中提取数据,这无法触发触发器/脚本。我怎样才能解决这个问题,以便带有公式(引用其他工作表)的单元格仍然会触发我的脚本?

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    onEdit trigger 仅在实际用户编辑电子表格时有效。取决于您的用例,但您可以使用 Time-driven trigger 并将其设置为重复间隔,并使用函数监视列的更改,这是一个示例:

    function monitorColumn() {
      // Add the trigger from the Resources menu in the Script Editor
      // Script Editor > Resources > Current oroject's triggers > Add trigger
      // [monitorColumn] - [Time-driven] - [Hour timer] - [Every hour]
    
      var s = SpreadsheetApp.getActiveSpreadsheet();
      var ss = s.getSheetByName("test"); // Get the sheet by name
    
      // Get the values of the columns P & Q with getRange(row, column, numRows, numColumns)
      var columnValues = ss.getRange(1, 16, ss.getLastRow(), 2).getValues();
    
      // Loop through the values
      for (var i = 0; i < columnValues.length; i++) {
    
        // If cell in column P is empty and cell in column Q is not empty set todays date
        if(columnValues[i][0] != "" && columnValues[i][1] == ""){
    
          // While a range index starts at 1, 1, the JavaScript array will be indexed from [0][0].
          ss.getRange(i+1,17).setValue(new Date());
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多