试图了解GAS中的getThreads [英] Trying to understand getThreads in GAS

查看:88
本文介绍了试图了解GAS中的getThreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to app script and I am trying to read an email from my inbox. I thought that getThreads would do the job but I still don't fully understand how to use it. When I try to execute the code I wrote below it comes up with a null error.

查看 getThreads()的文档,他们使用示例:

Looking at the documentation of getThreads(), they use the example:

 // 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"代表什么?

what does "MyLabel" stand for?

这是我尝试失败的代码

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)
}

推荐答案

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

MyLabel is the label of the email. It depends whether you added a label to a specific email or not. You can use the search method instead.

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
}

谢谢

这篇关于试图了解GAS中的getThreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆