简体   繁体   中英

Using IndexOf with dates and google Sheets

I am trying to use.indexOf() to find which column contains a date pulled form another tab on google sheets. One sheet is named Main the other is named Data the code is as follows

function FindDate() {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var ssProgram = ss.getSheetByName("Main");
     var range = ssProgram.getRange(2,3); 
     var programDate = range.getValue();

     var ssUpcoming = ss.getSheetByName("Data");
     var dateRange = ssUpcoming.getRange(1, 1, 1, ssUpcoming.getLastColumn());
     var HeaderFields = dateRange.getValues();

     Logger.log(HeaderFields[0]);
     Logger.log(HeaderFields[0].indexOf(programDate));


  Browser.msgBox('Greetings', "Done", Browser.Buttons.OK);

}

All the dates are unique so should only return 1 value. I know by looking at the array the dates are in the following formate "Sat Nov 09 23:00:00 GMT-08:00 2019" I am only interested in the date being the same the rest can be whatever not sure how to search on this one part of the index?

my ideas are to somehow get the search date in the right formate or to use.map to change the date formate in the array. Is there a easier way.

Thanks

here is a copy of a sample file

https://docs.google.com/spreadsheets/d/15ugbvEJ1-ot0G0EKQcL9SbZhr9KKJ8E2G9CX1nm8CAM/copy

  • You want to retrieve the index of element, which is the same with programDate , from HeaderFields[0] using indexOf .
  • You want to know the reason of your current issue of your script.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

If each value is only one in the values of the cells "B1:P1", how about the following modification?

Modification point:

In your script, the date object is used, because when the date is retrieved by getValue() and getValues() , the retrieved values are the date object. By this, HeaderFields[0].indexOf(programDate) is always -1 .

When your script is modified, how about the following modification?

Pattern 1:

In this pattern, the date is retrieved as the string by getDisplayValue() and getDisplayValues() .

Modified script:

From:
 var programDate = range.getValue();
To:
 var programDate = range.getDisplayValue();

and

From:
Logger.log(HeaderFields[0].map(function(e) {return e instanceof Date ? e.getTime() : e}).indexOf(programDate.getTime()));
To:
Logger.log(HeaderFields[0].map(function(e) {return e instanceof Date ? e.toISOString() : e}).indexOf(programDate.toISOString()));

Pattern 2:

In this pattern, the date is retrieved as the date object, and indexOf is used by converting the date object to the unix time or the ISO8601 string data.

Modified script:

From:
 Logger.log(HeaderFields[0].indexOf(programDate));
To:
 Logger.log(HeaderFields[0].map(function(e) {return e instanceof Date? e.getTime(): e}).indexOf(programDate.getTime()));

or

From:
 Logger.log(HeaderFields[0].indexOf(programDate));
To:
 Logger.log(HeaderFields[0].map(function(e) {return e instanceof Date? e.toISOString(): e}).indexOf(programDate.toISOString()));

Note:

  • At above modifications, all results are 1 .

References:

If I misunderstood your question and this was not the direction you want, I apologize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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