Created
December 17, 2018 21:25
-
-
Save richarmstrong/73353a4fd1095fdb8b02c0e7a31ad437 to your computer and use it in GitHub Desktop.
Label all gmail conversations you were having on this day in the past.
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
// This function finds all the conversations you were having on this day in the past years (since Gmails release in 2005), | |
// and adds the "On This Day" label to them. Put it in a new apps script project and set it to run at 3am every day. | |
// It'll first clear out the label and then add all relevant messages. | |
function onThisDay(){ | |
var onThisDayLabel = GmailApp.getUserLabelByName("On This Day") | |
var date = new Date() | |
var yesterday = new Date(); | |
yesterday.setDate(yesterday.getDate() - 1); | |
var tomorrow = new Date(); | |
tomorrow.setDate(tomorrow.getDate() + 1); | |
if (onThisDayLabel) { | |
Logger.log('label found') | |
// remove all the current messages on the label | |
Logger.log('removing all current messages on the label') | |
var threadsOnLabel = onThisDayLabel.getThreads() | |
for (yy = 0; yy < threadsOnLabel.length; yy++) { | |
onThisDayLabel.removeFromThread(threadsOnLabel[yy]) | |
} | |
Logger.log('finding messages for each year') | |
for (var xx = 2005; xx < date.getYear()-1; xx++) { | |
Logger.log('looking at year ' + xx) | |
tomorrow.setFullYear(xx) | |
yesterday.setFullYear(xx) | |
var searchQuery = "to:me -category:updates -category:promotions -category:social after:" + Utilities.formatDate(yesterday, "GMT", "YYYY/MM/dd") + " before:" + Utilities.formatDate(tomorrow, "GMT", "YYYY/MM/dd") | |
Logger.log(searchQuery) | |
var thisYearsThreads = GmailApp.search(searchQuery) | |
for (var j = 0; j < thisYearsThreads.length; j++){ | |
thisYearsThreads[j].addLabel(onThisDayLabel) | |
} | |
} | |
} else { | |
Logger.log('label not found') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment