Last active
December 6, 2021 17:47
-
-
Save gmcdev/d567b14ec4c56ea237174a204a231183 to your computer and use it in GitHub Desktop.
Map current cryptocurrency prices into Google Sheets
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
/* USAGE: | |
* Sheet -> Tools -> Script Editor... | |
* Paste this script | |
* Update the map (below) to your preferences | |
* Create a button in your Sheet and Assign Script: `test` | |
* et voila profit | |
*/ | |
function test() { | |
// maps currencies.tokens to sheet ranges | |
getPrices({ | |
'USD': { | |
'ETH': 'G6', | |
'DASH': 'H6', | |
'LTC': 'I6', | |
'GNT': 'J6', | |
'REP': 'K6', | |
'BAT': 'L6' | |
} | |
}); | |
} | |
function getPrices(model) { | |
for (var currency in model) { | |
var tokens = Object.keys(model[currency]).toString(); | |
var url = 'https://min-api.cryptocompare.com/data/price?fsym=' + currency + '&tsyms=' + tokens; | |
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); | |
var json = JSON.parse(response.getContentText()); | |
for (var token in model[currency]) { | |
updatePrice( | |
model[currency][token], | |
json[token] | |
); | |
} | |
} | |
} | |
function updatePrice(range, price) { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.getRange(range).setValue(1/price); | |
} |
if thanks for the code. i am getting #NUM! as a result when i pull data. can you help to solve this. thanks (actually i am new to this)
when i run updateprice function , it give me following error
9:47:07 PM Error
Exception: Argument cannot be null: a1Notation
updatePrice @ Crypto Prices live.gs:35
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfectly and also refreshes automatically on triggered events unlike every other script out there. Beautiful code. Thanks Greg!