简体   繁体   English

每次修改单元格时创建/更改日期戳

[英]Create/change a date stamp every time a cell is modified

I am trying to figure out how to create a date stamps, that get modification anytime a change is done in a cell.我试图弄清楚如何创建一个日期戳,只要在单元格中进行更改就可以进行修改。 I am also very new to Google App Script (everybody needs to start somewhere).我对 Google App Script 也很陌生(每个人都需要从某个地方开始)。

I found the following code on how to create the date stamp:我找到了有关如何创建日期戳的以下代码:

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Test" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 8 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, -1);
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date());
}
}
}

which work for adding the date stamp in the column G, for any data filled in column H.它适用于在 G 列中添加日期戳,用于在 H 列中填写的任何数据。

But if I wanna change the data in H2 for any value, the date in G2 stay the same.但是,如果我想将 H2 中的数据更改为任何值,则 G2 中的日期保持不变。

Anybody could make suggestions please.任何人都可以提出建议。

Your code includes the condition if( nextCell.getValue() === '' )您的代码包含条件if( nextCell.getValue() === '' )

This means that a time stamp will only be set if nextCell (the cell in column G) is empty这意味着仅当nextCell (G 列中的单元格)为空时才会设置时间戳

If you want the timestamp to updated every time a value in column H is changed -remove this condition.如果您希望每次更改 H 列中的值时更新时间戳 - 删除此条件。

So:所以:

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Test" ) { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 8 ) { //checks that the cell being edited is in column A
      var nextCell = r.offset(0, -1);
      nextCell.setValue(new Date());
    }
  }

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

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