Skip to content

Instantly share code, notes, and snippets.

@dwaq
Last active September 26, 2018 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwaq/03b556940e56a069bb555bdaf1ab0b75 to your computer and use it in GitHub Desktop.
Save dwaq/03b556940e56a069bb555bdaf1ab0b75 to your computer and use it in GitHub Desktop.
function menu(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Macros')
.addItem('Parse email', 'interest')
.addToUi();
}
function interest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var threads = GmailApp.search('subject: "Dollar Bank rate watch notification*"');
var a=[];
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j = 0; j < messages.length; j++) {
a.push(parseMail(messages[j].getPlainBody(), messages[j].getDate()));
}
}
// start at row 3, overwrite old data
var nextRow=3;
var numRows=a.length;
var numCols=a[0].length;
s.getRange(nextRow,1,numRows,numCols).setValues(a);
}
function parseMail(body, date) {
// split based on newlines
var lines = body.split("\n");
// hold lines split by spaces
var lines_split = [];
// hold important data
var rates = [];
rates.push(date);
for (var i = 0; i < 3; i++)
{
lines_split[i] = lines[i+7].split(" ");
rates.push(lines_split[i][4]);
rates.push(lines_split[i][6]);
rates.push(lines_split[i][10]);
}
return rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment