Created
March 21, 2014 03:12
-
-
Save siygle/9678772 to your computer and use it in GitHub Desktop.
Parse Gmail Inbox to sheet
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
function processInboxToSheet() { | |
//var threads = GmailApp.getInboxThreads(); | |
// Have to get data separate to avoid google app script limit! | |
var start = 0; | |
var threads = GmailApp.getInboxThreads(start, 100); | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var result = []; | |
for (var i = 0; i < threads.length; i++) { | |
var messages = threads[i].getMessages(); | |
var content = messages[0].getPlainBody(); | |
// implement your own parsing rule inside | |
if (content) { | |
var tmp; | |
tmp = content.match(/Name:\s*([A-Za-z0-9\s]+)(\r?\n)/); | |
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username'; | |
tmp = content.match(/Email:\s*([A-Za-z0-9@.]+)/); | |
var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email'; | |
tmp = content.match(/Subject:\s*([A-Za-z0-9\s]+)(\r?\n)/); | |
var subject = (tmp && tmp[1]) ? tmp[1].trim() : 'No subject'; | |
tmp = content.match(/Comments:\s*([\s\S]+)/); | |
var comment = (tmp && tmp[1]) ? tmp[1] : 'No comment'; | |
sheet.appendRow([username, email, subject, comment]); | |
Utilities.sleep(500); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two problems with this example:
.getPlainBody
will not include the subject (use the separate method call for getting the subject instead).forEach
call instead of a loop to avoidvar j
... funkiness)