Skip to content

Instantly share code, notes, and snippets.

@duanebester
Last active August 29, 2015 14:21
Show Gist options
  • Save duanebester/43cb6f5c695469ca7096 to your computer and use it in GitHub Desktop.
Save duanebester/43cb6f5c695469ca7096 to your computer and use it in GitHub Desktop.
Gmail filtering into google Spread Sheet
function createHeaders(sheet) {
// The SpreadSheet headers
var headers = ["Subject", "First Message From", "First Message Date",
"Message Count", "Last Message Date", "Have we replied?"];
// Let's give the columns some info
sheet.appendRow(headers);
// Bold the first row for the headers
sheet.getRange(1, 1, 1, headers.length).setFontWeight("bold")
}
function mainFunction() {
var myEmail = "<[email protected]>";
// URL to SpreadSheet located in Google docs
var spreadSheet = SpreadsheetApp.openByUrl(
'https://docs.google.com/spreadsheets/d/<WORK-SHEET-ID>/edit');
// The first Sheet of the SpreadSheet
var sheet = spreadSheet.getSheets()[0];
createHeaders(sheet);
// Get all Inbox Message Threads (A Thread has multiple messages... email chain)
var threads = GmailApp.getInboxThreads();
// Loop through first 5 Threads as an example
for (var i = 0; i < 5; i++)
{
// Get the thread Subject Line
var threadSubject = threads[i].getFirstMessageSubject();
// Get the last message timestamp
var latestTimestamp = threads[i].getLastMessageDate();
// All Messages within email chain
var messages = threads[i].getMessages();
// When was the first message sent
var firstMessageDate = messages[0].getDate();
// Who sent the first message
var firstMessageFrom = messages[0].getFrom();
// How many messages in the email chain
var messageCount = messages.length;
// The last message in the email chain...
var lastMessageInThread = messages[messageCount - 1];
// We look to see if the last message in the chain
// was sent from your email address
var didNativeReply;
if(lastMessageInThread.getFrom().indexOf(myEmail) > -1)
{
didNativeReply = "Replied To Last Message";
}
else
{
didNativeReply = "Haven't Replied To Last Message";
}
sheet.appendRow([threadSubject, firstMessageFrom, firstMessageDate,
messageCount, latestTimestamp, didNativeReply]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment