从线程获取最后一封电子邮件并获取csv附件

时间:2017-01-20 14:16:47

标签: google-apps-script google-apps

我试图获取最后一封电子邮件主题csv附件ID,但我在编写代码时遇到了困难。有人能帮忙吗?

var searcher = "Automail-"+CurrentDateFormat+".csv";

var searchedThreads = GmailApp.search( 'subject:""'+searcher+'""')[0];

var id = searchedThreads.getId(); 
var dateMessage = GmailApp.getMessageById(id).getDate();
var contentmessage = GmailApp.getMessageById(id).getPlainBody();

var attachtextLength = GmailApp.getMessageById(id).getAttachments()[0].getDataAsString().length;
if (attachtextLength >= 150){

var attachtext = GmailApp.getMessageById(id).getAttachments()[0].getDataAsString();

UpdateSheet(attachtext);

1 个答案:

答案 0 :(得分:2)

如果您正在寻找多个线程匹配的最后一个线程....

var searchedThreads = GmailApp.search( 'subject:""'+searcher+'""');
searchedThreads = searchedThreads[searchedThreads.length-1];

如果您正在寻找最后一个附件......

var attachtext = GmailApp.getMessageById(id).getAttachments();
attachtext = attachtext[attachtext.length-1].getDataAsString();

希望它有所帮助。