I’m trying to call this API response using a custom function where I can change the parameters. This is my code:
function callCandles(pair, start, end) { var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:" + "pair" + "/hist?limit=1000&start=" + "start" +"&end=" +"end" +"&sort=-1"); var fact = JSON.parse(response.getContentText()); //parse the data from the API and store it in the variable data and convert response to text format// return fact; }
This is what I’m typing in the spreadsheet:
=callCandles(tBTCUSD,"1577841154000","1606785154000")
But when I do it I get a “reference does not exist” error.
Thank you.
Advertisement
Answer
Please modify as follows and test it again.
Modified script:
function callCandles(pair, start, end) { var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:" + pair + "/hist?limit=1000&start=" + start + "&end=" + end + "&sort=-1"); // Modified var fact = JSON.parse(response.getContentText()); //parse the data from the API and store it in the variable data and convert response to text format// return fact; }
- Please use
pair
,start
andend
as the variables. - I thought that the reason of your error message is due to this. And also, please modify as follows.
Modified formula:
=callCandles("tBTCUSD","1577841154000","1606785154000")
- Please use
"tBTCUSD"
as a string enclosed by"
.
Note:
- For example, when
pair
is used as"pair"
,pair
is the string value. By this, even when=callCandles("tBTCUSD","1577841154000","1606785154000")
is used,"tBTCUSD"
is not used and"pair"
is used. - When
=callCandles(tBTCUSD,"1577841154000","1606785154000")
is used, in this case,tBTCUSD
is used as the named range. By this, when there is no named range oftBTCUSD
,#NAME?
is returned. By this,pair
offunction callCandles(pair, start, end) {
is#NAME?
. Please be careful this.