How do I retrieve data related to a crypto token using numbers as an identifier?

80 views Asked by At

I've been using Google Sheets to track the value of my various crypto currencies but ran into an issue I'm unable to resolve. The issue is that I have a new crypto currency called 00 (aka L00p) but when I use my regular code to grab the value from CoinMarketCap I get the following error:

"Syntax error: SyntaxError: missing ) after argument list line: 27 file: Code.gs"

I believe the issue is that the currency/token identifier is two digits and not letter-based.

My code looks like this:

    function getCryptoPrice() {
  var sh2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var sh3=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
  
  var apiKey=sh2.getRange(1, 2).getValue();
  
  var url="https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=00"
var requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
  qs: {
    start: 1,
    limit: 5000,
    convert: 'USD'
  },
  headers: {
    'X-CMC_PRO_API_KEY': apiKey
  },
  json: true,
  gzip: true
};
  
  var httpRequest= UrlFetchApp.fetch(url, requestOptions);
  var getContext= httpRequest.getContentText();
  
  var parseData=JSON.parse(getContext);
  sh3.getRange(48, 2).setValue(parseData.data.00.quote.USD.price)

}

For comparisons sake, here's the script I used as a template, and which works without issue:

function getCryptoPrice() {
  var sh2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var sh3=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
  
  var apiKey=sh2.getRange(1, 2).getValue();
  
  var url="https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=XCN"
var requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
  qs: {
    start: 1,
    limit: 5000,
    convert: 'USD'
  },
  headers: {
    'X-CMC_PRO_API_KEY': apiKey
  },
  json: true,
  gzip: true
};
  
  var httpRequest= UrlFetchApp.fetch(url, requestOptions);
  var getContext= httpRequest.getContentText();
  
  var parseData=JSON.parse(getContext);
  sh3.getRange(47, 2).setValue(parseData.data.XCN.quote.USD.price)

}

It seems as though the 00 on row 27 in the 00-script appears to be "un-tethered" from the 00 on row 7. And there's no missing end parenthesis, as the error code claims.

Any ideas?

1

There are 1 answers

0
Soren The Swede On

@TheWizEd, thank you!

After changing the code per TheWizEd's suggestion it now works. The thing that tricked me on my first attemt was that I though TheWizEd had forgot to enter a period in after "data", so I added one before testing. That was the wrong thing to do. I also thought that I had to make a similar change on row 7, but that was also not the case.

The complete - and functioning - code looks like this:

function getCryptoPrice() {
  var sh2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var sh3=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
  
  var apiKey=sh2.getRange(1, 2).getValue();
  
  var url="https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=00"
var requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
  qs: {
    start: 1,
    limit: 5000,
    convert: 'USD'
  },
  headers: {
    'X-CMC_PRO_API_KEY': apiKey
  },
  json: true,
  gzip: true
};
  
  var httpRequest= UrlFetchApp.fetch(url, requestOptions);
  var getContext= httpRequest.getContentText();
  
  var parseData=JSON.parse(getContext);
  sh3.getRange(48, 2).setValue(parseData.data["00"].quote.USD.price)

}

Again, huge thank you to TheWizEd!