The Artima Developer Community
Sponsored Link

Java Buzz Forum
GSpreadsheet: JavaScript Helper for Google Spreadsheets

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
GSpreadsheet: JavaScript Helper for Google Spreadsheets Posted: Jun 14, 2007 1:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: GSpreadsheet: JavaScript Helper for Google Spreadsheets
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

I am finding that more and more little applications that I have use Google Spreadsheets to store some data that I use in an Ajax app. After using the core API, you find yourself looking at fun code like foo.$t.

Most of the time I want a simple tabular view over a spreadsheet that has the first row as a header, and other rows as the data.

To do this I created GSpreadsheet which lets me do:

GSpreadsheet.load("pSYwzniwpzSFnt8Ix3ohQQA", { index: 'firstname' }, function(gs) {
  // display all
  document.getElementById("displayall").innerHTML = gs.displayAll();
      
  // show one
  var row = gs.select('Bob');
  document.getElementById("onebyindex").innerHTML = row.email;

  // show by row number
  row = gs.select(1);
  document.getElementById("onebyrownum").innerHTML = row.email;
      
  // display one row
  document.getElementById("displayrow").innerHTML = gs.displayRow('Bob');
});
You will see that you call GSpreadsheet.load(..., callback(takesAgsObject)) This is because of all of the asynchronous work going on. To get the JSON from the Spreadsheet back end you are always using the json-in-script output, and getting it by dynamically creating a script tag. The real dirty hack in this code is how to do that, and have the callback give you back the info to create the new object. To do this, I am creating a static method on the fly with eval() and calling into it passing in the right info. It's real ugly:
GSpreadsheet.load = function(key, options, callback) {
  if (!options['worksheet']) options['worksheet'] = 'od6';
  var worksheet = options['worksheet'];
  
  var callbackName = "GSpreadsheet.loader_" + key + "_" + worksheet;
  eval(callbackName + " = function(json) { var gs = new GSpreadsheet(key, json, options); callback(gs); }");
  
  var script = document.createElement('script');

  script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values' +
                        '?alt=json-in-script&callback=' + callbackName);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

Read: GSpreadsheet: JavaScript Helper for Google Spreadsheets

Topic: Using the Java Persistence API on the Desktop Previous Topic   Next Topic Topic: True Colors

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use