-
-
Save daudrain/fdfb7536f79a55a37b19b44e5f0b96fe to your computer and use it in GitHub Desktop.
Parse Gmail Inbox to sheet
This file contains hidden or 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.search('in:unread', 0, 100); | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var result = []; | |
for (var i = 0; i < threads.length; i++) { | |
var thread = threads[i]; | |
var messages = thread.getMessages(); | |
var message = messages[0]; | |
// https://developers.google.com/apps-script/reference/gmail/gmail-message for Message API | |
if (message.isUnread()) { | |
var content = message.getPlainBody(); | |
if (content) { | |
var tmp; | |
/* | |
* See https://regex101.com/r/H1mSmn/1 for the regex | |
*/ | |
tmp = content.match(/Firstname\s*[:]\s*[*]([A-Za-z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ\s-?@.']+)[*]/); | |
if (tmp && tmp[1]) { | |
var firstname = ""; | |
firstname = tmp[1].trim(); | |
tmp = content.match(/Lastname\s*[:]\s*[*]([A-Za-z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ\s-?@.']+)[*]/); | |
if (tmp && tmp[1]) { | |
var lastname = ""; | |
lastname = tmp[1].trim(); | |
tmp = content.match(/Company\s*[:]\s*[*]([A-Za-z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ\s]+)[*]/); | |
if (tmp && tmp[1]) { | |
var platform = tmp[1].trim(); | |
var from = message.getFrom(); | |
sheet.appendRow([from, firstname, lastname, platform]); | |
Utilities.sleep(200); | |
thread.markRead(); | |
} | |
} | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modifications made from the original file assumes there are 3 fields in the email:
-Firstname
-Lastname
-Platform
When all fields have been successfully read, a new row is appended to the sheet with the from email, firstname, lastname and platform.
The thread is marked as read.
The script only analyzes unread threads.