简体   繁体   English

将 IndexOf 与日期和谷歌表格一起使用

[英]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.我正在尝试使用 .indexOf() 来查找哪一列包含从谷歌表格上的另一个选项卡中提取的日期。 One sheet is named Main the other is named Data the code is as follows一张名为Main,另一张名为Data,代码如下

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.所有日期都是唯一的,因此应该只返回 1 个值。 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?我通过查看数组知道日期在以下格式中“Sat Nov 09 23:00:00 GMT-08:00 2019”我只对相同的日期感兴趣,其余的可以不确定如何搜索在索引的这一部分?

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.我的想法是以某种方式在正确的格式中获取搜索日期或使用 .map 来更改数组中的日期格式。 Is there a easier way.有没有更简单的方法。

Thanks谢谢

here is a copy of a sample file这是示例文件的副本

https://docs.google.com/spreadsheets/d/15ugbvEJ1-ot0G0EKQcL9SbZhr9KKJ8E2G9CX1nm8CAM/copy 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 .您想要使用indexOfHeaderFields[0]检索与programDate相同的元素索引。
  • You want to know the reason of your current issue of your script.您想知道当前脚本问题的原因。
  • You want to achieve this using Google Apps Script.您希望使用 Google Apps 脚本实现此目的。

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?如果每个值在单元格“B1:P1”的值中只有一个,那么下面的修改怎么样?

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.在您的脚本中,使用了日期对象,因为当getValue()getValues()检索日期时,检索到的值是日期对象。 By this, HeaderFields[0].indexOf(programDate) is always -1 .这样, HeaderFields[0].indexOf(programDate)总是-1

When your script is modified, how about the following modification?当你的脚本被修改时,下面的修改怎么样?

Pattern 1:模式 1:

In this pattern, the date is retrieved as the string by getDisplayValue() and getDisplayValues() .在此模式中,日期作为字符串由getDisplayValue()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:模式 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.在此模式中,日期作为日期对象检索,通过将日期对象转换为 unix 时间或 ISO8601 字符串数据来使用indexOf

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 .在上述修改中,所有结果都是1

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

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

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