Google apps script 如何将邮件正文文本保存到Google文档

Google apps script 如何将邮件正文文本保存到Google文档,google-apps-script,gmail,google-docs,Google Apps Script,Gmail,Google Docs,我正在用Google.Scripts编写一个小脚本,用于在我的Gmail帐户中查找特定的邮件地址,并将正文复制到Google.docs。在找到电子邮件和阅读尸体的内容时,它似乎工作得很好,正如我在处决的痕迹中所看到的那样 它还会创建文档,如果文档不存在,则打开文档。但是,当我检查文档时,它是空的。我在这里迷路了,因为在我看来,我正在正确地使用doc方法。以下是脚本的代码: /** * Creates a Google Doc and copy the texts of mails in it.

我正在用Google.Scripts编写一个小脚本,用于在我的Gmail帐户中查找特定的邮件地址,并将正文复制到Google.docs。在找到电子邮件和阅读尸体的内容时,它似乎工作得很好,正如我在处决的痕迹中所看到的那样

它还会创建文档,如果文档不存在,则打开文档。但是,当我检查文档时,它是空的。我在这里迷路了,因为在我看来,我正在正确地使用doc方法。以下是脚本的代码:

/**
 * Creates a Google Doc and copy the texts of mails in it.
 * Mails will be retrieved by sender. This script is intended
 * as a way to get information from newsletters in a single document
 */
function getMailsToDoc() {
  // Create a new Google Doc named 'MyMailsText'
  var doc = DocumentApp.create('MyMailsText');

  // Get the email address of the active user - that's you.
  var email = Session.getActiveUser().getEmail();

  var threads = GmailApp.search('from:NewsLetterMail', 0, 20);
  Logger.log("Messages unread in inbox: " + GmailApp.getInboxUnreadCount());
  var messages = threads[0].getMessages();
  var senderEmail = 'admin@newsletter.com';
  for (var i = 0; i < threads.length; i++) {
        messages = threads[i].getMessages()
        Logger.log(messages[0].getPlainBody());

        // Get all possible mails in the thread and copy their bodies to 
        for (var j = 0; j < messages.length; j++) {
          if(senderEmail == messages[j].getFrom()){
            Logger.log(messages[0].getFrom());
            doc.getBody().appendParagraph(messages[j].getPlainBody());
          }
        }
  }
  doc.saveAndClose();

}

你能给我一些关于我做错了什么的提示,或者给我一些关于如何找出谷歌文档为什么是空的建议吗?我将感谢任何帮助。多谢各位

基于评论的解决方案

正如评论家tehhowch所指出的,if条件总是错误的。这是由于与
邮件[j].getFrom()比较的senderEmail字符串错误造成的

按以下方式更改它可以使其正常工作:

var senderEmail = 'NewsLetterMail <admin@newsletter.com>';
var senderEmail='newslettesmail';

如果电子邮件地址与您期望的地址匹配,则在发件人之间进行匹配后,我看不到相关的
记录器
呼叫。这表明你的
如果
测试总是错误的。你是对的!问题是这个条件总是错误的。玩一玩,我发现了真正的原因。这是因为变量senderEmail的格式不正确。你可以把它写成一个答案,这样我就可以把它标记为一个解决方案(我想可以结束这个问题)。非常感谢你!你可以用一句话自我回答。我要提到的是,在这种情况下,还有其他方法可以比较两个字符串,如果您想使用此函数处理来自电子邮件地址的消息,而该电子邮件地址在运行时才提供,则可以使用此函数。另一种方法会很有趣。请你详细说明或指出一些如何做的想法好吗?非常感谢。
var senderEmail = 'NewsLetterMail <admin@newsletter.com>';