Created
November 1, 2010 13:54
-
-
Save darwin/658202 to your computer and use it in GitHub Desktop.
Podpora sluzeb CNB pro Google Spreadsheets
This file contains hidden or 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
// Podpora sluzeb CNB pro Google Spreadsheets | |
// autor: [email protected] | |
// ========================================== | |
// | |
// =historicalRate(currency, date, zone) | |
// ------------------------------------- | |
// currency je kod meny (USD, EUR, apod.) | |
// date je ve formatu d.m.r (pokud neni date zadano, pak se vrati aktualni kurz) | |
// zone je volitene a uplatni se pro konverzi date na string (pokud neni zadana pouzije se CET) | |
// | |
// priklad API volani: | |
// http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt?date=8.9.2010 | |
// ------ | |
// 08.09.2010 #175 | |
// země|měna|množství|kód|kurz | |
// Austrálie|dolar|1|AUD|17,848 | |
// Brazílie|real|1|BRL|11,263 | |
// Bulharsko|lev|1|BGN|12,634 | |
// Čína|renminbi|1|CNY|2,864 | |
// Dánsko|koruna|1|DKK|3,319 | |
// EMU|euro|1|EUR|24,710 | |
// Estonsko|koruna|1|EEK|1,579 | |
// Filipíny|peso|100|PHP|43,937 | |
// Hongkong|dolar|1|HKD|2,504 | |
// Chorvatsko|kuna|1|HRK|3,395 | |
// Indie|rupie|100|INR|41,714 | |
// Indonesie|rupie|1000|IDR|2,162 | |
// Japonsko|jen|100|JPY|23,220 | |
// Jihoafrická rep.|rand|1|ZAR|2,678 | |
// Jižní Korea|won|100|KRW|1,660 | |
// Kanada|dolar|1|CAD|18,563 | |
// Litva|litas|1|LTL|7,156 | |
// Lotyšsko|lat|1|LVL|34,852 | |
// Maďarsko|forint|100|HUF|8,552 | |
// Malajsie|ringgit|1|MYR|6,257 | |
// Mexiko|peso|1|MXN|1,493 | |
// MMF|SDR|1|XDR|29,425 | |
// Norsko|koruna|1|NOK|3,135 | |
// Nový Zéland|dolar|1|NZD|14,015 | |
// Polsko|zlotý|1|PLN|6,252 | |
// Rumunsko|nové leu|1|RON|5,763 | |
// Rusko|rubl|100|RUB|62,983 | |
// Singapur|dolar|1|SGD|14,473 | |
// Švédsko|koruna|1|SEK|2,665 | |
// Švýcarsko|frank|1|CHF|19,245 | |
// Thajsko|baht|100|THB|62,713 | |
// Turecko|nová lira|1|TRY|12,809 | |
// USA|dolar|1|USD|19,460 | |
// Velká Británie|libra|1|GBP|30,073 | |
function historicalRate(currency, date, zone) { | |
currency = currency.toUpperCase(); | |
if (!zone) zone = "CET"; | |
var api = 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt'; | |
var params = ""; | |
if (date) { | |
if (typeof date != "string") { | |
// expect Date object and convert it to string | |
date = Utilities.formatDate(date, zone, "d.M.yyyy"); | |
} | |
params = "?date="+date; | |
} | |
var response = UrlFetchApp.fetch(api+params); | |
var data = response.getContentText().split("\n"); | |
for (var i=2; i<data.length; i++) { | |
var row = data[i]; // 'USA|dolar|1|USD|19,460' | |
var parts = row.split("|"); | |
var cur = parts[3]; | |
var val = parts[4]; | |
if (cur == currency) { | |
if (!val) val = "0"; | |
val = val.replace(",", "."); | |
return parseFloat(val); | |
} | |
} | |
return "?"; | |
} | |
function _test() { | |
var ok = 0; | |
var failed = 0; | |
var _check = function(params, expected) { | |
var res = historicalRate.apply(this, params); | |
if (res===expected) { | |
Logger.log("=historicalRate("+params.join(",")+") => "+res); | |
ok++; | |
} else { | |
Logger.log("=historicalRate("+params.join(",")+") => "+res+" FAILED! expected:"+expected); | |
failed++; | |
} | |
} | |
Logger.log("Running CNB script tests..."); | |
// string date | |
_check(["USD", "8.9.2010"], 19.46); | |
_check(["EUR", "8.9.2010"], 24.71); | |
// real date | |
_check(["USD", new Date(2010,8,8)], 19.46); // the month is 0-based | |
// real date + zone, tady to chce vysvetleni: | |
// apps scripty jsou spousteny na serverech google, | |
// ktere v mem pripade pracuji v casove zone GMT-0700 (PDT) | |
// vyraz new Date(2010,8,7,23,0,0) vytvori datum 8.9.2010, 23:00 v PDT | |
// ja ho chci ale zobrazit v casove zone CET, coz znamena GMT+1 | |
// a tudiz lokalni datum 8.9.2010 7:00 v CET | |
_check(["USD", new Date(2010,8,7,23,0,0), "CET"], 19.46); // the month is 0-based | |
// invalid currency | |
_check(["XXX", "8.9.2010"], "?"); | |
Logger.log("------------"); | |
if (failed) { | |
Logger.log("Failed "+failed+" tests from "+(ok+failed)); | |
} else { | |
Logger.log("All "+ok+" tests OK"); | |
} | |
} | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super :) Diky!