亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何僅獲取今天的電子郵件 - Gmail + Google Script

如何僅獲取今天的電子郵件 - Gmail + Google Script

汪汪一只貓 2023-05-25 17:29:12
在我的谷歌腳本中,我只想解析今天+帶有特定標簽的電子郵件。使用 Gmail.Users.Messages.list() 的解決方案我發現可能的解決方案是使用搜索查詢。今天是20.10.2020,此搜索查詢after:2020/10/20 before:2020/10/22 僅返回今天的電子郵件。如果我使用此解決方案,我不知道如何將正確的日期傳遞給查詢。使用 GmailApp.getUserLabelByName() 的解決方案我寧愿使用GmailApp.getUserLabelByName()notGmail.Users.Messages.list()這樣我就可以使用線程而不是消息。我正確地理解這兩種方法是如何工作的。
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

解釋:

  • 正如Cooper所建議的那樣,您可以使用Utilities類構造日期并將它們轉換為所需的格式。

  • 然后,您可以使用模板文字構造查詢參數并將所有日期和標簽變量傳遞給字符串對象。

  • getSpreadsheetTimeZone()用于獲取電子表格的時區。您可以將其替換為實際的GMT,例如:

    const?td?=?Utilities.formatDate(today,?'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 = 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);?


}

自定義時區的解決方案:

調整GMT+1到您自己/想要的時區。


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

}


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號