Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thisismorgan/c2b70743862cce08c67ea1820fadf6ce to your computer and use it in GitHub Desktop.
Save thisismorgan/c2b70743862cce08c67ea1820fadf6ce to your computer and use it in GitHub Desktop.
Tracking Changes in Cryptocurrency Value
function todaysDateAsString() {
var today = new Date();
var todayMonth = today.getMonth() + 1;
var todayDay = today.getDate();
var todayYear = today.getFullYear();
return `${todayMonth}/${todayDay}/${todayYear}`;
}
function recordCryptoPrices() {
QUANTITY_COLUMN = 'C';
PRICE_COLUMN = 'D';
VALUE_COLUMN = 'E';
CRYPTO_ROWS = {
'ETH' : '2',
'BTC' : '3',
'FIL' : '4',
'DOGE' : '5',
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Crypto');
var response = UrlFetchApp.fetch(
`https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${Object.keys(CRYPTO_ROWS).join(',')}`,
{
'method' : 'GET',
'contentType': 'application/json',
'headers' : {
'X-CMC_PRO_API_KEY' : `${COINMARKETCAP_API_KEY}`,
}
}
);
var json = response.getContentText();
var data = JSON.parse(json);
var values = [todaysDateAsString()];
for (let [symbol, row] of Object.entries(CRYPTO_ROWS)) {
var currentPrice = data['data'][symbol]['quote']['USD']['price'];
var currentValue = sheet.getRange(QUANTITY_COLUMN + row).getValue() * currentPrice;
sheet.getRange(PRICE_COLUMN + row).setValue(currentPrice);
sheet.getRange(VALUE_COLUMN + row).setValue(currentValue);
values.push(currentValue);
}
sheet.appendRow(values);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment