Hi, I am using a script to pull data from Gmail to sheets automatically and it works perfect. I need to know how to modify this script so it will work with PDF attachments instead. Does anyone know how to modify this or know of an existing script that will do the job?
function getRelevantMessages()
{
var threads = GmailApp.search("newer_than:1d AND from:***TheSendersEmailGoesHere.com*** AND -label:Processed AND subject:***Word or words in the subject line go here***",0,100);
var messages=[];
var arrToConvert=[];
for(var i = threads.length - 1; i >=0; i--){
arrToConvert.push(threads[i].getMessages());
}
var messages = [];
for(var i = 0; i < arrToConvert.length; i++)
{
messages = messages.concat(arrToConvert[i]);
}
Logger.log(messages);
return messages;
}
function parseMessageData(messages)
{
var records=[];
if(!messages)
{
//messages is undefined or null or just empty
return records;
}
for(var m=0;m<messages.length;m++)
{
var text = messages[m].getBody();
var matches = text.match(/right">/***REGEX goes here, go to
https://regex101.com to learn REGEX. REGEX will extract partial text from your email***/);
if(!matches || matches.length < 5)
{
//No matches; couldn't parse continue with the next message
continue;
}
var rec = {};
rec.account = matches[3];
rec.date = matches[4];
rec.merchant= matches[2];
rec.amount = matches[1];
//cleanup data
rec.amount = parseFloat(rec.amount.replace(/,/g, ''));
records.push(rec);
}
return records;
}
function getMessagesDisplay()
{
var templ = HtmlService.createTemplateFromFile('messages');
templ.messages = getRelevantMessages();
return templ.evaluate();
}
function getParsedDataDisplay()
{
var templ = HtmlService.createTemplateFromFile('parsed');
templ.records = parseMessageData(getRelevantMessages());
return templ.evaluate();
}
function saveDataToSheet(records)
{
var spreadsheet = SpreadsheetApp.openByUrl(***"
https://docs.google.com/spreadsheets/d/YourOwnGoogleSheetLinkGoesHere***");
var sheet = spreadsheet.getSheetByName("***TheTabNameGoesHere***");
for(var r=0;r<records.length;r++)
{
sheet.appendRow([records[r].account, records[r].date, records[r].merchant, records[r].amount ] );
}
}
function processTransactionEmails()
{
var messages = getRelevantMessages();
var records = parseMessageData(messages);
saveDataToSheet(records);
labelMessagesAsDone(messages);
return true;
}
function labelMessagesAsDone(messages)
{
var label = 'Processed';
var label_obj = GmailApp.getUserLabelByName(label);
if(!label_obj)
{
label_obj = GmailApp.createLabel(label);
}
for(var m =0; m < messages.length; m++ )
{
label_obj.addToThread(messages[m].getThread() );
}
}
function doGet()
{
return getParsedDataDisplay();
//return getMessagesDisplay();
}