Skip to content

Instantly share code, notes, and snippets.

@orvn
Created August 5, 2023 02:27
Show Gist options
  • Save orvn/aefe96195282b9d79cc85bedb18f74f2 to your computer and use it in GitHub Desktop.
Save orvn/aefe96195282b9d79cc85bedb18f74f2 to your computer and use it in GitHub Desktop.
Organize Notion notification emails with Google Apps Script
function countMentionAndCommentEmails() {
var query = 'from:notify@mail.notion.so is:unread';
var start = 0;
var max = 200;
var allThreads = [];
while (true) {
var threads = GmailApp.search(query, start, max);
if (threads.length === 0) {
break;
}
allThreads = allThreads.concat(threads);
start += max;
}
var subjectsCount = {};
for (var i = 0; i < allThreads.length; i++) {
var messages = allThreads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
if (messages[j].isUnread()) {
var subject = messages[j].getSubject();
// Clipping out the part before "mentioned you in " or "commented in "
var mentionedIndex = subject.indexOf("mentioned you in ");
var commentedIndex = subject.indexOf("commented in ");
if (mentionedIndex != -1) {
subject = subject.substring(mentionedIndex + "mentioned you in ".length);
} else if (commentedIndex != -1) {
subject = subject.substring(commentedIndex + "commented in ".length);
}
if (!subjectsCount[subject]) {
subjectsCount[subject] = 0;
}
subjectsCount[subject]++;
}
}
}
var sortable = [];
for (var subject in subjectsCount) {
sortable.push([subject, subjectsCount[subject]]);
}
sortable.sort(function(a, b) {
return b[1] - a[1];
});
Logger.log("Unread email count by subject:");
for (var i = 0; i < sortable.length; i++) {
Logger.log(sortable[i][0] + ": " + sortable[i][1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment