Last active
December 4, 2022 16:39
-
-
Save commonquail/3c1f9503034358fdbf0d679b8b8a2c08 to your computer and use it in GitHub Desktop.
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
var months = { | |
"jan.": "01", | |
"feb.": "02", | |
"mar.": "03", | |
"apr.": "04", | |
"maj": "05", | |
"juni": "06", | |
"juli": "07", | |
"aug.": "08", | |
"sept.": "09", | |
"okt.": "10", | |
"nov.": "11", | |
"dec.": "12" | |
}; | |
var save = (transactions) => { | |
const filename = "ynab.csv"; | |
const blob = new Blob([transactions], {type: 'text/csv'}); | |
if (window.navigator.msSaveOrOpenBlob) { | |
window.navigator.msSaveBlob(blob, filename); | |
} else { | |
const elem = document.createElement('a'); | |
elem.href = window.URL.createObjectURL(blob); | |
elem.download = filename; | |
document.body.appendChild(elem); | |
elem.click(); | |
document.body.removeChild(elem); | |
window.URL.revokeObjectURL(elem); | |
} | |
} | |
var stot = (e, selector) => e.querySelector(selector).innerText; | |
var perItem = (prev, cur) => { | |
// YNAB wants date format dd/mm/yyyy. | |
// Date field format is "dd. mmm. yyyy" ... | |
var date = stot(cur, '.transaction-field--date'); | |
// ... unless it is "I dag" or "I går", then construct the value dynamically. | |
if (date.startsWith("I ")) { | |
const d = new Date(Date.now()); | |
if (date === "I går") { | |
d.setDate(d.getDate() - 1); | |
} | |
const opts = { year: "numeric", month: "numeric", day: "numeric" }; | |
// dd/mm/yyyy | |
date = d.toLocaleDateString('en-DK', opts); | |
} else { | |
for (const k in months) { | |
date = date.replace(`. ${k} `, `-${months[k]}-`) | |
} | |
date = date.replace(/-/g, '/'); | |
} | |
const statement = stot(cur, '.transaction-field--statementText'); | |
const memoRegex = /\s+((?:Nota (?:nr. )?[A-Z0-9]+)|(?:Aftalenr. \d+))/; | |
var components = statement.split(memoRegex) | |
const payee = '"' + components[0] + '"'; | |
const memo = components.length > 1 ? '"' + components[1] + '"' : ''; | |
// Naively US-ify transaction amount. | |
const value = stot(cur, '.transaction-field--amount').replace('.','').replace(',', '.'); | |
const isOutflow = value.startsWith('-'); | |
const inflow = isOutflow ? '' : value; | |
const outflow = isOutflow ? value.substr(1) : ''; | |
return prev + date + ',' + payee + ',,' + memo + ',' + outflow + ',' + inflow + '\n'; | |
} | |
var csv = Array.prototype.reduce.call(document.querySelectorAll('.transaction-item.transaction-item--hasClick'), perItem, 'Date,Payee,Category,Memo,Outflow,Inflow\n'); | |
save(csv); |
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
javascript:(function(){ | |
const request = new XMLHttpRequest(); | |
const load = function() { | |
const script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.innerHTML = request.responseText; | |
document.getElementsByTagName('head')[0].appendChild(script); | |
}; | |
request.open("GET", "https://gist.githubusercontent.com/commonquail/3c1f9503034358fdbf0d679b8b8a2c08/raw/bank-to-ynab.js", true); | |
request.onload = load; | |
request.onerror = function() { alert("Something went wrong :("); }; | |
request.send(); | |
})(); |
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
Date | Payee | Category | Memo | Outflow | Inflow | |
---|---|---|---|---|---|---|
25/07/2010 | Sample Payee | Sample Category | Sample Memo for an outflow | 100.00 | ||
26/07/2010 | Sample Payee 2 | Sample Category | Sample memo for an inflow | 500.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment