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");
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With