如何删除GMail标签 - Google Apps脚本

时间:2013-09-17 09:44:30

标签: google-apps-script gmail google-api

我想从收件箱中放回的每封邮件中删除标签“Followup”。我尝试过几件事,但仍然没有成功。我希望有人可以帮助我或指出我正确的方向。有关的职能是:

function moveToInbox(page) {
  GmailApp.moveThreadsToInbox(page);
//  GmailApp.markThreadsUnread(page);
//  GmailApp.starMessages(page)
  var label = GmailApp.getUserLabelByName("FollowUp");
  var firstThread = GmailApp.getInboxThreads(0,1)[0];
  var coupleOfMessages = firstThread.getMessages().slice(0, 10);
  GmailApp.starMessages(coupleOfMessages);
  label.removeFromThread(firstThread);
}

整个脚本是:

function getLabelName(i, labelSuffixString) {
  return "FollowUp/" + i + labelSuffixString;
}

function setup() {
  for (var i = 1; i <= 7; ++i) {
    GmailApp.createLabel(getLabelName(i, "days"));
    GmailApp.createLabel(getLabelName(i, "weeks"));
  }
  GmailApp.createLabel("FollowUp");
}

function moveToInbox(page) {
  GmailApp.moveThreadsToInbox(page);
//  GmailApp.markThreadsUnread(page);
//  GmailApp.starMessages(page)
  var label = GmailApp.getUserLabelByName("FollowUp");
  var firstThread = GmailApp.getInboxThreads(0,1)[0];
  var coupleOfMessages = firstThread.getMessages().slice(0, 10);
  GmailApp.starMessages(coupleOfMessages);
  label.removeFromThread(firstThread);
}

function cleanOldFollowUpLabels() {
  var searchString = "-label:inbox label:FollowUp";
  for (var i = 1; i <= 7; ++i) {
    searchString += " -label:" + getLabelName(i, "days");
    searchString += " -label:" + getLabelName(i, "weeks");
  }
  searchString = searchString.replace(RegExp("/", "g"), "-");
  Logger.log("cleanOldFollowUpLabels() Search String:");
  Logger.log(searchString);
  var followUpLabel = GmailApp.getUserLabelByName("FollowUp");  
  var page = null;
  // Get threads in "pages" of 100 at a time
  while(!page || page.length == 100) {
    page = GmailApp.search(searchString, 0, 100);
    Logger.log("found: " + page.length);
    if (page.length > 0)
      followUpLabel.removeFromThreads(page);   
  }
}

function update(labelSuffixString) {
  var oldLabel, newLabel, page;
  var followUpLabel = GmailApp.getUserLabelByName("FollowUp");
  for (var i = 1; i <= 7; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i, labelSuffixString));
    page = null;
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      if (page.length > 0) {
        followUpLabel.addToThreads(page);
        if (newLabel) {
          // Move the threads into "today’s" label
          newLabel.addToThreads(page);
        } else {
          moveToInbox(page);
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
        // Wait for a minute to prevent timeout errors
        Utilities.sleep(1000);
      }  
    }
  }
}

function dailyUpdate() {
  update("days");
}
function weeklyUpdate() {
  update("weeks");
}

1 个答案:

答案 0 :(得分:5)

如果要从所有线程中删除“FollowUp”,可以使用label.deleteLabel()。但是,由于您只想从正在恢复到收件箱的线程中取出该标签,因此需要循环使用它们。

function moveToInbox(threadArray) {
  GmailApp.moveThreadsToInbox(threadArray);
  var label = GmailApp.getUserLabelByName("FollowUp");
  for (var i=0; i< threadArray.length; i++) {
    threadArray[i].removeLabel(label);
  }
}