简体   繁体   English

删除或更新一定范围的单元格时,Google表格时间戳记脚本未更新

[英]google sheets timestamp script not updating when deleting or updating a range of cells

This is my first time posting so I hope I've done everything right :) 这是我的第一次发贴,所以我希望我做对了一切:)

I've implemented a script in a google sheets that creates a timestamp in column B, when a cell is edited on that row. 我已经在Google工作表中实现了一个脚本,该脚本在该行中的单元格进行编辑时会在B列中创建一个时间戳。

It works perfectly, except: 它工作完美,除了:

  1. when I select a range of cells and press "delete", only one of the timestamps updates (the timestamp of the active cell), even though I've modified multiple cells 当我选择一个单元格范围并按“删除”时,即使已修改了多个单元格,也只有一个时间戳更新(活动单元的时间戳)
  2. If I drag downwards to auto-fill values from one cell down into multiple cells (eg, if I drag from A1 down to A5, to fill A2-A5 with the value that was in A1), then only the first timestamp (eg, A2) updates, and the rest stay as they were. 如果我向下拖动以将值从一个单元格自动填充到多个单元格中(例如,如果我从A1向下拖动到A5,以使用A1中的值填充A2-A5),则仅使用第一个时间戳(例如, A2)更新,其余部分保持原样。

Any ideas how to fix these issues? 任何想法如何解决这些问题?

Here's my code 这是我的代码

 function onEdit(e) { var s = SpreadsheetApp.getActiveSheet(); var r = s.getActiveCell(); if( r.getColumn() != 2 && e.source.getActiveSheet().getName() === "MySheet") { //checks the column & checks the sheet is the correct one var row = r.getRow(); var time = new Date(); time = Utilities.formatDate(time, "GMT+10:00", "dd/MM/yyyy HH:mm:ss"); SpreadsheetApp.getActiveSheet().getRange('B' + row.toString()).setValue(time); }; }; 

And here's an example spreadsheet that has the code: 这是一个包含代码的电子表格示例:

https://docs.google.com/a/eventsafetyservices.com.au/spreadsheets/d/1hFtS-diq_8QsWwqFTIlHg8zHxpvVUsDZex0nSuvXizg/edit?usp=sharing https://docs.google.com/a/eventsafetyservices.com.au/spreadsheets/d/1hFtS-diq_8QsWwqFTIlHg8zHxpvVUsDZex0nSuvXizg/edit?usp=sharing

It's generally good practice to use the event object for everything related to that action. 通常,将事件对象用于与该操作相关的所有事情都是一种好习惯。

Handling multiple rows can be handled by using the range of the event a bit further to get the extent of it. 可以通过进一步使用事件的范围来处理多行,以获取事件的范围。

function onEdit(e) {
  var r = e.range;

  if(r.getColumn() !== 2 && r.getSheet().getName() === "MySheet") {
    var row = r.getRow();
    var lastRow = r.getLastRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT+10:00", "dd/MM/yyyy HH:mm:ss");

    r.getSheet().getRange(row, 2, lastRow - row + 1, 1).setValue(time);
  };
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM