Created
October 29, 2018 19:56
-
-
Save WillsonSmith/82006eebc4bd68deb2fe5ac717308b47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function searchThreads(offset, max, threads) { | |
const searchedThreads = GmailApp.search("in: inbox older_than:365d", offset, max); | |
var maxDate = new Date(); | |
maxDate.setDate(maxDate.getDate()-365); | |
return searchedThreads.filter(function(thread) { | |
return thread.getLastMessageDate() < maxDate; | |
}); | |
} | |
function moveThreads(threads) { | |
var threadList = threads; | |
if (threadList.length > 100) { | |
GmailApp.moveThreadsToArchive(threadList.splice(0, 100)); | |
moveThreads(threadList); | |
} else { | |
return GmailApp.moveThreadsToArchive(threadList); | |
} | |
} | |
function archive() { | |
var searchedThreads = searchThreads(0, 500, []); | |
moveThreads(searchedThreads); | |
const totalThreadsString = 'Archived a total of ' + searchedThreads.length + ' threads this week.'; | |
GmailApp.sendEmail('[email protected]', 'Archived threads summary', totalThreadsString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment