How to remove GMail label - Google Apps script

送分小仙女□ 提交于 2019-12-09 18:56:33

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/18846457/how-to-remove-gmail-label-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!