我想从收件箱中的每封邮件中删除标签"跟进"。我已经尝试了几件事,但仍然没有成功。我希望有人能帮助我或为我指出正确的方向。相关功能是:
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");
}
如果要从所有线程中删除"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);
}
}