I am working on a tracker for my cryptocurrencies. In the following function I fetch data from an API and put in a cell. For instance, when I want to get the value of Bitcoin in USD I put the formula =getCryptodata("bitcoin", "price_usd") in a cell and it returns the current price.
function getCryptoData(coin, api) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'https://api.coinmarketcap.com/v1/ticker/' + coin + '/';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var data = JSON.parse(json);
return parseFloat(data[0][api]);
}
I want the price to be updated every minute, so I've set a trigger for the function. All of a sudden there seems to be a problem now. Every time the function is triggered this error shows up (in my mailbox).
TypeError: Cannot read property "(class)@35c554a5" from undefined.
When I run the script from the script editor I get the same problem. Then I came up with the idea to check whether the parameters in the function are defined or not, and if not, to give them a generic value. Google seemed to have a problem with the undefined parameters (although they are defined in the sheet). So I added these lines before the lines I already had in my function.
if (typeof coin == 'undefined') {
var coin = "bitcoin";
}
if (typeof api == 'undefined') {
var api = "price_usd";
}
else {
Now the error is gone but the values are not updated when the function is triggered.
What am I missing?