我正在用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();
}
下面是跟踪示例:
[18-09-08 06:07:56:535 PDT] Iniciando ejecución
[18-09-08 06:07:57:592 PDT] DocumentApp.create([MyMailsText]) [1,051 segundos]
[18-09-08 06:07:57:593 PDT] Session.getActiveUser() [0 segundos]
[18-09-08 06:07:57:593 PDT] User.getEmail() [0 segundos]
[18-09-08 06:07:58:109 PDT] GmailApp.search([from:NewsLetterMail, 0, 20]) [0,514 segundos]
[18-09-08 06:07:58:212 PDT] GmailApp.getInboxUnreadCount() [0,102 segundos]
[18-09-08 06:07:58:213 PDT] Logger.log([Messages unread in inbox: 265, []]) [0 segundos]
[18-09-08 06:07:58:334 PDT] GmailThread.getMessages() [0,12 segundos]
[18-09-08 06:07:58:335 PDT] GmailThread.getMessages() [0 segundos]
[18-09-08 06:07:58:443 PDT] GmailMessage.getPlainBody() [0,107 segundos]
**[18-09-08 06:07:58:444 PDT] Logger.log([News and information related to different topics, blablablabla, []]...) [0 segundos]**
[18-09-08 06:07:58:444 PDT] GmailMessage.getFrom() [0 segundos]
[18-09-08 06:07:58:839 PDT] GmailThread.getMessages() [0,395 segundos]
[18-09-08 06:07:58:934 PDT] GmailMessage.getPlainBody() [0,094 segundos]
**[18-09-08 06:07:58:934 PDT] Logger.log([More interesting news from this interesting newsletter blablabla , []]...) [0 segundos]**
您能给我一些关于我做错了什么的提示,或者是关于如何弄清楚为什么Google文档为空的建议吗?我将不胜感激任何帮助。非常感谢你!
答案 0 :(得分:0)
基于评论的解决方案
正如评论员tehhowch指出的,if条件始终为false。这是由于错误的senderEmail字符串与messages[j].getFrom()
进行比较。
按如下所示进行更改即可:
var senderEmail = 'NewsLetterMail <admin@newsletter.com>';