组合两个设定值字符串

Combining two setvalue strings

我正在尝试调整我的 inEdit 脚本,使其不仅提供时间戳,还在另一个字段中添加“2”,

我认为以下内容会起作用,但只有日期有效,它会忽略我的“2”。

日期和 2 都出现在 Edit 上我错过了什么?

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

我不知道是否真的需要检查 'nextcell' 是否为空?如果发生新的编辑,您仍然想更新该单元格,对吗?无论如何,我认为您可以将脚本简化为:

function onEdit(e) {
if (e.source.getActiveSheet()
    .getName() !== "Sanshiro" || e.range.columnStart !== 6) return //checks that we're on the correct sheet
e.range.offset(0, -5)
    .setValue(new Date());
e.range.offset(0, 10)
    .setValue(2);
}