如何仅获取今天的电子邮件-Gmail + Google脚本 [英] how to get only today emails - Gmail + Google Script
问题描述
在我的Google脚本中,我只想解析今天起带有特定标签的电子邮件.
- 使用Gmail.Users.Messages.list()的解决方案
我发现可能的解决方案是使用搜索查询.今天是 20.10.2020
,此搜索查询 after:2020/10/20 before:2020/10/22
仅返回今天的电子邮件.如果使用此解决方案,我将不知道如何将正确的日期传递给查询.
- 使用GmailApp.getUserLabelByName()的解决方案
我宁愿使用 GmailApp.getUserLabelByName()
,而不是 Gmail.Users.Messages.list()
,这样我可以使用线程而不是消息.我很了解这两种方法的工作原理.
说明:
-
然后,您可以使用模板文字构造查询参数,并将所有日期和标签变量传递给字符串对象.
-
getSpreadsheetTimeZone()用于获取电子表格的时区.您可以将其替换为实际的
GMT
,例如:const td = Utilities.formatDate(今天,"GMT + 1","yyyy/MM/dd");const td_2 = Utilities.formatDate(today_2,'GMT + 1',"yyyy/MM/dd");
使用电子表格的时区的解决方案:
function myFunction(){const ss = SpreadsheetApp.getActive();const today = new Date();const today_2 =新的Date();today_2.setDate(new Date().getDate()+ 2);const td = Utilities.formatDate(today,ss.getSpreadsheetTimeZone(),"yyyy/MM/dd");const td_2 = Utilities.formatDate(today_2,ss.getSpreadsheetTimeZone(),"yyyy/MM/dd");const mylabel ='未读';const queryString =`label:$ {mylabel}之后:$ {td}之前:$ {td_2}`;const线程= GmailApp.search(queryString);}
自定义时区的解决方案:
将 GMT + 1
调整为您自己/所需的时区.
function myFunction(){const today = new Date();const today_2 =新的Date();today_2.setDate(new Date().getDate()+ 2);const td = Utilities.formatDate(today,'GMT + 1',"yyyy/MM/dd");const td_2 = Utilities.formatDate(today_2,'GMT + 1',"yyyy/MM/dd");const mylabel ='未读';const queryString =`label:$ {mylabel}之后:$ {td}之前:$ {td_2}`;Logger.log(queryString);//检查视图"中的输出->日志const线程= GmailApp.search(queryString);//GmailThread []-与此查询匹配的Gmail线程数组}
In my Google Script I want to parse only email from today + with a certain label.
- solution using Gmail.Users.Messages.list()
I found out that possible solution is to use search query. Today is 20.10.2020
and this search query after:2020/10/20 before:2020/10/22
returns only emails from today. If I use this solution the I do not know how to pass the right dates to the query.
- solution using GmailApp.getUserLabelByName()
I would prefer to use GmailApp.getUserLabelByName()
not Gmail.Users.Messages.list()
so I can work with threads not messages. I understand correcly how the two method work.
Explanation:
As also Cooper suggested, you can construct the dates and convert them to the desired format by using the Utilities class.
Then, you can use Template literals to construct the query argument and pass all the date and label variables to the string object.
getSpreadsheetTimeZone() is used to get the time zone of the spreadsheet. You can replace that with your actual
GMT
, for example:const td = Utilities.formatDate(today, 'GMT+1', "yyyy/MM/dd"); const td_2 = Utilities.formatDate(today_2, 'GMT+1', "yyyy/MM/dd");
Solution that uses the time zone of the spreadsheet:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const today = new Date();
const today_2 = new Date();
today_2.setDate(new Date().getDate()+2);
const td = Utilities.formatDate(today, ss.getSpreadsheetTimeZone(), "yyyy/MM/dd");
const td_2 = Utilities.formatDate(today_2, ss.getSpreadsheetTimeZone(), "yyyy/MM/dd");
const mylabel = 'unread';
const queryString = `label: ${mylabel} after: ${td} before: ${td_2}`;
const threads = GmailApp.search(queryString);
}
Solution with custom timezone:
Adjust GMT+1
to your own/desired timezone.
function myFunction() {
const today = new Date();
const today_2 = new Date();
today_2.setDate(new Date().getDate()+2);
const td = Utilities.formatDate(today, 'GMT+1', "yyyy/MM/dd");
const td_2 = Utilities.formatDate(today_2, 'GMT+1', "yyyy/MM/dd");
const mylabel = 'unread';
const queryString = `label: ${mylabel} after: ${td} before: ${td_2}`;
Logger.log(queryString); // check the output in the View -> Logs
const threads = GmailApp.search(queryString); // GmailThread[] — an array of Gmail threads matching this query
}
这篇关于如何仅获取今天的电子邮件-Gmail + Google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!