Skip to content

Instantly share code, notes, and snippets.

@daudrain
Forked from siygle/gist:9678772
Last active July 17, 2020 14:56
Show Gist options
  • Save daudrain/fdfb7536f79a55a37b19b44e5f0b96fe to your computer and use it in GitHub Desktop.
Save daudrain/fdfb7536f79a55a37b19b44e5f0b96fe to your computer and use it in GitHub Desktop.
Parse Gmail Inbox to sheet
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();
}
}
}
}
}
}
};
@daudrain
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment