I'm trying to extract numbers such as the ones listed below from my Gmail messages using Google Apps Script.
2,495.00
1,594
3,777.23
642.00
This is the code:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var threads = GmailApp.search('subject:(Transaction) after:2016/7/31 before:2016/8/10');
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
var tmp;
for (var j=0; j<messages.length; j++)
{
var content = messages[j].getBody();
var subject = messages[j].getSubject();
var date = messages[j].getDate();
Logger.log(content);
if (content)
{
tmp = content.match(/\d+(,\d+)*(\.\d+(e\d+)?)?/);
var number = (tmp && tmp[j]) ? tmp[j] : 'No number';
sheet.appendRow([number, subject, date]);
}
else
{
sheet.appendRow([content, subject, date]);
}
}
}
}
I've been getting mixed results. For some messages this works as intended but for some it completely skips the numbers from the messages. I'm a newbie to JS/GAS and I thought the problem was in the regex but I'm not sure. Any help in this would be appreciated.