Skip to content

Instantly share code, notes, and snippets.

@filiph
Created March 13, 2013 07:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save filiph/5150036 to your computer and use it in GitHub Desktop.
Save filiph/5150036 to your computer and use it in GitHub Desktop.
A simple script that fetches the Fio bank transaction data for you and pastes them into your spreadsheet. I'm afraid you're on your own while getting this to work for you, but here are the pre-requisites: 1) the Fio API token from the bank should be set in File > Project properties > User properties as `fioApiToken`. 2) The sheet 'rawData' must …
/**
* Fetch JSON from the API.
*/
function fetchLatestJson() {
var token = UserProperties.getProperty("fioApiToken");
if (token === null) {
throw "Fio API Token not set. Please go to File > Project properties > User properties and create token fioApiToken.";
}
var response = UrlFetchApp.fetch("https://www.fio.cz/ib_api/rest/last/"+token+"/transactions.json");
if (response.getResponseCode() !== 200) {
throw "Fio API request failed. Ensure your Fio API token is still valid and there is no outage.";
}
return response.getContentText();
}
/**
* Fio API returns JSON in a strange format. This function reverts each data.transactionList.transaction
* member into a 1D array.
*/
function createTransactionArrayFromApiObject(obj) {
return [
obj.column22 == null ? "" : obj.column22.value, // id
obj.column0 == null ? "" :obj.column0.value, // date
obj.column1 == null ? "" : obj.column1.value, // amount
obj.column14 == null ? "" : obj.column14.value,
obj.column2 == null ? "" : obj.column2.value,
obj.column3 == null ? "" : obj.column3.value,
obj.column4 == null ? "" : obj.column4.value, // KS
obj.column5 == null ? "" : obj.column5.value, // VS
obj.column6 == null ? "" : obj.column6.value, // SS
obj.column7 == null ? "" : obj.column7.value, // uzivatelska identifikace
obj.column16 == null ? "" : obj.column16.value, // zpráva pro příjemce
obj.column8 == null ? "" : obj.column8.value, // typ
obj.column9 == null ? "" : obj.column9.value, // provedl
"", // nazev protiuctu <-- FIX THIS
"" // nazev banky <-- FIX THIS
];
}
/**
* Go fetch data from API and write it into the spreadsheet.
*/
function updateFromFio() {
var json = fetchLatestJson();
var resp = Utilities.jsonParse(json);
data = resp.accountStatement;
if (data.transactionList === null) {
// no new data
} else {
// we have data, let's parse it
var transactionsData = data.transactionList.transaction;
var transactions = new Array(transactionsData.length);
for (var i = 0; i < transactionsData.length; i += 1) {
transactions[i] = createTransactionArrayFromApiObject(transactionsData[i]);
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("rawData");
var lastRow = sheet.getLastRow();
var blankRows = sheet.getMaxRows() - lastRow;
if (transactions.length > blankRows) {
sheet.insertRowsAfter(lastRow, transactions.length - blankRows + 1);
}
var range = sheet.getRange(lastRow + 1, 1, transactions.length, transactions[0].length);
range.setValues(transactions);
}
}
function debug() {
var token = UserProperties.getProperty("fioApiToken");
var response = UrlFetchApp.fetch("https://www.fio.cz/ib_api/rest/set-last-date/"+token+"/2010-07-29/");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment