0

I'm trying to compare a user specified date against a date in a databank on another sheet. The script should return the specified columns of any row with the matching date. Currently, it returns all of the data. It seems like the IF statement is always reading as true, regardless of the values. The logs show the values are different. Why might this IF statment always be returning true, and thus returning all databank rows? Code Below:

function search() 
{

//clears contents in search sheet (sheet1)
var app=SpreadsheetApp;
var WorkSheet =  app.getActiveSpreadsheet().getSheetByName("Sheet1");
WorkSheet.getRange("A2:D50").clearContent();

//store date search value - getrange x and get it's value
var DateSearch = WorkSheet.getRange(1,2).getValue();
//view variable for testing
Logger.log(DateSearch);

//data sheet lastrow variable
var dataSheet = app.getActiveSpreadsheet().getSheetByName("Form Responses 
1");
var a = dataSheet.getDataRange().getNumRows();
//view variable for testing
Logger.log(a);

//var i is looper
var i;
var dataI;
var copyValue;
//view if statement value for testing

for(i=2;i<=a;i++)
{
Logger.log(i);
dataI = dataSheet.getRange(i,1).getValue();
Logger.log(dataI); 
Logger.log(DateSearch);

if (dataI = DateSearch)
{
  // returns values in variable row and first 2 columns if condition is met
  copyValue = dataSheet.getRange(i, 1, 1, 2).getValues();
  //test copyValue
  Logger.log(copyValue);
  WorkSheet.getRange(i,1,1,2).setValues(copyValue);
    }
  }
}
Shawn
  • 45
  • 1
  • 11
  • 2
    In your script, ``dataI = DateSearch`` is used at ``if (dataI = DateSearch)``. So when ``DateSearch`` has a value, it always works as true. By this, the script when ``if`` is true works every time. I think that the reason of your issue is this. In order to avoid this, please modify to ``if (dataI == DateSearch)`` and try it again. Even if this modification was done, if the same issue occurs, can you provide your sample spreadsheet? I would like to confirm about the situation. – Tanaike Aug 25 '18 at 06:23
  • @tan Dates need to be compared numerically. – tehhowch Aug 25 '18 at 11:04
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – tehhowch Aug 25 '18 at 11:04
  • @Tanaike Here's a link to the sheet. I tried ==, but to no avail https://docs.google.com/spreadsheets/d/1SATW91kVpzgoLpCsvemkXxGB5nr6_72XfH0PIZfMaJg/edit?usp=sharing – Shawn Aug 25 '18 at 21:51
  • @tehhowch Thank you for your comment. Yes. I think that you are correct. I posted the modified script as an answer. – Tanaike Aug 25 '18 at 23:34
  • @Shawn Thank you for providing the sample Spreadsheet. And I'm really sorry for the inconvenience. I posted the modified script as an answer. Could you please confirm it? – Tanaike Aug 25 '18 at 23:35

1 Answers1

0

How about this modification?

Modification points:

  • In the case of if (dataI = DateSearch), when DateSearch has a value, it always works as true. By this, the script when if is true works every time.
  • When 2 date values are compared, please compare them by converting to number.
    • This is mentioned from @tehhowch

Modified script:

In order to reflect above points, please modify as follows and try to run again. The former and latter compare the unix time and each year, month and day, respectively. You can choose one of 2 samples.

From:
if (dataI = DateSearch)
To:
if (new Date(dataI).getTime() === new Date(DateSearch).getTime())

Or

To:
if (new Date(dataI).getFullYear() === new Date(DateSearch).getFullYear() &&
    new Date(dataI).getMonth() === new Date(DateSearch).getMonth() &&
    new Date(dataI).getDate() === new Date(DateSearch).getDate())

Other modification:

By the way, from your script and sample spreadsheet, it is considered that when the number of data becomes large, the process time will become long. So as the other modification for this situation, how about this? But I'm not sure whether this is useful for your situation. So please think of this as a sample script.

function search() {
  var app=SpreadsheetApp;
  var WorkSheet =  app.getActiveSpreadsheet().getSheetByName("Sheet1");
  WorkSheet.getRange("A2:D50").clearContent();
  var DateSearch = WorkSheet.getRange(1,2).getValue();
  var dataSheet = app.getActiveSpreadsheet().getSheetByName("Form Responses 1");

// Below script was modified.
  var dataI = dataSheet.getDataRange().offset(1, 0).getValues();
  var copyValue = dataI.map(function(e, i) {
    return new Date(e[0]).getTime() === new Date(DateSearch).getTime() ? e : ["", ""];
  });
  WorkSheet.getRange(2,1,copyValue.length,copyValue[0].length).setValues(copyValue);
}

References :

If this result was not what you want, please tell me. I would like to modify it.

Added:

This modification script puts the retrieved values without the empty rows to the row 2.

function search() {
  var app=SpreadsheetApp;
  var WorkSheet =  app.getActiveSpreadsheet().getSheetByName("Sheet1");
  WorkSheet.getRange("A2:D50").clearContent();
  var DateSearch = WorkSheet.getRange(1,2).getValue();
  var dataSheet = app.getActiveSpreadsheet().getSheetByName("Form Responses 1");

// Below script was modified.
  var dataI = dataSheet.getDataRange().offset(1, 0).getValues();
  var copyValue = dataI.filter(function(e) { // Modified
    return new Date(e[0]).getTime() === new Date(DateSearch).getTime(); // Modified
  });
  WorkSheet.getRange(2,1,copyValue.length,copyValue[0].length).setValues(copyValue);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Your faster solution is definitely what I need, because datasets will be large. However, I need to get it to paste into the first available row in Sheet1, What it does now is leave blanks where data doesn't match, presumably because the setValue is following "i". I thought that variable "a" I had might work because it's storing the last row from earlier, but that doesn't work for me either. Any idea how to get it to print out a seamless dataset without the breaks? I'm marking your solution as correct, because you did actually solve the initial problem that I had. – Shawn Aug 26 '18 at 05:22
  • @Shawn Thank you for your reply. I added a script for your reply. Could you please confirm it? If that is not what you want, in order to understand the result you want, can you provide the spreadsheet including input and output you want? Using this, I would like to modify the script. – Tanaike Aug 26 '18 at 07:48