[英]How to compare time in google sheets scripts?
In google sheets, I am looping through a range of cells, then splitting the part of the string that contains a time. 在Google工作表中,我正在遍历一系列单元格,然后拆分包含时间的字符串部分。 Example:
6:40 AM
is derived from TEST:6:40 AM
using the line: 示例:使用以下行从
TEST:6:40 AM
派生6:40 AM
:
text.slice(text.indexOf(":") + 1, text.length);
I am then trying to see if this time matches the time of the cell next to the cell that originally called the function. 然后,我试图查看此时间是否与最初调用该函数的单元格相邻的单元格时间匹配。
var sheetTime = timeSheetRange.getCell(1,1).getValue();
var time = 0;
for (var i = 1; i <= 30; i++) {
for (var j = 1; j <= 8; j++) {
time = getTime(taskRange.getCell(i,j).getValue());
if (time == sheetTime) {
return time;
}
else {
return "NO ACTIVITY";
}
}
}
The problem is that when putting in the same time values for time
and sheeetTime
, time
does not equal sheetTime
. 问题在于,当为
time
和sheeetTime
输入相同的时间值time
, time
不等于sheetTime
。
Returning both time
and sheetTime
prints out the correct, equal times. 同时返回
time
和sheetTime
打印出正确的相等时间。 I have a feeling that it might have to do with the cell time formatting? 我觉得这可能与单元格时间格式有关? time is reading from this cell:
时间正在从此单元格读取:
While sheetTime is reading from this cell: 当sheetTime从此单元格读取时:
SO what's going on here? 所以这是怎么回事? Is it the formatting that I have to chage or am I approaching this wrong?
是我必须修改的格式还是我要解决这个错误? Thanks.
谢谢。
The issue is that sheetTime
is an actual time value. 问题是
sheetTime
是实际时间值。 Ideally, you'd be comparing time with time, but the easiest way is to use getDisplayValue()
(as suggested by Serge insas in a comment to my original answer). 理想情况下,您应该将时间与时间进行比较,但是最简单的方法是使用
getDisplayValue()
(如Serge insas在对我的原始答案的评论中所建议)。
var sheetTime = timeSheetRange.getCell(1,1).getDisplayValue();
As an overall suggestion, try using Logger.log()
statements to try debugging. 作为总体建议,请尝试使用
Logger.log()
语句尝试调试。 You'll be able to see exactly what values your script is working with. 您将能够准确查看脚本正在使用的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.