繁体   English   中英

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

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

我有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。
我认为这是即使我的两个框都显示相同格式的日期也无法获得“匹配”值的原因。 老实说,我很困惑如何解决此问题,因此,我们将不胜感激。

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

暂无
暂无

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

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