Romanovich095

Google Spreasheet Script

Oct 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getCurrencyRates() {
  2.   const updateTime = 12000;
  3.   const exchangeUrl = 'https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11';
  4.   const cache = CacheService.getScriptCache();
  5.  
  6.   if ((rates = cache.get('rates')) == null){
  7.     rates = UrlFetchApp.fetch(exchangeUrl).getContentText();
  8.     cache.put('rates', rates, 600);
  9.   }
  10.  
  11.   Logger.log(typeof JSON.parse(rates));
  12.   return JSON.parse(rates)[0];
  13. }
  14.  
  15. function convertUahToUsd(uah) {
  16.   if (!uah) { return Logger.log('Nothing has been passed as an argument to convert'); }
  17.  
  18.   const rates = getCurrencyRates();
  19.   return Math.ceil(uah /rates.sale);
  20. }
  21.  
  22. function convertUsdToUah(usd) {  
  23.   if (!usd) { return Logger.log('Nothing has been passed as an argument to convert'); }
  24.  
  25.   const rates = getCurrencyRates();
  26.   return Math.floor(rates.buy * usd);
  27. }
  28.  
  29. /** Triggers **/
  30. function onOpen() {
  31.   getCurrencyRates();
  32. }
  33.  
  34. function testConvertUahToUsd() {
  35.   getCurrencyRates();
  36.   convertUahToUsd(1000);
  37. }
  38.  
  39. function testConvertUsdToUah() {
  40.   getCurrencyRates();
  41.   convertUsdToUah(900);
  42. }
Add Comment
Please, Sign In to add comment