Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Fix determination of worksheet ID for new sheets #48

Merged
merged 2 commits into from
Apr 27, 2014

Conversation

ghing
Copy link
Contributor

@ghing ghing commented Apr 26, 2014

Tabletop.loadSheets uses this logic to determine each worksheet's ID from the worksheets feed (https://spreadsheets.google.com/feeds/worksheets/{{SPREADSHEET_KEY}}/public/basic?alt=json):

var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3);

For old sheets, data.feed.entry.links has 4 entries:

link: [
  {
    rel: "http://schemas.google.com/spreadsheets/2006#listfeed",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/list/{{SPREADSHEET_KEY}}/od6/public/basic"
  },
  {
    rel: "http://schemas.google.com/spreadsheets/2006#cellsfeed",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/cells/{{SPREADSHEET_KEY}}/od6/public/basic"
  },
  {
    rel: "http://schemas.google.com/visualization/2008#visualizationApi",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/tq?key={{SPREADSHEET_KEY}}&sheet=od6&pub=1"
  },
  {
    rel: "self",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/worksheets/{{SPREADSHEET_KEY}}/public/basic/od6"
  }
]

and the code correctly extracts od6 from the href property of the last link element.

For new sheets, however, data.feed.entry.links has 5 entries:

link: [
  {
    rel: "http://schemas.google.com/spreadsheets/2006#listfeed",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/list/{{SPREADSHEET_KEY}}/od6/public/basic"
  },
  {
    rel: "http://schemas.google.com/spreadsheets/2006#cellsfeed",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/cells/{{SPREADSHEET_KEY}}/od6/public/basic"
  },
  {
    rel: "http://schemas.google.com/visualization/2008#visualizationApi",
    type: "application/atom+xml",
    href: "https://docs.google.com/spreadsheets/d/{{SPREADSHEET_KEY}}/gviz/tq?gid=0&pub=1"
  },
  {
    rel: "http://schemas.google.com/spreadsheets/2006#exportcsv",
    type: "text/csv",
    href: "https://docs.google.com/spreadsheets/d/{{SPREADSHEET_KEY}}/export?gid=0&format=csv"
  },
  {
    rel: "self",
    type: "application/atom+xml",
    href: "https://spreadsheets.google.com/feeds/worksheets/{{SPREADSHEET_KEY}}/public/basic/od6"
  }
]

So, the code will incorrectly grab csv from the end of the fourth link element, instead of od6 from the end of the last link element.

While this method of extracting the worksheet ID seems to be too brittle in general and might be worth reconsidering, we can make the existing method work for both old and new sheets by inspecting the last link element instead of the fourth:

var linkIdx = data.feed.entry[i].link.length-1;
var sheet_id = data.feed.entry[i].link[linkIdx].href.substr( data.feed.entry[i].link[linkIdx].href.length - 3, 3);

Without this fix, we get the error "Cannot read property 'title' of undefined" in Tabletop.Model as a result of no data being retrieved from an incorrectly constructed sheet list feed URL (https://spreadsheets.google.com/feeds/list/{{SPREADSHEET_KEY}}/csv/public/values?alt=json).

Note that this pull request also contains the change for #46

The spreadsheets API endpoint returns an HTTP 400 error with a
message of "Invalid query parameter value for sq." when the
``sq`` query parameter is empty.  The default case for Tabletop
is an empty query.  Only add the ``sq`` parameter to the generated
URL if ``this.query`` is not empty.

Addresses jsoma#45
Tabletop.loadSheets looks at the ``data.feed.entry[i].link``
elements to determine the worksheet ID for each worksheet. With
old sheets, the worksheet ID can be parsed from the fourth link
element.  However, with new sheets, there are five entries in
``data.feed.entry[i].link``.

In either case, we can parse the worksheet ID from the last
element in ``data.feed.entry[i].link``.  So, use this last element
instead of hard-coding access to the fourth (index 3) element.
ghing added a commit to workdept/affirmations-referral-network that referenced this pull request Apr 26, 2014
I had to make some changes to tabletop in order to support new
Google sheets.

See jsoma/tabletop#48

Addresses https://pm.theworkdept.com/issues/3861
@ghing
Copy link
Contributor Author

ghing commented Apr 26, 2014

I'll add that support for new sheets is increasingly important to me because now, when I create a new spreadsheet in Drive, it is a "new"-style sheet. I don't recall opting in and don't know if there's a way to go back.

@jsoma
Copy link
Owner

jsoma commented Apr 27, 2014

Thanks so much, digging in to all of these "new"-style sheet issues now!

jsoma added a commit that referenced this pull request Apr 27, 2014
Fix determination of worksheet ID for new sheets
@jsoma jsoma merged commit f199e92 into jsoma:master Apr 27, 2014
@jsoma jsoma mentioned this pull request Apr 27, 2014
@jlord
Copy link

jlord commented Apr 27, 2014

🎉 YAY! 🎊 Thank you!

ghing added a commit to workdept/affirmations-referral-network that referenced this pull request Apr 29, 2014
Now that jsoma/tabletop#46 and
jsoma/tabletop#48 have been merged, we
can use regular Tabletop instead of my fork.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants