Created
March 7, 2017 16:13
-
-
Save nikolay-kapustin/f325132760310cb494e94bdc7b42321d to your computer and use it in GitHub Desktop.
Google Apps Script for parsing entire gmail messages and save it into google spreadsheet
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
// Google may setup some restrictions for a massive requests per seconds. | |
// So you need planning to run script multiple times | |
var SEARCH_QUERY = '!label:Parsed_by_Script'; | |
// The Main func - entry point | |
function gmailExportToSpreadsheet(){ | |
// we | |
for (var index = 0; index <= 2000; index += 100){ | |
parseEmailMessages(index); | |
Logger.log("Parsed next %s messages.", index); | |
Utilities.sleep(1000); | |
} | |
} | |
function parseEmailMessages(start) { | |
var sheetId, sheet, threads, labelName, label; | |
labelName = 'Parsed_by_Script'; // label for already parsed threads | |
// SheetId is your google spreadsheet id | |
sheetId = '1r7Gb7fLbsiyPImzh4TqX9HWm6SThbcYAO2SsPIPBqOM'; // place id document here (from your browser address line) | |
label = GmailApp.getUserLabelByName(labelName)||GmailApp.createLabel(labelName); | |
sheet = SpreadsheetApp.openById(sheetId).getSheets()[0]; | |
var threads = GmailApp.search(SEARCH_QUERY, start, 100); | |
for (var i = 0; i < threads.length; i++) { | |
// Get the first email message of a threads | |
var messages = threads[i].getMessages(); | |
for (var j = 0; j < messages.length; j++){ | |
// Get the plain text body of the email message | |
// Note that for some types of messages getPlainBody() method may return null | |
// in such cases consider using getRawContent() or getBody() methods and parse HTML | |
var message = messages[j]; | |
var content = message.getPlainBody(); | |
if (content) { | |
var date = message.getDate(), | |
subject = message.getSubject(), | |
sender_tmp = message.getFrom(), | |
body = content; | |
var tmp = sender_tmp.match(/<\s*([A-Za-z0-9_@.-]+)>/); | |
var sender = (tmp && tmp[1]) ? tmp[1].trim() : sender_tmp.trim(); | |
sheet.appendRow([date, subject, sender, body]); | |
}// End if | |
}// End for j loop | |
threads[i].addLabel(label); //add your label for parsed messages | |
//Utilities.sleep(2000); | |
}// End for i loop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment