26 lines
7.3 KiB
JSON
26 lines
7.3 KiB
JSON
{
|
|
"name": "feed-tables",
|
|
"author": {
|
|
"name": "Tomas Vitvar",
|
|
"email": "tomas@vitvar.com"
|
|
},
|
|
"version": "0.1.3",
|
|
"description": "A lightweight parser for Google Spreadsheets tables in cells or list feed JSON data formats.",
|
|
"keywords": [
|
|
"Google Spreadsheets",
|
|
"server",
|
|
"client",
|
|
"Atom feed"
|
|
],
|
|
"directories": {
|
|
"lib": "lib"
|
|
},
|
|
"main": "lib/feed-tables",
|
|
"readme": "# Feed Tables\n\nFeed tables provide lightweight parsers for Google Spreadsheets data available as a table in cells feed or list feed JSON data formats.\nFeed tables work for both Node.js and a client-side JavaScript. Loading the data is out of the scope of this library, the sample code below shows how you can load the data in Node.js \nand in a browser by using JSONP. You can, however, use any other mechanism to access the data including authorization mechanisms. \n\n### Spreadsheet data\n\nYou must have a Google spreadsheet ready. For example, a <a href=\"https://spreadsheets.google.com/spreadsheet/ccc?key=0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc\">sample data</a>\ncontains data about people such as for every person in the table there is a name, a street, and a city. \nIn order to use this data, you can access it\nas an atom feed in JSON format either as a <a href=\"https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json\">list feed</a> or a <a href=\"https://spreadsheets.google.com/feeds/cells/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json\">cells feed</a>. Note that in order to access a spreadsheet data at such URLs without any authorization, \nyou need to publish your spreadsheet as a Web page (in Google Spreadsheet go to Share -> Publish as a Web Page).\n\n## Usage in Node.js\n\n### Installation\n\n npm install feed-tables\n\n### Loading the data \n\nFirst, you need to load the data from the spreadsheet. In Node.js, you can use\nhttp and url libraries and the following `receive` function:\n\n```js\nvar http = require('http');\nvar url = require('url');\n\nvar receive = function(dataUrl, dataReady) {\n var urlp = url.parse(dataUrl);\n var service = http.createClient(urlp.port ? urlp.port : 80, urlp.hostname);\n var req = service.request('GET', urlp.pathname + urlp.search,\n {'host': urlp.hostname});\n\n var data = null, status = null, finished = false;\n\n req.on('response', function (res) {\n var body = \"\";\n status = res.statusCode;\n\n res.on('data', function (chunk) {\n body += chunk;\n });\n\n res.on('end', function () {\n data = JSON.parse(body);\n if (!finished) {\n finished = true;\n dataReady(data);\n }\n });\n });\n\n req.end();\n\n setTimeout(function() {\n if (!finished) {\n finished = true;\n dataReady(null);\n }\n }, 2000);\n};\n```\n\n### Parsing the data\n\nYou can use either `CellsFeed` or `ListFeed` parser to parse the data. This depends on the format\nof the spreadsheet you want to use. Cells feeds are larger as every spreadsheet cell data \nis in a separated atom feed entry. The `CellsFeed` parser expects that there are names of header fields \nin the first row of the spreadsheet. \n\nThe following code shows how to use the `CellsFeed` parser.\n\n```js\nvar ft = require('feed-tables');\n\nreceive(\"https://spreadsheets.google.com/feeds/cells/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json\",\n function(data) {\n if (data) {\n var table = new ft.CellsFeed(data); \n for (var r = 0; r < table.length; r++) {\n var row = table.getRow(r);\n console.log(\"name: \" + row.name + \", street: \" + row.street, \" city: \" + row.city + \"\\n\");\n }\n } else\n console.log(\"Timeout while fetching the Google Spreadsheet data.\");\n });\n```\n\nList feeds are much smaller in size as every atom feed entry contains a single row, however, this format\ndoes not contain a row with all the table's header field names, hence you need to provide \nthe header field names expicitly when creating the parser. \n\nTo create the `ListFeed` parser you need to use the List feed url https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json \nwhen retrieving the spreadsheet data and the following code\n\n```js\nvar table = new ft.ListFeed(data, [\"name\", \"street\", \"city\"]); \n```\n## Usage in a browser\n\nIn a browser you can load the data either by using XMLHttpRequest or JSONP. Google spreadsheets\nsupports both options including Cross-Origin Resource Sharing if you use XHR. Following code\nshows how to load the data using JSONP for which you need to add `callback` parameter at the end of \nthe spreadsheet url. You also need to have `feed-tables.js` included in your document.\n\n```html\n<html>\n <head>\n <!-- ... -->\n <script type=\"text/javascript\" src=\"path/to/your/feed-tables.js\"></script>\n </head>\n <body>\n <script>\n var URL = \"https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json&callback=dataReady\";\n \n function loadData() {\n var scp = document.createElement('script');\n \tscp.setAttribute(\"type\",\"text/javascript\");\n \tscp.setAttribute(\"src\", URL);\t\n \tdocument.getElementsByTagName(\"head\")[0].appendChild(scp);\t\n }\n \n function dataReady(data) {\n var table = new ListFeed(data, [\"name\", \"street\", \"city\"]);\n for (var r = 0; r < table.length; r++) {\n var row = table.getRow(r);\n // access data here row.name, row.street, row.city\n // add them to your HTML code\n }\n }\n </script>\n <!-- ... -->\n </body>\n</html>\n```\n\n## License \n\n(The MIT License)\n\nCopyright (c) 2011 Tomas Vitvar <tomas@vitvar.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
|
|
"_id": "feed-tables@0.1.3",
|
|
"dist": {
|
|
"shasum": "ca4d11895af032cf8269f1ea97b7c0d360752f65"
|
|
},
|
|
"_from": "feed-tables"
|
|
}
|