在谷歌应用程序脚本中的100多个线程中使用.removeFromThreads

时间:2017-08-17 17:36:06

标签: google-apps-script gmail-api

我有一个脚本可以删除标签和所有子标签中的每个线程。这样做很好,但最近我收到一个错误,指出操作childrens[i].removeFromThreads(threads);不能应用于更多的100个线程..我该如何解决这个问题?

function removingThreadsfromLabel() {
  var parentlabelstring = 'THELabel';
  var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
  var childrens = children(parentlabel);

  for (var i = 0; i < childrens.length; i++){
    var threads = childrens[i].getThreads();
    childrens[i].removeFromThreads(threads);
  }
}

function children(parent) { 
  var name = parent.getName() + '/';
  return GmailApp.getUserLabels().filter(function(label) {
    return label.getName().slice(0, name.length) == name;
  });
}

现在我做了以下工作,但这不是最佳的......

function removingThreadsfromLabel() {
  var parentlabelstring = 'THELabel';
  var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
  var childrens = children(parentlabel);

  for (var i = 0; i < childrens.length; i++){
    var threads = childrens[i].getThreads();
    Logger.log(threads.length);
    while (threads.length>100){
      childrens[i].removeFromThread(threads[0]);
      var threads = childrens[i].getThreads();
    }   
    childrens[i].removeFromThreads(threads);
  }
}

此致

1 个答案:

答案 0 :(得分:0)

好的,我弄清楚如何批处理,所以我的第一个解决方案更好:

function removingThreadsfromLabel() {
  var batchSize = 100;
  var parentlabelstring = 'THELabel';
  var parentlabel = GmailApp.getUserLabelByName(parentlabelstring);
  var childrens = children(parentlabel);

  for (var i = 0; i < childrens.length; i++){
    var threads = childrens[i].getThreads();
    Logger.log(threads.length);
    for (var j = 0; j < threads.length; j+=batchSize) {
      childrens[i].removeFromThreads(threads.slice(j, j+batchSize));
    }
    var threads = childrens[i].getThreads();
    childrens[i].removeFromThreads(threads);    
  }