Skip to content

Instantly share code, notes, and snippets.

@elvisciotti
Last active August 14, 2022 06:51
Show Gist options
  • Save elvisciotti/106eda8d0e6312ccc17ebd427beff79f to your computer and use it in GitHub Desktop.
Save elvisciotti/106eda8d0e6312ccc17ebd427beff79f to your computer and use it in GitHub Desktop.
Google spreadsheet function to calculate the value of a CryptoCurrency using coinmarketcap API
/**
* Return a cryptocurrency value in USD using coinmarketcap API
*
* @param string currency e.g. BTC, ETH
*
* @return float value
*/
function coinmarketcap(currency)
{
var body = UrlFetchApp.fetch("https://api.coinmarketcap.com/v1/ticker/");
var json = JSON.parse(body);
currencyToValue = {};
for (var i=0, l = json.length; i<l; i++) {
var row = json[i];
if (currency === row.symbol) {
return row.price_usd;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment