【问题标题】:Insert "LastUpdate Date" when some specific columns are modified (Help in the script)修改某些特定列时插入“LastUpdate Date”(脚本中的帮助)
【发布时间】:2013-06-09 23:59:54
【问题描述】:

情况:

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

脚本功能:

当有人修改任何列中的任何行时,此脚本将使用 DateTime 更新最后一列,并插入与进行该修改的用户相关的评论。

问题:

(1) 性能问题:该脚本在用户修改任何列时运行。这可能是因为当我有更多 > 3 个用户登录电子表格时,会慢慢转动以保存。
(2) 只有在修改某些特定的列时才应运行此脚本。对于 ej.:如果活动用户修改列 A、B、C;D、E 和 I,则脚本将最后一列 J 更新为日期和时间;但是如果 activeuser 修改了 F,G,H 列,则不应运行该脚本。

测试用例:

(1) 这个脚本在 OnEdit 上运行得很好,但是当有人修改任何列中的任何行时,它会更新 lastcoulmn。

如果有人可以帮助我修改此脚本,以便仅在修改特定列时运行,我将不胜感激。

脚本:

    function onEdit(event)
    {
      var ss = SpreadsheetApp.getActiveSpreadsheet();

    //Script Last Update Timming

      var actSht = event.source.getActiveSheet();
      var actRng = event.source.getActiveRange();

      var index = actRng.getRowIndex();
      var dateCol = actSht.getLastColumn();
      var lastCell = actSht.getRange(index,dateCol);
      var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");

      // Note: Insert the Date when someone update the row in the last coulmn
        lastCell.setValue(date);

      // Nota: Insert a comment in the lastCell with the user who modify that row
        lastCell.setComment(Session.getEffectiveUser());
    }

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您可以使用以下代码检查活动行和活动列,然后根据识别的行、列,您可以继续进行。

    var activeCell = sheet.getActiveCell();
    var row = activeCell.getRow();
    var column = activeCell.getColumn();
    

    您的整体代码将如下所示。

    function onEdit(event){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    
      //Script Last Update Timming
    
      var actSht = event.source.getActiveSheet();
      var actRng = event.source.getActiveRange();
    
      var activeCell = actSht.getActiveCell();
      var row = activeCell.getRow();
      var column = activeCell.getColumn();
    
      if(row < 2)   return; //If header row then return
      var colNums  = [1, 5, 6, 7]; //Coulmns, whose edit is considered
      if(colNums.indexOf(column) == -1) return; //If column other than considered then return
    
    
      var index = actRng.getRowIndex();
      var dateCol = actSht.getLastColumn();
      var lastCell = actSht.getRange(index,dateCol);
      var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm");
    
      // Note: Insert the Date when someone update the row in the last coulmn
      lastCell.setValue(date);
    
      // Nota: Insert a comment in the lastCell with the user who modify that row
      lastCell.setComment(Session.getEffectiveUser());
    }
    

    【讨论】:

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