Custom script for grab json data from web
Hi.
I need check currency exchange rate from particular bank service. I already successfully done it in google sheets but for some reasons I wish the Numbers, and have problem with this part. I put here part of javascript what done for me in google sheets, and ask you is it possible to make something here, or suggest something close to it what can easy initiate every time.
It's getting currency type and date and returning me rate for one working day before that date:
function rateNBP(currency,cellDate,boolDataStamp) {
var price
let date
for(let i = 1; i < 6; i++){
date = createDate(cellDate,i)
try{
let resp = UrlFetchApp.fetch(createLinkNBP(currency,date))
let content = resp.getContentText();
let jsonn = JSON.parse(content);
price = jsonn["rates"][0]['mid'];
break
}
catch(e){
continue
}
}
if(boolDataStamp){
price = price + " (" + date + ")"
}
return price
}
function createDate(date,howMuchTakeAway){
let result = new Date(date.getTime()-howMuchTakeAway*(24*3600*1000))
result = Utilities.formatDate(result,SpreadsheetApp.getActive().getSpreadsheetTimeZone(),"yyyy-MM-dd")
return result
}
function createLinkNBP(currency,date){
let baseOfString = 'http://api.nbp.pl/api/exchangerates/rates/'
let separator = '/'
let tableType = 'a'
let format = '?format=json'
let returnString = baseOfString + tableType + separator + currency + separator + date + separator + format
return returnString
}
}
MacBook Air 13″