首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试理解GAS中的getThreads

尝试理解GAS中的getThreads
EN

Stack Overflow用户
提问于 2018-01-28 03:03:50
回答 1查看 579关注 0票数 0

我是应用程序脚本的新手,我正在尝试从收件箱中读取一封电子邮件。我认为getThreads可以完成这项工作,但我仍然不完全理解如何使用它。当我尝试执行我在下面写的代码时,它出现了一个空错误。

在查看documentation of getThreads()时,他们使用了以下示例:

代码语言:javascript
运行
复制
 // Log the subject lines of the threads labeled with MyLabel
 var label = GmailApp.getUserLabelByName("MyLabel");
 var threads = label.getThreads();
 for (var i = 0; i < threads.length; i++) {
   Logger.log(threads[i].getFirstMessageSubject());
 }

"MyLabel“代表什么?

这是我尝试的代码,但失败了

代码语言:javascript
运行
复制
function myFunction() {


   var label = GmailApp.getUserLabelByName('bobtheman@gmail.com');
   var threads = label.getThreads();


     for (var t in threads) {
       var thread = threads[t];

    // Gets the message body
       var message = thread.getMessages()[0].getPlainBody();
     }

  GmailApp.sendEmail('barbrabat@gmail.com', 'hola', message)
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-18 15:20:07

MyLabel是电子邮件的标签。这取决于您是否在特定电子邮件中添加了标签。您可以改用搜索方法。

代码语言:javascript
运行
复制
function myFunction(){
   var label = 'yourLabel'; // if no label, remove the label in search
   // it would be better if you add a label to a specific email for fast and more precise searching
   var searchEmail = GmailApp.search('from:me subject:"' + subject + '" label:' + label + '');
   var threadId = searchEmail[0].getId(); // get the id of the search email
   var thread = GmailApp.getThreadById(threadId); // get email thread using the threadId
   var emailMsg = thread.getMessages()[0]; // get the content of the email
   var emailContent = emailMsg.getPlainBody(); // get the body of the email
   // check using log
   Logger.log(emailContent);
   // how to open log
   // Ctrl + Enter
   // Run this function before checking the log
}

谢谢

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48479463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档