Skip to content
Advertisement

reference does not exist with custom function – google script

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 and end 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 of tBTCUSD, #NAME? is returned. By this, pair of function callCandles(pair, start, end) { is #NAME?. Please be careful this.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement