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+

Are you interested in this topic? Follow me on Twitter, Google+ or subscribe RSS

7 comments:

  1. I ordered a replica gucci bag here. When I received the bag, it surprised me a lot.replica gucci handbags This bag is perfect and the quality is exactly the same.fake high quality gucci womens totes A dazzling masonry. I introduced this website to my friends, and my friends liked it very much.

    ReplyDelete
  2. I bought this watch for my dad on this website on Father's Day.Iwc Watches Resale Value This watch has been bought since my father has been wearing it.Fake Watches Rappers It has been running and working very hard. cartier Watches For WomenThe key is that it looks no different from genuine and the design is perfect. He likes it very much.Luxury Watches Rimini

    ReplyDelete
  3. i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. niche adults relevant

    ReplyDelete
  4. This is really very nice post you shared, i like the post, thanks for sharing.. Inexpensive Domain Registration

    ReplyDelete
  5. Best transport company from Dammam to Riyadh
    The preference of the best transport company from Dammam to Riyadh over other companies is due to a number of factors:
    شركة نقل عفش
    شركة نقل عفش من الرياض الى الدمام
    شركة نقل اثاث من الرياض الى الدمام
    شركة نقل عفش بالقصيم

    ReplyDelete