Estoy tratando de llamar datos de Coin Market Cap a una hoja de Google a través de Apps Script. No entiendo el TypeError que estoy haciendo aquí.
Aquí está el código:
function getCryptoPrice() { var sh1=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EPS data CMC"); var url="https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=BTC" var requestOptions = { method: 'GET', uri: 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest', qs: { start: 1, limit: 5000, convert: 'USD' }, headers: { 'X-CMC_PRO_API_KEY': '***********hidden*************' }, json: true, gzip: true }; var httpRequest= UrlFetchApp.fetch(url, requestOptions); var getContext= httpRequest.getContentText(); var parseData=JSON.parse(getContext); sh1.getRange(1, 2).setValue(parseData.data.BTC.quote.USD.price) }Y el error:
TypeError: Cannot read property 'USD' of undefined getCryptoPrice @ Code.gs:24Palabras clave : hoja de google, script de aplicaciones, capitalización de mercado de monedas, cotización, último, V2, api,
Está recibiendo ese error porque parseData.data.BTC está devolviendo una array .
Intenta cambiar esto a: -
sh1.getRange(1, 2).setValue(parseData.data.BTC.quote.USD.price)este
sh1.getRange(1, 2).setValue(parseData.data.BTC[0].quote.USD.price)