Sunday, April 21, 2013

How to get data from Jawbone UP and insert to Spreadsheet by Google Apps Script

A two days ago I've purchased Jawbone UP. It can track my life (sleeping, running walking, and eating habits). I'm data addict, so I was looking for way to get data from this incredible (internet) thing. I found out unofficial Jawbone UP API (http://eric-blue.com/projects/up-api/). Its look like better way instead of "ofiicial CSV" which can be downloaded from website. So I've created Google Apps Script library to download data from Jawbone and insert to Spreadsheet.

In short tutorial I will describe how to install script and get data for specify day. You can create your own script for automatization e.g. post to Twitter, create charts or reports.


For GAS novice

1) Create a new Google Spreadsheet. Open Script Editor (from menu Tools > Script editor)




2) In a new window select Blank project


3) In Script Editor select Resources > Manage libraries


4) To input box on the bottom insert M6ZtEUVi85vR5n2ozP3eYNFeS6DrsjUUV 
(it is unique id of Google Apps Script library). If it is succesfull loaded, select last version. Click to button Save.



5) Copy below code and insert to Google Apps Script editor. You have to replace your email and password

var EMAIL = "your@email.com";
var PASSWORD  = "*****";

function getStatistics() {
var token =  JawboneUp.getToken(EMAIL,PASSWORD);
var from = new Date(2013,03,21,8,00,00,000); // get data from date 21.4.2013 8:00
var to = new Date(2013,03,21,23,00,00,000); // to date 21.4.2013 23:00
var data = JawboneUp.getActivity(from,to, token);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var header = [ "time", "distance", "active_time", "calories", "steps", "aerobic", "speed" ];
var rows = []
rows.push(header);
for (i in data) {
var row = []
for( j in header) {
  var val = data[i].value[header[j]];
  if (header[j] == "time") val = new Date( parseInt(val,10) *1000)
  row.push( val )
} 
rows.push(row);
} 
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows)
} 



6)  Run copied code (Run > getStatistics ) First you should approve data access and then run again.


Thats all - now you can see result in Spreadsheet.



My report is showed below.  I've add chart (Insert > Chart) and change row color.



For GAS ninjas

  • Library has two method
    • getToken(EMAIL, PASSWORD)
    • getActivity(from,to, token) - from and to are Date() objects
  • Token can be store in UserProperites / Spreadsheet or ScriptDB, and you save one request each time
  • I'm planning to add more methods in the future. Stay tunned and follow me Google+