检查Google表格/ Google脚本中的日期是否相等

时间:2018-08-30 13:10:50

标签: javascript google-apps-script google-sheets date-comparison

我有2张纸都将日期保存到列中。我当前的日期以相同的代码保存在Sheet1和Sheet2上:

curDate = Utilities.formatDate(new Date(), "EST", "MM/dd/yyyy");

我想让我的一个脚本比较Sheet1和Sheet2的日期

在Sheet1上,我使用了一个小脚本来设置当前日期,然后使用拖动功能使用以下相同的格式设置列中的上一个和下一个日期:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Update Dates')
      .addItem('Set Dates', 'setDatesUp')
      .addToUi();
}

function setDatesUp(){
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Sheet1");

  var curDate = Utilities.formatDate(new Date(), "EST", "MM/dd/yyyy");

  sheet1.getRange("A4").setValue(curDate);
}

一旦设置了Sheet1上的日期,我将使用以下脚本比较Sheet1和Sheet2中的日期,该脚本还将设置Sheet2的日期,因为激活此脚本后,它应该在相应的框中标记当前日期

function onEdit() {
  //Loops through all of Column C to determine which values are True and False
  //Saves all True Values Row # in SavedValues

  //Initialize Variables
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var sheet2 = ss.getSheetByName("Sheet2");

  //Sheet 2 S2
  var cValuesS2 = sheet2.getRange("C:C").getValues();
  var dValuesS2 = sheet2.getRange("D:D").getValues();
  var lastRowS2 = sheet2.getLastRow();

  //Variables
  var curDate;
  var curVar;

  //Loops through all S2 rows, stops at last row
  for (var i = 0; i <= lastRowS2; i++){
    //Checks checkboxes in S2C:C for True OR "Complete" and adds dates in corresponding D:D cells
    if (cValuesS2[i] == "true" || cValuesS2[i] == "Complete") {
      //If D:i is empty
      if (dValuesS2[i] == "") {
        //Sets current date
        curDate = Utilities.formatDate(new Date(), "EST", "MM/dd/yyyy");
        //Set current D:i cell (*+1 offset)
        curVar = ("D" + (i + 1));
        //Sets curVar value to curDate
        sheet2.getRange(curVar).setValue(curDate);
      }
      //Checks checkboxes in S2C:C for False OR "Incomplete" and deletes dates in corresponding D:D cells
    } else if (cValuesS2[i] == "false" || cValuesS2[i] == "Incomplete") {
      //If D:i is NOT empty
      if (dValuesS2[i] != "") {
        //Set current D:i cell (*+1 offset)
        curVar = ("D" + (i + 1));
        //Sets curVar to ""
        sheet2.getRange(curVar).setValue("");
      }
    } 
  }
  updateS1();
}

然后最后我有了脚本,可以将2张纸上的日期进行比较。

function updateS1() {
  //Initialize Variables
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  var sheet1 = ss.getSheetByName("Sheet1");
  var sheet2 = ss.getSheetByName("Sheet2");

  //Sheet 1 S1
  var aValuesS1 = sheet1.getRange("A:A").getValues();
  var lastRowS1 = sheet1.getLastRow();

  //Sheet 2 S2
  var dValuesS2 = sheet2.getRange("D:D").getValues();
  var lastRowS2 = sheet2.getLastRow();

  //Variables
  var curVar;
  var curVar2;

  //Loop through Sheet 1 until the bottom
  //For each value in S1 A I use i
  for (var i = 0; i <= lastRowS1; i++) {
    //Loop through Sheet 2 until the bottom
    //For each value in S2 D I use j
    for (var j = 0; j <= lastRowS2; j++) {
        //TODO: Compare dates from S1 A:i to S2 D:j
        //If they are the same date the if statement will execute
    }
  }
}

我已经尝试使用以下内容

if (aValuesS1[i].toString() == dValuesS2[j].toString()) {
}

if (aValuesS1[i] == dValuesS2[j]) {
}

,但两个选项均无效。我注意到,当我在记录器中获取日期的“值”时,会得到很多我不需要或不需要的信息:

  

2018年10月30日星期四00:00:00 GMT-0400(EDT)

而不是8/30/18。
我认为这是即使我的两个框都显示相同格式的日期也无法获得“匹配”值的原因。老实说,我很困惑如何解决此问题,因此,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

事实证明,实际上我可以将if语句与.toString()进行比较,我的循环在Google Apps脚本中执行需要花费相当长的时间。