通过Google脚本和网络界面执行的Google查询公式可得出不同的结果 [英] Google query formula executed via Google Script and web interface gives different results
问题描述
我需要一个解决方案来解决如何在Google脚本中获取today and tomorrow
的日期的问题.我在请注意,脚本有6个线程,但Web界面为7.
I needed a solution to my question how to get date of today and tomorrow
in Google Script. I got it in Note that the script gets 6 threads but web interface 7.
任何想法该怎么做?调试,报告?
Any idea what to do about it? Debug, report?
function today_tmp() {
const today = new Date();
const today_2 = new Date();
today_2.setDate(new Date().getDate()+1);
const td = Utilities.formatDate(today, 'GMT+2', "yyyy/MM/dd");
const td_2 = Utilities.formatDate(today_2, 'GMT+2', "yyyy/MM/dd");
const mylabel = 'unread';
const queryString = `label: ${mylabel} after: ${td} before: ${td_2}`;
const threads = GmailApp.search(queryString);
//const threads =GmailApp.search(queryString, 0, 10); // this one did not work
Logger.log(queryString);
Logger.log(threads.length);
for (var t in threads) {
Logger.log(threads[t].getMessages()[0].getSubject());
}
}
更新 在项目属性中设置了GMT + 1.我试图更改代码中的GMT + 1,但是它对问题没有任何影响.我住在布拉格时区.
UPDATE GMT+1 is set up in Project Properties. I tried to change GMT+1 in the code but it has no effect for the issue. I live in Prague time zone.
getDate()
表示缺失"消息是Wed Oct 21 00:49:09 GMT+02:00 2020
,今天是10月21日.
getDate()
for the "missing" message is Wed Oct 21 00:49:09 GMT+02:00 2020
and today is Oct 21.
推荐答案
经过反复试验,搜索时GmailApp.search
似乎与时区信息(脚本项目属性与GmailApp
API命令)不一致仅按日期.如果您需要非常具体地捕获基于您的时区的日期边界,则应将该信息作为时间戳值传递.
After some trial and error it seems as if GmailApp.search
isn't consistent with timezone information (Script Project Properties vs. GmailApp
API commands) when searching by dates alone. If you need to be very specific to capture on date boundaries based on your timezone, you should pass that information in as timestamp values.
const mylabel = 'unread';
const morning = new Date();
morning.setHours(0, 0, 0, 0); // Set date to midnight of current day
const night = new Date();
night.setHours(24, 0, 0, 0); // Set end date to future midnight of current day
const queryString = `label: ${mylabel} newer:${morning.getTime()/1000} older:${night.getTime()/1000}`;
const threads = GmailApp.search(queryString);
这篇关于通过Google脚本和网络界面执行的Google查询公式可得出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!