Google Apps 脚本:比较日期

Jos*_*shM 2 google-sheets google-apps-script

我在比较 Google Apps 脚本中的日期时遇到了一个奇怪的问题。

为此,我的工作表在 cell.getValue() 中有一个日期,所以

e.range.setNote(cell.getValue() + " --- " + startDate);
Run Code Online (Sandbox Code Playgroud)

行中,注释显示两个看起来相同的日期,但它们并非如此,因为不满足以下 if 语句。我在比较中遗漏了一些东西吗?谢谢。

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sss = ss.getSheetByName("Sheet1");

  var row = e.range.getRow();
  var startDate = sss.getRange(row, 3).getValue(); //Wed Aug 10 2016 00:00:00 GMT-0600 (GALT)

  var cell = ss.getSheetByName("Sheet2").getRange(1, 1);
  e.range.setNote(cell.getValue() + " --- " + startDate); //Wed Aug 10 2016 00:00:00 GMT-0600 (GALT) --- Wed Aug 10 2016 00:00:00 GMT-0600 (GALT)

  if (cell.getValue() === startDate){
      cell.setBackground('blue');
    }  

}
Run Code Online (Sandbox Code Playgroud)

con*_*rpw 5

我认为你需要比较日期/sf/answers/34511291/

代替

cell.getValue() === startDate
Run Code Online (Sandbox Code Playgroud)

cell.getValue().getTime && startDate.getTime && cell.getValue().getTime() === startDate.getTime()
Run Code Online (Sandbox Code Playgroud)

  • `cell.getValue().getTime` 和 `startDate.getTime` 检查它是否是日期。 (2认同)