简体   繁体   English

为什么Google表格中的日期验证会拒绝今天上午8:00之前的日期?

[英]Why date validation in Google sheets rejects today's date before 8:00am?

I've created a Google sheet to keep a list of work tasks with a column to track the date on which items are created, and built a script to automatically populate the cells in that column with the day's date when a new line is inserted. 我创建了一个Google工作表来保留一个工作任务列表,其中包含一列以跟踪创建项目的日期,并创建了一个脚本,用于在插入新行时使用当天的日期自动填充该列中的单元格。

The cell (eg G9) that is target of the script uses the following validation formula to make sure that when users change the date, they use a date that is neither a weekend nor in the future: 作为脚本目标的单元格(例如G9​​)使用以下验证公式来确保用户更改日期时,他们使用的日期既不是周末也不是将来的日期:

=and(isdate(G9), weekday(G9,2)<6, G9<=today())

IT ONLY WORKS BUT ONLY IF THE SCRIPT IS RUN ANYTIME AFTER 8:00am ! 仅当脚本在8:00 am之后任何时间运行时,它才起作用! If I try using it any earlier the cell validation will reject the input! 如果我尝试更早使用它,单元格验证将拒绝输入!

The script looks like this (curRow is the number of the row that's been added): 该脚本如下所示(curRow是已添加的行号):

 // Adds today's date without using =today()
  var myrangename = "G"+curRow;
  var dateCell = sheet.getRange(myrangename);
  var d = new Date();
  var dateArr = [];
  dateArr[0]=d.getFullYear();
  dateArr[1]=d.getMonth() + 1; //Months are zero based
  dateArr[2]=d.getDate();
  dateCell.setValue(dateArr.join('/'));

(nb: I cannot use the script to simply put =today() in the cell because all the entries would change every day. ) (nb:我不能使用脚本简单地将=today()放在单元格中,因为所有条目每天都会更改。

WHY DOES IT ONLY WORK AFTER 8:00AM? 为什么仅在8:00 AM之后工作? Is Google somehow running on a different time zone than my computer?? Google是否在与我的计算机不同的时区上运行? I'm based in the UK, so using BST , but that shouldn't be a problem, shouldn't it...? 我住在英国,所以使用BST ,但这应该不是问题,不是吗?

Try 尝试

var d = new Date();
var d = Utilities.formatDate(d, "GMT+1", "yyyy-MM-dd HH:mm:ss");

I am not sure if google would recognise BST as a time zone, but you could also try 我不确定Google是否会将BST识别为时区,但您也可以尝试

var d = Utilities.formatDate(d, "BST", "yyyy-MM-dd HH:mm:ss");

Thank you for your suggestion, Aprillion. 谢谢您的建议,Aprillion。 Turns out that a Google Sheets file has its own internal time-zone setting! 原来, Google表格文件具有自己的内部时区设置! which in my case was set to American Pacific time (so 8hrs behind) 在我的情况下,该时间设置为美国太平洋时间(晚8小时)
(You'd think it would pick up the date and time info automatically from Windows, like other applications do!) (您认为它会像其他应用程序一样从Windows自动获取日期和时间信息!)
To set the sheet's time-zone to the correct one, you need to go to the main menu, click 'File', then 'Spreadsheet settings...', and adjust as necessary. 要将工作表的时区设置为正确的时区,您需要进入主菜单,依次单击“文件”,“电子表格设置...”,并根据需要进行调整。
The script and validation now all work fine. 现在脚本和验证都可以正常工作。
Thank you all for your help. 谢谢大家的帮助。

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

相关问题 PHP服务器端对将来日期与今天日期的比较进行验证 - PHP server side validation of future date in comparison with today's date 带有弹出警报的谷歌表格中的自定义日期验证 - Custom date validation in google sheets with popup alert 检查日期验证是否大于今天的日期以及js中的日期格式 - check date validation with greater than today's date along with date format in js javascript日期验证不适用于今天的日期 - javascript date validation is not working for today date Laravel after_or_equal 验证返回今天之前日期的错误状态代码 - Laravel after_or_equal validation returns wrong status code for date before today 数据验证中的Date的Vba代码 - Vba code for Date today in data validation 检查所选日期是今天的日期还是 vuejs 验证中的未来日期 - Check if the selected date is today's date or a future date in vuejs validations Google AppMaker日期验证 - Google AppMaker date validation 使用CompareValidator控件将用户输入日期与今天的日期进行比较 - Using the CompareValidator control to compare user input date with today's date 将输入类型日期最小值设置为今天并给出验证错误 - Setting Input type date Minimum to Today and give validation error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM