« Custom Search Engine that automatically knows what to site restrict |
Main
| Browser User Agent Strings »
GSpreadsheet: JavaScript Helper for Google Spreadsheets
Posted by:
Dion Almaer
on 06/13/2007
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);
}
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);
}
About Dion Almaer
Dion Almaer is the founder and CTO of Adigio, Inc. He is an architect, mentor, pragmatic, and evangelist of technologies such as J2EE, JDO, AOP, and Groovy. He is the Editor-in-Chief of TheServerSide.com J2EE Community and enjoys working in the community. He is a member of the Java Community Process, where he participates on various expert groups.
More About Dion »Why Attend the NFJS Tour?
- » Cutting-Edge Technologies
- » Agile Practices
- » Peer Exchange
Current Topics:
- Languages on the JVM: Scala, Groovy, Clojure
- Enterprise Java
- Core Java, Java 7
- Agility
- Testing: Geb, Spock, Easyb
- REST
- NoSQL: MongoDB, Cassandra
- Hadoop
- Spring 3
- Automation Tools: Git, Hudson, Sonar
- HTML5, Ajax, jQuery, Usability
- Mobile Applications - iPhone and Android
- More...
NFJS, the Magazine
December Issue Now AvailableBDD and REST
by Brian SlettenMocks and Stubs in Groovy Tests
by Kenneth KousenAlgorithms for Better Text Search Results
by John GriffinKnowns and Unknowns of Scrum and Agile
by Brian Tarbox

