本文介绍了如何删除GMail标签 - Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从放入收件箱的每封邮件中删除标签Followup。我尝试了几件事,但仍然没有成功。我希望有人能够帮助我,或者指引我走向正确的方向。相关函数是:

I want to remove the label "Followup" from each message that is placed back in the inbox. I have tried several things, but still without success. I hope someone can help me or point me in the right direction. The function concerned is:

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);
}

整个脚本是:

The entire script is:

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");
}


推荐答案

如果您想删除 FollowUp,你可以使用 label.deleteLabel()。但是,由于您只是希望将标签从要恢复到收件箱的线程中取出,因此您需要循环访问它们。

If you wanted to remove "FollowUp" from all threads, you could use label.deleteLabel(). But since you're just interested in taking that label off of the threads you're restoring to the Inbox, you need to loop through them.

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

这篇关于如何删除GMail标签 - Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:18