the following code works ok for me in GAS (Google Apps Script) for CoinMarketCap API
function cmcQuotes2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('base');
var lastRow = sheet.getLastRow();
var extension= lastRow - 2;
var oldAsset=sheet.getRange(2, 1, extension).getValues();
var rows=[];
var j;
for (j=0; j < oldAsset.length; j++) {
rows.push(oldAsset[j][0]);}
var apiKey = '*** your api key';
var params = {
'method' : 'GET',
'qs' : {'start': '1', 'limit': '100', 'convert': 'USD'},
'headers' : {'X-CMC_PRO_API_KEY': apiKey},
'muteHttpExceptions': true};
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
var data = UrlFetchApp.fetch(url, params);
var data = JSON.parse(data.getContentText());
var data = data.data;
var i;
for (i=0; i < data.length; i++) {
var asset = data[i].symbol;
var quote = data[i].quote.USD.price;
if (quote < 1) {var a = parseFloat(quote); var a = a.toFixed(8); var quote = parseFloat(a);}
if (quote >=1) {var a = parseFloat(quote); var a = a.toFixed(2); var quote = parseFloat(a);}
var row=rows.indexOf(asset)+2;
if (row>=2){
sheet.getRange(row, 2).setValue(quote);
}
}
}
but
no matter what I try to get the price quoted in EUR instead of USD, I can't.
I simply can't.
convert EUR instead of USD? Doesn't work.
var params = {
'method' : 'GET',
'qs' : {'start': '1', 'limit': '100', 'convert': 'EUR'},
'headers' : {'X-CMC_PRO_API_KEY': apiKey},
'muteHttpExceptions': true};
quote.EUR instead of quote.USD ?? Doesn't work ..
var quote = data[i].quote.EUR.price;
first case simply ignores EUR, second case it returns:
12:59:50
Errore
TypeError: Cannot read property 'price' of undefined
What's your take about it? thanks
params
of UrlFethApp.fetch(url, params)
, the property of qs
is not existing. I thought that this might be the reason of your issue.qs
is required to be used as the query parameters.When above points are reflected to your script, it becomes as follows.
var apiKey = '*** your api key';
var params = {
'method' : 'GET',
'qs' : {'start': '1', 'limit': '100', 'convert': 'USD'},
'headers' : {'X-CMC_PRO_API_KEY': apiKey},
'muteHttpExceptions': true};
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
var data = UrlFetchApp.fetch(url, params);
To:
var apiKey = '*** your api key';
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=100&convert=USD";
var params = {
'method': 'GET',
'headers': {'X-CMC_PRO_API_KEY': apiKey, 'Accept': 'application/json'},
'muteHttpExceptions': true
};
var data = UrlFetchApp.fetch(url, params);