Create a gist now

Instantly share code, notes, and snippets.

Google Script snippet to demonstrate how a Google Spreadsheet can access account data from Blueleaf using the Blueleaf API
// 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