Created
January 17, 2016 13:47
-
-
Save nyctef/2263ac926f68e2d136e8 to your computer and use it in GitHub Desktop.
Pull csv from hsbc statements
This file contains 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(window) { | |
// based on https://github.com/dahousecat/hsbc-statement-to-csv/blob/master/create-csv.js | |
function assert(condition, message) { | |
if (!condition) throw message || "Assertion failed"; | |
} | |
assert($, "need jquery") | |
assert($('table[summary*="This table contains"] thead').length == 1, "expecting one table head") | |
var $tbody = $('table[summary*="This table contains"] tbody') | |
assert($tbody.length == 1, "expecting one table body") | |
function csvClean(x) { | |
return x.replace(/[\r,]/g, '').replace(/\n/g, ' ').trim() | |
} | |
var csv = "" | |
$('tr', $tbody).each((i, x) => { | |
var cells = $('td', $(x)) | |
var date = csvClean(cells[0].innerText) | |
var type = csvClean(cells[1].innerText) | |
var desc = csvClean(cells[2].innerText) | |
var pout = csvClean(cells[3].innerText) | |
var pin = csvClean(cells[4].innerText) | |
var aftr = csvClean(cells[5].innerText) | |
console.log(date, ",", type, ",", desc, ",", pout, ",", pin, ",", aftr) | |
csv += `${date}, ${type}, ${desc}, ${pout}, ${pin}, ${aftr}\n` | |
}) | |
var statement_date = new Date().toISOString() // todo: pull from webpage? | |
var data = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); | |
$('body').append('<a href="'+data+'" download="statement-'+(statement_date.replace(' ', '-'))+'.csv" id="download-statement" style="display: none;">Download</a>'); | |
$('#download-statement')[0].click(); | |
})(window) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment