Created
February 27, 2022 16:42
-
-
Save bpwebs/904953cfa222fa107357c1f36c026bf9 to your computer and use it in GitHub Desktop.
Getting crypto price updates to Google Sheets from CoinMarketCap with Google Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CRYPTO PORTFOLIO TRACKER | |
* CREATE BY: www.bpwebs.com | |
*/ | |
const SHEET_NAME = 'Crypto Portfolio'; | |
const RN = 7; //The row number of the first coin the coins table | |
function getCryptoUpdates() { | |
const ss = SpreadsheetApp.getActive(); | |
const sheet = ss.getSheetByName(SHEET_NAME); | |
const coinmarketcap_url = sheet.getRange('B2').getValue(); | |
const coinmarketcap_api_key = sheet.getRange('B3').getValue(); | |
let noOfCoins = sheet.getRange('B4').getValue(); | |
let coins = sheet.getRange('A7:A'+(RN+noOfCoins-1)).getValues().filter(String); | |
let options = { | |
method: 'GET', | |
headers: {'X-CMC_PRO_API_KEY': coinmarketcap_api_key }, | |
} | |
let responses = []; | |
coins.forEach(function(e){ | |
let result = UrlFetchApp.fetch(coinmarketcap_url+'?symbol='+e.toString(),options); | |
let text = result.getContentText(); | |
let d = JSON.parse(text); | |
responses.push(d.data[e.toString()].quote.USD.price); | |
}); | |
for(let i=RN;i<responses.length+RN;i++) { | |
sheet.getRange('B'+i).setValue(responses[i-RN]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment