简体   繁体   English

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

[英]Checking if date equal in Google Sheets/Google Scripts

I have 2 sheets that both have dates saved to columns. 我有2张纸都保存有日期的列。 My current date is saved on both Sheet1 and Sheet2 with the same code: 我当前的日期使用相同的代码保存在Sheet1和Sheet2上:

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

I want to have one of my scripts compare the dates from Sheet1 to Sheet2 我想让我的一个脚本比较Sheet1和Sheet2的日期

On Sheet1 I used a small script to set the current date and then used the drag function to set the previous and next dates in the column using the same formatting here: 在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);
}

Once the dates on Sheet1 are set up I use the following script to compare the dates from Sheet1 and Sheet2, this script also sets the date for Sheet2 because when this script is activated it's supposed to mark the current date in the corresponding box. 设置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();
}

Then finally I have my script to compare the dates from the 2 Sheets together. 最后,我有了我的脚本来比较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
    }
  }
}

I've already tried using the following 我已经尝试使用以下内容

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

and

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

but neither option has worked. 但两种选择均无效。 I've noticed that when I grab the "value" for the date in the Logger I get a lot of information that I don't want or need: 我注意到,当我在记录器中获取日期的“值”时,会得到很多我不需要或不需要的信息:

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

instead of 8/30/18. 而不是8/30/18。
I think that this is the reason that I do not get "matching" values even if both of my boxes show the date formatted the same. 我认为这是即使我的两个框都显示相同格式的日期也无法获得“匹配”值的原因。 I'm honestly stumped at how to solve this, so any help would be greatly appreciated. 老实说,我很困惑如何解决此问题,因此,我们将不胜感激。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM