特定单元格包含文本“YES”时的自动日期

时间:2017-12-04 19:31:15

标签: google-apps-script google-sheets

我正在寻求这段代码的帮助:

function onEdit() {

  var s = SpreadsheetApp.getActiveSheet();

  if( s.getName() == "Trip Overview" ) { //checks that we're on the correct sheet

    var r = s.getActiveCell();

    if( r.getColumn() == 12 ) { //checks the column

      var nextCell = r.offset(0, 15);

      //if( nextCell.getValue() !==  ) //is empty?

      nextCell.setValue(new Date());
    }
  }
}

如果第12列包含任何值,1,2,3或“是”,“adf”,“否”等,则代码当前正在添加日期。

如果单元格12包含“是”,如何修改此代码只添加日期?

1 个答案:

答案 0 :(得分:0)

检查您是否在正确的表格和列中后,您需要检查单元格的值是否为“是”

function onEdit() {

  var s = SpreadsheetApp.getActiveSheet();

  if( s.getName() == "Trip Overview" ) { //checks that we're on the correct sheet

    var r = s.getActiveCell();

    if( r.getColumn() == 12 ) { //checks the column

      if(r.getValue() == "YES") { //checksthe value of the cell

        var nextCell = r.offset(0, 15);

        nextCell.setValue(new Date());
      }

    }

  }

}