【问题标题】:Is there a more efficient way to get arbitrary numbers of threads from Gmail with GmailApp.search()?有没有更有效的方法可以使用 GmailApp.search() 从 Gmail 中获取任意数量的线程?
【发布时间】:2020-01-06 13:44:09
【问题描述】:

我试图从我的 Gmail 中的特定标签中获取大量电子邮件,并将它们放入 Google 云端硬盘文档中。如何使用 GmailApp.search() 获取邮件而不丢失任何邮件?

我尝试简单地使用非范围定义的 GmailApp.search()。根据日志,它只返回了 500 封电子邮件,而我在这个标签中大约有 2000 封。问题是,如果没有 500 封(或任何数量)更多可用的电子邮件,GmailApp.search() 会返回一个空白数组。

假设我有 600 封电子邮件,但不知道确切的数字。 我可以使用 GmailApp.search(searchQuery, 0, 500) 返回前 500 封邮件,但是第二次搜索 500 会返回一个空白数组,因为只剩下 100 封电子邮件。在这种情况下,我怎样才能得到剩下的 100 个?如果我想要每封电子邮件,一次单步执行一个线程,如下面的代码所述,真的是最好的解决方案吗?

for(var i = 0; i < ; i++) {
    //get a single thread 
    var thread = GmailApp.search(searchQuery, i, 1);
    //...
  }

我想输出所有电子邮件,但是没有指定范围的 GmailApp.search(searchQuery) 会返回 500 封电子邮件(至少根据 Logger - 我让它在每封电子邮件之后记录一行,它从 0 开始最多只能达到 499)。

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    var thread = GmailApp.search(searchQuery, i, 1); 更有效的是估计您最大期望的电子邮件数量(例如 2000 封)并运行搜索查询 2000/500 次:

    var threadArray=[];
        for(var j = 0; j <3 ; j++){    
        var thread = GmailApp.search(searchQuery, j*500, 500);
        thread.forEach(function(e){threadArray.push(e.getId());})
        //now you have an array containing the Ids of all queried emails 
      } 
    

    这只会将找到的元素附加到您的数组中,而不是空白条目。

    【讨论】:

      【解决方案2】:

      试试这个:

      function getAllThreads(qry) {
        var qry=qry || 'label:qs-financial-institutions-wfb';
        var threads=GmailApp.search(qry);
        var html="";
        for(var i=0;i<threads.length;i++) {
          html+=Utilities.formatString('<br />%s -Thread Id:%s First Subject: %s Message Count: %s',i+1,threads[i].getId(),threads[i].getFirstMessageSubject(),threads[i].getMessageCount());
          //You can put another loop in here to get all of the messages
        }
        var userInterface=HtmlService.createHtmlOutput(html).setWidth(1200);
        SpreadsheetApp.getUi().showModelessDialog(userInterface, "Threads");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多