Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getCurrencyRates() {
- const updateTime = 12000;
- const exchangeUrl = 'https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11';
- const cache = CacheService.getScriptCache();
- if ((rates = cache.get('rates')) == null){
- rates = UrlFetchApp.fetch(exchangeUrl).getContentText();
- cache.put('rates', rates, 600);
- }
- Logger.log(typeof JSON.parse(rates));
- return JSON.parse(rates)[0];
- }
- function convertUahToUsd(uah) {
- if (!uah) { return Logger.log('Nothing has been passed as an argument to convert'); }
- const rates = getCurrencyRates();
- return Math.ceil(uah /rates.sale);
- }
- function convertUsdToUah(usd) {
- if (!usd) { return Logger.log('Nothing has been passed as an argument to convert'); }
- const rates = getCurrencyRates();
- return Math.floor(rates.buy * usd);
- }
- /** Triggers **/
- function onOpen() {
- getCurrencyRates();
- }
- function testConvertUahToUsd() {
- getCurrencyRates();
- convertUahToUsd(1000);
- }
- function testConvertUsdToUah() {
- getCurrencyRates();
- convertUsdToUah(900);
- }
Add Comment
Please, Sign In to add comment