[英]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
programDate
, from HeaderFields[0]
using indexOf
.indexOf
从HeaderFields[0]
检索与programDate
相同的元素索引。 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”的值中只有一个,那么下面的修改怎么样?
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?当你的脚本被修改时,下面的修改怎么样?
In this pattern, the date is retrieved as the string by getDisplayValue()
and getDisplayValues()
.在此模式中,日期作为字符串由
getDisplayValue()
和getDisplayValues()
检索。
var programDate = range.getValue();
To:
var programDate = range.getDisplayValue();
and和
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()));
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
。
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要么
Logger.log(HeaderFields[0].indexOf(programDate));
To:
Logger.log(HeaderFields[0].map(function(e) {return e instanceof Date? e.toISOString(): e}).indexOf(programDate.toISOString()));
1
.1
。 If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.