Skip to content

Instantly share code, notes, and snippets.

@rheajt
Last active November 19, 2016 15:48
Show Gist options
  • Select an option

  • Save rheajt/86f0159cdc41d3cf79656f576dbb750d to your computer and use it in GitHub Desktop.

Select an option

Save rheajt/86f0159cdc41d3cf79656f576dbb750d to your computer and use it in GitHub Desktop.
summarize your labels

#EmailSummarizer

This script will look for all emails of a given label. It will grab the first message and append it to a new document. Then it will email you a link to that document so that you don't get bombarded. Instructions:

  1. Create a filter in your Gmail that adds a label to the emails that you want this script to read. Make sure you also set the filter to mark these emails as read, since you will be getting them summarized for you.
  2. Copy the code below into a new Script in your Drive.
  3. Set the label variable (see documentation in code)
  4. Set the number of days you want the trigger to run (see documentation in code)
  5. Set the email address you want to send the link to the created document

OPTIONAL: Look in the code for a for loop that has been commented out. If you want the document to contain every single message and reply in the threads, then switch this code with the code below.

// the name of the label in your Gmail that you want to create a document for
var LABEL = '';
// number of days to use if you set a trigger
var DAYS = 1;
// email address of person to send the link to your document
var EMAIL = 'TEST@EXAMPLE.COM';
// the function that creates the document and emails you a link
function summarizeLabel() {
var threads = GmailApp.getUserLabelByName(LABEL).getThreads();
//create a document to hold all the messages...
var title = DAYS + ' day summary of emails from ' + LABEL;
var doc = DocumentApp.create(title);
for(var i = 0; i < threads.length; i++) {
var table = doc.getBody().appendTable();
table.appendTableRow().appendTableCell(threads[i].getFirstMessageSubject()).setLinkUrl(threads[i].getPermalink());
var messages = threads[i].getMessages();
/* if you want to see all of the messages in the thread then uncomment the following loop */
// for(var j = 0; j < messages.length; j++) {
// table.appendTableRow().appendTableCell(messages[j].getFrom());
// table.appendTableRow().appendTableCell(messages[j].getPlainBody());
// }
/* if you want to see only the first message in each thread */
table.appendTableRow().appendTableCell(messages[0].getFrom());
table.appendTableRow().appendTableCell(messages[0].getPlainBody());
// the following line removes the label and archives the thread
threads[i].removeLabel(GmailApp.getUserLabelByName(LABEL)).moveToArchive();
}
GmailApp.sendEmail(EMAIL, title, doc.getUrl());
}
function setTrigger() {
ScriptApp.newTrigger('summarizeLabel')
.timeBased()
.everyDays(DAYS);
}
function removeTrigger() {
var triggers = ScriptApp.getProjectTriggers();
for(var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment