Skip to content

Instantly share code, notes, and snippets.

@sfelde
Last active August 29, 2015 14:08
Show Gist options
  • Save sfelde/9a5cfd653d77e681c042 to your computer and use it in GitHub Desktop.
Save sfelde/9a5cfd653d77e681c042 to your computer and use it in GitHub Desktop.
// Blueleaf API Integration with Google Spreadsheet using Google Scripts to build
// custom spreadsheet functions to retrieve (query only) account data from Blueleaf.
//
// This script code is provided AS IS for demonstration purposes; no warranty of its
// robustness nor any guarantee it will work in your Google Apps environment is
// provided -- Jim Koch 2014
//
// When debugging, use the Logger function below to check the data fields available
// from Blueleaf.
//
// var doc = Xml.parse(response.getContentText(), true);
// Logger.log(response.getContentText()); // Log the data returned from the server
// var holdings = doc.household.accounts.account.holdings.getElsements("holding");//
//
// Return the current number of shares held (rounded) for specific holding symbol
function GetShareCountFromBlueleaf(id, accountname, symbol, token) {
// Set the vars
var unamepass = token + ":";
var digest = Utilities.base64Encode(unamepass);
var digestfull = "Basic " + digest;
// Get the data
var response = UrlFetchApp.fetch("https://secure.blueleaf.com/api/v1/households/" + id + ".xml",
{
method: "get",
headers: {"Authorization": digestfull}
});
// Parse the XML
var doc = Xml.parse(response.getContentText(), true);
Logger.log(response.getContentText()); // Log the data returned from the server
// Get the accounts
var accounts = doc.household.accounts.getElements("account");
for (var i in accounts) {
if (accounts[i].name.getText() == accountname) {
// Get the holdings
var holdings = accounts[i].holdings.getElements("holding");
// If found, get the holdings information
for (var j in holdings) {
if (holdings[j].ticker_name.getText() == symbol) {
return Math.round(holdings[j].quantity.getText());
}
}
}
}
// Not found
return "";
}
// Return the current value of the account's money market sweep fund assuming $1 price
function GetCashValueFromBlueleaf(id, accountname, symbol, token) {
// Set the vars
var unamepass = token + ":";
var digest = Utilities.base64Encode(unamepass);
var digestfull = "Basic " + digest;
// Get the data
var response = UrlFetchApp.fetch("https://secure.blueleaf.com/api/v1/households/" + id + ".xml",
{
method: "get",
headers: {"Authorization": digestfull}
});
// Parse the XML
var doc = Xml.parse(response.getContentText(), true);
Logger.log(response.getContentText()); // Log the data returned from the server
// Get the accounts
var accounts = doc.household.accounts.getElements("account");
for (var i in accounts) {
if (accounts[i].name.getText() == accountname) {
// Get the holdings
var holdings = accounts[i].holdings.getElements("holding");
// If found, get the holdings information
for (var j in holdings) {
if (holdings[j].ticker_name.getText() == symbol) {
return Math.round(holdings[j].value.getText());
}
}
}
}
// Not found
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment