GmailApp.moveToArchive:执行成功,但没有任何动静

时间:2014-01-17 05:47:03

标签: javascript google-apps-script gmail

更新:嗯,似乎发生了一些事情。现在,我的收件箱已经从36,000+消息增加到353,000多封。咦?到底是怎么回事。


我从未存档我的Gmail收件箱,因此其中包含36,000多封邮件。我想存档超过60天的所有邮件,我不能手动完成。

所以经过一些谷歌搜索后,我找到了一个谷歌脚本,可以做到这一点,下面。但是,GmailApp.moveThreadsToArchive(threads);显然一次最多只能调用100个线程,所以我尝试将它变成一个while循环,每次调用100个线程多次:

/**
* Archives Emails older than a given time interval
* src:  http://www.quora.com/Gmail/Is-there-a-way-to-auto-archive-emails-after-a-certain-number-of-days
* 
* example:
*   RunAutoArchive('2w')
*   RunAutoArchive('1m')
*   RunAutoArchive('5y')
*/

function RunAutoArchive(){

  // archive messages older than:
  var interval='60d';

  // number of threads
  var threadCount = 0;
  var start = 0; 
  var end = 100;

  // find messages older than a certain time
  if(interval != ""){
    var searchQuery = 'in:Inbox older_than:'+interval;
    var threads = GmailApp.search(searchQuery);    
    threadCount = threads.length;

    // if there are any threads
    if(threadCount > 0){
      while(start <= threadCount){

        // move threads to archive
        GmailApp.moveThreadsToArchive(threads.slice(start,end));

        // increment count
        start += 100;
        end += 100;

      }
    }
  }

  // refresh threads
  GmailApp.refreshThreads(threads);

  return threadCount;
}

然而,它既调试又运行成功(很长的执行记录以[14-01-16 21:40:59:199 PST] Execution succeeded [284.472 seconds total runtime]结尾),但我的收件箱保持不变 - 剩下36,000多条未归档的邮件。

有什么建议可能是错的吗?

1 个答案:

答案 0 :(得分:0)

要在x天后将收件箱gmail线程移动到存档中,您可以使用以下内容并每分钟设置一次触发器来存档大量传入的电子邮件,只需设置scriptproperties键&#34; CNT&#34;为0并运行以下脚本

function gmailAutoarchive1() {

  var delayDays = 2; // will only impact emails more than 48h old
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
  Logger.log("MAXDATE",maxDate);

  // Get all the threads labelled 'autoarchive'
  var label = GmailApp.getUserLabelByName("AUTOARCHIVE");

  var scriptProperties = PropertiesService.getScriptProperties();
  var cnt=scriptProperties.getProperty("CNT");
  var cnt_val=parseInt(cnt)


//  var threads = label.getThreads(cnt_val,499);
   var threads = GmailApp.getInboxThreads(cnt_val,499);

  // we archive all the threads if they're unread AND older than the limit we set in delayDays
  for (var i = 0; i < threads.length; i++) {

      Logger.log("del",threads[i].getLastMessageDate());     

    if (threads[i].getLastMessageDate()<maxDate)
    {  
      threads[i].moveToArchive();
     // threads[i].moveToTrash();
    }
  }

  scriptProperties.setProperty("CNT", cnt_val+499)

}