Skip to content

Instantly share code, notes, and snippets.

@bpwebs
Created February 27, 2022 16:42
Show Gist options
  • Save bpwebs/904953cfa222fa107357c1f36c026bf9 to your computer and use it in GitHub Desktop.
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
/**
* 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