diff --git a/README.md b/README.md index 3ccd316..56529a3 100755 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ You pass in either `key` as the actual spreadsheet key, or just the full publish `callbackContext` sets the `this` for your callback. It's the tabletop object by default. +`prettyColumnNames` can be true or false (default to false). It adds an object `pretty_columns` as a sibling to `column_names` which contains human-readable column names. + ### Tabletop itself Once you've initialized a `tabletop` object you can access its good parts. diff --git a/src/tabletop.js b/src/tabletop.js index 6533df2..6e28497 100755 --- a/src/tabletop.js +++ b/src/tabletop.js @@ -69,6 +69,7 @@ this.singleton = !!options.singleton; this.simple_url = !!options.simple_url; this.callbackContext = options.callbackContext; + this.prettyColumnNames = !!options.prettyColumnNames; if(typeof(options.proxy) !== 'undefined') { // Remove trailing slash, it will break the app @@ -367,6 +368,39 @@ if(ttIndexOf(this.model_names, model.name) === -1) { this.model_names.push(model.name); } + if (this.prettyColumnNames) { + var cellurl = data.feed.link[3].href.replace('/feeds/list/', '/feeds/cells/').replace('https://spreadsheets.google.com', ''); + this.requestData(cellurl, this.loadPrettyColumnNames); + } else { + this.sheetsToLoad--; + if(this.sheetsToLoad === 0) + this.doCallback(); + } + }, + + /* + * Store column names as an object + * with keys of Google-formatted "columnName" + * and values of uman-readable "Column name" + */ + loadPrettyColumnNames: function(data) { + var pretty_columns = {}; + + var column_names = this.models[data.feed.title.$t].column_names; + + var i = 0; + var l = column_names.length; + + for (; i < l; i++) { + if (typeof data.feed.entry[i].content.$t !== 'undefined') { + pretty_columns[column_names[i]] = data.feed.entry[i].content.$t; + } else { + pretty_columns[column_names[i]] = column_names[i]; + } + } + + this.models[data.feed.title.$t].pretty_columns = pretty_columns; + this.sheetsToLoad--; if(this.sheetsToLoad === 0) this.doCallback();