LesterCovax / crypto-sheets

Google Sheets script editor code for managing a cryptocurrency tracking spreadsheet
https://www.reddit.com/r/cryptosheets
GNU General Public License v3.0
482 stars 67 forks source link

SyntaxError: Unexpected token: < (line 268, file "Code") #51

Open Jstorms1991 opened 5 years ago

Jstorms1991 commented 5 years ago

Just went step by step with the guide. Couldn't find any solution for the the error listed. Took a screenshot as well just in case. Thanks in advance.

Line 268 is listed below.

var data = JSON.parse(json);

Screen Shot 2019-07-15 at 11 16 19 PM

blackrandom commented 5 years ago

The API for coinmarketcap has changed, you need to make an account now and update the code to use the new format, instructions here [https://pro.coinmarketcap.com/migrate/]. Here's the changes I had to make to get this working:

Updated parsed data, starts at line 105 for me. The value 'price_btc' is not in the new API so it still shows as 'undefined' on the spreadsheet. Not sure if anyone can fix this, but I don't use it as much so I left it unchanged.

    ssRates.getRange('A'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['id']);
    ssRates.getRange('B'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['name']);
    ssRates.getRange('C'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['symbol']);
    ssRates.getRange('D'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['cmc_rank']);
    ssRates.getRange('E'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['price']);
    ssRates.getRange('F'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['price_btc']);
    ssRates.getRange('G'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['volume_24h']);
    ssRates.getRange('H'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['market_cap']);
    ssRates.getRange('I'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['circulating_supply']);
    ssRates.getRange('J'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['total_supply']);
    ssRates.getRange('K'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['max_supply']);
    ssRates.getRange('L'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['percent_change_1h']);
    ssRates.getRange('M'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['percent_change_24h']);
    ssRates.getRange('N'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['quote']['USD']['percent_change_7d']);
    ssRates.getRange('O'+(c).toString()).setValue(myCoinsObj[myCoins[i]]['last_updated']);

The updated getCoins() function, you can change the 'limit' value if you want to get all the coins, this uses up more credits with coinmarketcap. 100 works for me so that's what I left it as is.

function getCoins() {

  var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"

  requestOptions = {
    method: 'GET',
    muteHttpExceptions: true,
    qs: {
      'start': '1',
      'limit': '100',
      'convert': targetCurrency.toUpperCase()
    },
    headers: {
      'X-CMC_PRO_API_KEY': 'KEY'
    }
  }
  var response = UrlFetchApp.fetch(url, requestOptions);  
  var json = JSON.parse(response.getContentText());
  var data = json['data'];

  return data;
}