gpt4 book ai didi

javascript - 在当前时间之前 10 分钟使用 Google App Script 从 Gmail 获取电子邮件

转载 作者:行者123 更新时间:2023-12-02 02:01:40 29 4
gpt4 key购买 nike

我想使用 Google App Script 搜索当前时间之前 10 分钟的电子邮件,我编写了以下脚本但它不起作用,因为我有与查询中定义的主题相同的电子邮件和 10 分钟之前(甚至 1 分钟)在邮件出现之前)但 GAS 显示零线程。

function testforemails(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Emails");
var Gmail = GmailApp;
var lasttime = sheet.getRange("Z1").getValue(); // it will use later as the time of last searched email.
Logger.log(lasttime);
var cdate = new Date();
var ctime = cdate.getTime();
var qDate = new Date(ctime - 600000); // less 10 minutes from current time.
Utilities.formatDate(qDate, Session.getScriptTimeZone(), "dd-MMM-yy hh:mm a")
Logger.log("QDATE IS " + qDate); // perfectly displaying time less than 10 minutes
var qtime = qDate.getTime();
Logger.log("qtime is " + qtime);

// SEARCH EMAIL
var query = 'subject: defined subject of emails, after:' + (qDate);
var threadsNew = Gmail.search(query);
Logger.log(threadsNew.length);
var folderid = 'define folder id'
var folder = DriveApp.getFolderById(folderid);
for(var n in threadsNew){
var thdNew = threadsNew[n];
var msgsNew = thdNew.getMessages(); 
var msgNew = msgsNew[msgsNew.length-1];
// GET ATTACHMENT
var bodyNew = msgNew.getBody();
var plainbody = msgNew.getPlainBody();
var subject = msgNew.getSubject();
var Etime = msgNew.getDate();
var attachments = msgNew.getAttachments();
var attachment = attachments[0];

Logger.log(Etime);
Logger.log(subject);
}

Logger.log(threadsNew.length);


}

最佳答案

更新

这确实可以完成,请参阅@Iamblichus 的答案。不管怎样,我的方法也管用,所以我留给用户选择更适合他需要的方法。


实现此目标的解决方法:

function testforemails(){
// SEARCH EMAIL
let now = new Date();
let query = 'subject: YOUR_SUBJECT, after:' + now.toISOString().slice(0, 10); // find mails from today
console.log(query);
let threadsNew = GmailApp.search(query);
console.log("threadsNew.length: " + threadsNew.length);

for(let thread of threadsNew){
let lastMsgTime = thread.getLastMessageDate();
let tenMinutesAgo = new Date(now - 600000);

if(lastMsgTime - tenMinutesAgo > 0) { // if the last message on the thread was less than 10 minutes ago
// do your magic here
console.log(thread.getLastMessageDate())
}
}
}

关于javascript - 在当前时间之前 10 分钟使用 Google App Script 从 Gmail 获取电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68948383/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com