【问题标题】:Create/change a date stamp every time a cell is modified每次修改单元格时创建/更改日期戳
【发布时间】:2020-04-26 18:25:35
【问题描述】:

我正在尝试弄清楚如何创建日期戳,以便在单元格中进行更改时随时进行修改。我对 Google App Script 也很陌生(每个人都需要从某个地方开始)。

我找到了以下关于如何创建日期戳的代码:

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());
}
}
}

用于在 G 列中添加日期戳,用于在 H 列中填写的任何数据。

但如果我想将 H2 中的数据更改为任何值,则 G2 中的日期保持不变。

任何人都可以提出建议。

【问题讨论】:

  • 欢迎。也许你应该先阅读developers.google.com/apps-script/guides/sheets
  • 我不明白这个But if I wanna change the data in H2 for any value, the date in G2 stay the same.你能提供一些图片展示你想要的例子吗?

标签: javascript google-apps-script google-sheets timestamp triggers


【解决方案1】:

您的代码包含条件if( nextCell.getValue() === '' )

这意味着仅当nextCell(G 列中的单元格)为空时才会设置时间戳

如果您希望每次更新时间戳,H 列中的值发生更改 - 删除此条件。

所以:

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());
    }
  }

【讨论】:

  • 谢谢@ziganotschka,我尝试了代码,但出现以下错误:“TypeError: Cannot read property 'range' of undefined (ligne 7, fichier "test")”
  • 您好,经过一些额外的测试,它可以工作了。感谢您的帮助
猜你喜欢
  • 2019-06-12
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 2020-11-18
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多