Skip to content

Instantly share code, notes, and snippets.

@th0ma5w
Created January 9, 2014 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th0ma5w/8327127 to your computer and use it in GitHub Desktop.
Save th0ma5w/8327127 to your computer and use it in GitHub Desktop.
JSON parsing script for BitCoin prices within Google Docs
// Adds a menu option to refresh all options.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Refresh",
functionName : "refreshLastUpdate"
}];
sheet.addMenu("Refresh", entries);
};
// Work-around for mobile docs, add the date to a cell and pass that cell as
// an unused variable to the functions in order to update on load
// You also have to schedule this to happen periodically for mobile to work.
// I set a trigger to run this function hourly.
function refreshLastUpdate() {
SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue(new Date().toTimeString());
}
// The following are several online services. Be polite!
// Pass the cell A1 as an unused parameter in order for mobile
// viewing of the spreadsheet to update values
//
// To use, put this in a cell.
//
// =BitPayPrice(A1)
function BitPayPrice(dt) {
var result = UrlFetchApp.fetch("https://bitpay.com/api/rates");
var o = JSON.parse(result.getContentText());
var price = 0;
for (var i in o) {
d = o[i];
if (d.code="USD") {
price = d.rate;
//Logger.log(d.rate);
break;
}
}
return price;
SpreadsheetApp.flush(); // note, possible unnecessary voodoo
}
function CoinBaseBuyPrice(dt) {
var result = UrlFetchApp.fetch("https://coinbase.com/api/v1/prices/buy");
var o = JSON.parse(result.getContentText());
return o.amount;
SpreadsheetApp.flush(); // note, possible unnecessary voodoo
}
function CoinBaseSellPrice(dt) {
var result = UrlFetchApp.fetch("https://coinbase.com/api/v1/prices/sell");
var o = JSON.parse(result.getContentText());
return o.amount;
SpreadsheetApp.flush(); // note, possible unnecessary voodoo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment