我认为这与如何使用Google App脚本以自定义方式检索Gmail电子邮件?,因为返回的数字不是整数(例如500).我假设我可以使用getSpamThreads,getStarredThreads,getTrashThreads,getDraftMessages来获取相关文件夹,但是直到我理解为什么我只从收件箱中收到一些电子邮件时,我才不相信这些电子邮件会给我一切. /p>任何人都可以帮忙吗?解决方案尝试一下:function allEmailsInLabels() { var allLabels,i,j,L,L2,msgCount,theCount,threads,thisLabel; msgCount = 0; theCount = 0; allLabels = GmailApp.getUserLabels(); L = allLabels.length; for (i = 0; i < L; i++) { Logger.log("label: " + allLabels[i].getName()); thisLabel = allLabels[i]; threads = thisLabel.getThreads(); //Logger.log('threads: ' + threads); L2 = threads.length; for (j = 0; j < L2; j++) { msgCount = threads[j].getMessageCount(); //Logger.log('thread message count: ' + threads[j].getMessageCount()); // You could do something with threads[j] here like // threads[j].moveToTrash(); theCount = theCount + msgCount; }; }; //Logger.log('theCount: ' + theCount);};它首先获取所有标签,然后获取线程,然后获取每个线程中的消息计数,并保持运行计数.您还需要在收件箱中获取消息,该代码不包含这些消息.这是文档中的示例代码,显示了基本概念:// Log the subject lines of your Inboxvar threads = GmailApp.getInboxThreads();for (var i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject());}I'm trying to read ALL email in my gmail account - inbox, sent, draft, trash, emails with labels, archive, etc. I could live without the junk but I want everything else.(all examples below use try {} catch {} to avoid errors with empty labels etc.)I've triedfor (var i=StartLabel; i<=EndLabel; i++){ var label = labels[i].getName(); // get all messages, then join them into a single dimension array var messages = GmailApp.getMessagesForThreads(GmailApp.search("label:" + label)) .reduce(function(a, b) {return a.concat(b);}); CountByLabels += messages.length;}That gives me everything in the labels (I think) but not the other stuff.I tried other things, to get the inbox (to combine with the above) or all of the emailsvar messages = GmailApp.getMessagesForThreads(GmailApp.getInboxThreads()).reduce(function(a, b) {return a.concat(b);});CountInbox += messages.length;but I only get about 549 results (GMail shows 5,478). If I add in the results from getPriorityInboxThreads I get 1,829 results.I tried// get all messages, then join them into a single dimension arrayvar messages = GmailApp.getMessagesForThreads(GmailApp.search("(is:unread OR is:read) in:anywhere")).reduce(function(a, b) {return a.concat(b);});CountByLabels += messages.length;I get 598 results.I tried different search terms in the code directly above, eg:is:unread = 528 resultsis:read = 1,037 resultsis:read OR is:unread = 599 resultsNone of them gave the right number, or even close, and incidentally if I try those search terms directly in gmail I get a totally different, and much higher, result for each - several thousand, or 'many'.I don't think this is related to How to use Google App Scripts to retrieve Gmail emails in a customised way? as the numbers returned are not round numbers (eg 500).I'm assuming that I can use getSpamThreads, getStarredThreads, getTrashThreads, getDraftMessages to get the relevant folders but until I understand why I'm only getting some emails from the inbox I don't trust those to give me everything.Can anyone help? 解决方案 Try this:function allEmailsInLabels() { var allLabels,i,j,L,L2,msgCount,theCount,threads,thisLabel; msgCount = 0; theCount = 0; allLabels = GmailApp.getUserLabels(); L = allLabels.length; for (i = 0; i < L; i++) { Logger.log("label: " + allLabels[i].getName()); thisLabel = allLabels[i]; threads = thisLabel.getThreads(); //Logger.log('threads: ' + threads); L2 = threads.length; for (j = 0; j < L2; j++) { msgCount = threads[j].getMessageCount(); //Logger.log('thread message count: ' + threads[j].getMessageCount()); // You could do something with threads[j] here like // threads[j].moveToTrash(); theCount = theCount + msgCount; }; }; //Logger.log('theCount: ' + theCount);};It first gets all the labels, then the threads, then the message count in each thread, and keeps a running count. You'll also need to get the messages in the inbox, that code doesn't include them. This is the sample code from the documentation that shows the basic concept:// Log the subject lines of your Inboxvar threads = GmailApp.getInboxThreads();for (var i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject());} 这篇关于如何使用Google Apps脚本读取Gmail中的所有电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!