Skip to content

Instantly share code, notes, and snippets.

@imightbeamy
Created October 18, 2018 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imightbeamy/6d63f0b81bfe84f48c11204a841d68cd to your computer and use it in GitHub Desktop.
Save imightbeamy/6d63f0b81bfe84f48c11204a841d68cd to your computer and use it in GitHub Desktop.
var DAYS = 24;
function gmailAutoarchive() {
processesLabel(12, "merged", archive);
processesLabel(7*DAYS, "OOO", archive);
processesLabel(20, "daily", archive);
processesLabel(7*DAYS, "Etc.", markRead);
}
var MILLIS_PER_HOUR = 1000 * 60 * 60;
function processesLabel(delayHours, labelName, action) {
var maxDate = new Date((new Date()).getTime() - (delayHours*MILLIS_PER_HOUR));
// Get all the labelled threads
var label = GmailApp.getUserLabelByName(labelName);
var threads = label.getThreads(0, 400);
// Take action on threads if they're older than the limit set in delayHours
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate) {
action(threads[i]);
}
}
}
function archive(thread) {
thread.moveToArchive();
}
function markRead(thread) {
thread.markRead();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment