简体   繁体   English

如何在Google表格脚本中比较时间?

[英]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 . 问题在于,当为timesheeetTime输入相同的时间值timetime不等于sheetTime

Returning both time and sheetTime prints out the correct, equal times. 同时返回timesheetTime打印出正确的相等时间。 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从此单元格读取时:

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.

相关问题 如何使用脚本在 Google 表格中更新单元格时添加时间戳? - How to time stamp when a cell was updated in Google Sheets using scripts? 如何比较 Google App Scripts 中的字符串 - How to compare strings in Google App Scripts 如何使用 Google Scripts & Google Sheets 执行 Cartesian Join? - How to perform Cartesian Join with Google Scripts & Google Sheets? 通过脚本比较谷歌表中的 A 列。 当匹配正确时,在两侧粘贴偏移值 - compare column A in google sheets by scripts. when match is correct then paste offset value in both side Google Sheets Scripts - 以管理员/所有者身份运行脚本 - Google Sheets Scripts - run scripts as administrator / owner 脚本 - 在Google表格中查找和替换 - Scripts - Find & Replace in Google Sheets Google脚本/表格:如何基于值汇总行/对象? - Google Scripts/Sheets: How to aggregate row/objects based on values? 如何使用变量引用 Google 表格脚本中的特定行? - How to refer to a specific row in Google Sheets Scripts using a variable? Google表格脚本-如何引用给定行的单元格? - Google sheets scripts--how to refer to a cell given a row? 比较 Google 表格中两列数据和 output 只有使用 Google 脚本的不匹配数据的最佳方法是什么? - What's the best way to compare two columns of data in Google Sheets and output ONLY the non-matching using Google Scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM