From f00a3a712ee9dbe6f43d8a34d42d6fec1aa60311 Mon Sep 17 00:00:00 2001 From: Krzysztof Trzewiczek Date: Tue, 7 Aug 2012 12:09:05 +0200 Subject: [PATCH] Worksheets managed in GDoc Backend --- src/backend.gdocs.js | 16 ++++++++-------- test/backend.gdocs.test.js | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/backend.gdocs.js b/src/backend.gdocs.js index 5901b347..2b321d67 100644 --- a/src/backend.gdocs.js +++ b/src/backend.gdocs.js @@ -57,8 +57,8 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {}; // :return: tabular data object (hash with keys: field and data). // // Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows. - my.parseData = function(gdocsSpreadsheet) { - var options = arguments[1] || {}; + my.parseData = function(gdocsSpreadsheet, options) { + var options = options || {}; var colTypes = options.colTypes || {}; var results = { fields : [], @@ -87,7 +87,6 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {}; var value = entry[_keyname].$t; var num; - // TODO decide the entry format of percentage data to be parsed // TODO cover this part of code with test // TODO use the regexp only once // if labelled as % and value contains %, convert @@ -107,15 +106,16 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {}; // Convenience function to get GDocs JSON API Url from standard URL my.getSpreadsheetAPIUrl = function(url) { - // https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0 - var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/; + // https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=YYY + var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*gid=([\d]+).*/; var matches = url.match(regex); var key; - // TODO check possible worksheet options - var worksheet = 1; + var worksheet; if(!!matches) { - key = matches[1]; + key = matches[1]; + // the gid in url is 0-based and feed url is 1-based + worksheet = parseInt(matches[2]) + 1; url = 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json'; } return url; diff --git a/test/backend.gdocs.test.js b/test/backend.gdocs.test.js index f5a042c7..210e0710 100644 --- a/test/backend.gdocs.test.js +++ b/test/backend.gdocs.test.js @@ -191,9 +191,11 @@ test("GDocs Backend", function() { test("GDocs Backend.getUrl", function() { var key = 'Abc_dajkdkjdafkj'; - var url = 'https://docs.google.com/spreadsheet/ccc?key=' + key + '#gid=0' + var gid = 0; + var worksheet = 1; + var url = 'https://docs.google.com/spreadsheet/ccc?key=' + key + '#gid=' + gid var out = recline.Backend.GDocs.getSpreadsheetAPIUrl(url); - var exp = 'https://spreadsheets.google.com/feeds/list/' + key + '/1/public/values?alt=json' + var exp = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values?alt=json' equal(exp, out); });