Skip to content

Instantly share code, notes, and snippets.

@patt0
Created March 15, 2013 03:15
Show Gist options
  • Save patt0/5167237 to your computer and use it in GitHub Desktop.
Save patt0/5167237 to your computer and use it in GitHub Desktop.
This simple code snippet can be included in a Google Docs spreadsheet script editor to extract header information from email.
function processInbox() {
sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// get all threads with the label draft > replave with the lable you need
searchString = "label:draft";
var threads = GmailApp.search(searchString);
for (var i = 0; i < threads.length; i++) {
// get all messages in a given thread
var messages = threads[i].getMessages();
// iterate over each message
for (var j = 0; j < messages.length; j++) {
var data = [];
data[0] = new Array();
data[0][0] = messages[j].getFrom();
data[0][1] = messages[j].getTo();
data[0][2] = messages[j].getCc();
data[0][3] = messages[j].getBcc();
data[0][4] = messages[j].getSubject();
data[0][5] = messages[j].getDate();
var attachments = messages[j].getAttachments();
// number of attachments
data[0][6] = attachments.length;
var attachmentList = ""
for (var k = 0; k < attachments.length; k++) {
attachmentList += attachments[k].getName() + ", ";
}
// name of attachments if any
data[0][7] = attachmentList;
// this link will allow you to open the mail thread from the spreadsheet to see the attachments.
data[0][8] = threads[i].getPermalink();
// update spreasheet
sheet.getRange(sheet.getLastRow()+1, 1, 1, 9).setValues(data);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment