all da files
This commit is contained in:
162
node_modules/feed-tables/README.md
generated
vendored
Normal file
162
node_modules/feed-tables/README.md
generated
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
# Feed Tables
|
||||
|
||||
Feed tables provide lightweight parsers for Google Spreadsheets data available as a table in cells feed or list feed JSON data formats.
|
||||
Feed 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
|
||||
and in a browser by using JSONP. You can, however, use any other mechanism to access the data including authorization mechanisms.
|
||||
|
||||
### Spreadsheet data
|
||||
|
||||
You must have a Google spreadsheet ready. For example, a <a href="https://spreadsheets.google.com/spreadsheet/ccc?key=0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc">sample data</a>
|
||||
contains data about people such as for every person in the table there is a name, a street, and a city.
|
||||
In order to use this data, you can access it
|
||||
as 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,
|
||||
you need to publish your spreadsheet as a Web page (in Google Spreadsheet go to Share -> Publish as a Web Page).
|
||||
|
||||
## Usage in Node.js
|
||||
|
||||
### Installation
|
||||
|
||||
npm install feed-tables
|
||||
|
||||
### Loading the data
|
||||
|
||||
First, you need to load the data from the spreadsheet. In Node.js, you can use
|
||||
http and url libraries and the following `receive` function:
|
||||
|
||||
```js
|
||||
var http = require('http');
|
||||
var url = require('url');
|
||||
|
||||
var receive = function(dataUrl, dataReady) {
|
||||
var urlp = url.parse(dataUrl);
|
||||
var service = http.createClient(urlp.port ? urlp.port : 80, urlp.hostname);
|
||||
var req = service.request('GET', urlp.pathname + urlp.search,
|
||||
{'host': urlp.hostname});
|
||||
|
||||
var data = null, status = null, finished = false;
|
||||
|
||||
req.on('response', function (res) {
|
||||
var body = "";
|
||||
status = res.statusCode;
|
||||
|
||||
res.on('data', function (chunk) {
|
||||
body += chunk;
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
data = JSON.parse(body);
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
dataReady(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.end();
|
||||
|
||||
setTimeout(function() {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
dataReady(null);
|
||||
}
|
||||
}, 2000);
|
||||
};
|
||||
```
|
||||
|
||||
### Parsing the data
|
||||
|
||||
You can use either `CellsFeed` or `ListFeed` parser to parse the data. This depends on the format
|
||||
of the spreadsheet you want to use. Cells feeds are larger as every spreadsheet cell data
|
||||
is in a separated atom feed entry. The `CellsFeed` parser expects that there are names of header fields
|
||||
in the first row of the spreadsheet.
|
||||
|
||||
The following code shows how to use the `CellsFeed` parser.
|
||||
|
||||
```js
|
||||
var ft = require('feed-tables');
|
||||
|
||||
receive("https://spreadsheets.google.com/feeds/cells/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json",
|
||||
function(data) {
|
||||
if (data) {
|
||||
var table = new ft.CellsFeed(data);
|
||||
for (var r = 0; r < table.length; r++) {
|
||||
var row = table.getRow(r);
|
||||
console.log("name: " + row.name + ", street: " + row.street, " city: " + row.city + "\n");
|
||||
}
|
||||
} else
|
||||
console.log("Timeout while fetching the Google Spreadsheet data.");
|
||||
});
|
||||
```
|
||||
|
||||
List feeds are much smaller in size as every atom feed entry contains a single row, however, this format
|
||||
does not contain a row with all the table's header field names, hence you need to provide
|
||||
the header field names expicitly when creating the parser.
|
||||
|
||||
To create the `ListFeed` parser you need to use the List feed url https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json
|
||||
when retrieving the spreadsheet data and the following code
|
||||
|
||||
```js
|
||||
var table = new ft.ListFeed(data, ["name", "street", "city"]);
|
||||
```
|
||||
## Usage in a browser
|
||||
|
||||
In a browser you can load the data either by using XMLHttpRequest or JSONP. Google spreadsheets
|
||||
supports both options including Cross-Origin Resource Sharing if you use XHR. Following code
|
||||
shows how to load the data using JSONP for which you need to add `callback` parameter at the end of
|
||||
the spreadsheet url. You also need to have `feed-tables.js` included in your document.
|
||||
|
||||
```html
|
||||
<html>
|
||||
<head>
|
||||
<!-- ... -->
|
||||
<script type="text/javascript" src="path/to/your/feed-tables.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
var URL = "https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json&callback=dataReady";
|
||||
|
||||
function loadData() {
|
||||
var scp = document.createElement('script');
|
||||
scp.setAttribute("type","text/javascript");
|
||||
scp.setAttribute("src", URL);
|
||||
document.getElementsByTagName("head")[0].appendChild(scp);
|
||||
}
|
||||
|
||||
function dataReady(data) {
|
||||
var table = new ListFeed(data, ["name", "street", "city"]);
|
||||
for (var r = 0; r < table.length; r++) {
|
||||
var row = table.getRow(r);
|
||||
// access data here row.name, row.street, row.city
|
||||
// add them to your HTML code
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<!-- ... -->
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011 Tomas Vitvar <tomas@vitvar.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
39
node_modules/feed-tables/examples/example.html
generated
vendored
Normal file
39
node_modules/feed-tables/examples/example.html
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../lib/feed-tables.js"></script>
|
||||
<title>Feed Tables Example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
var URL = "https://spreadsheets.google.com/feeds/list/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json&callback=dataReady";
|
||||
|
||||
function loadData() {
|
||||
var scp = document.createElement('script');
|
||||
scp.setAttribute("type","text/javascript");
|
||||
scp.setAttribute("src", URL);
|
||||
document.getElementsByTagName("head")[0].appendChild(scp);
|
||||
}
|
||||
|
||||
function dataReady(data) {
|
||||
var table = new ListFeed(data, ["name", "street", "city"]);
|
||||
|
||||
var div = document.getElementById("table"), s = "";
|
||||
|
||||
for (var r = 0; r < table.length; r++) {
|
||||
var row = table.getRow(r);
|
||||
s += "<p>" + row.name + ", " + row.street + ", " + row.city + "</p>";
|
||||
}
|
||||
div.innerHTML = s;
|
||||
}
|
||||
|
||||
window.onload = function() { loadData(); };
|
||||
</script>
|
||||
|
||||
<h1>Data from the spreadsheet</h1>
|
||||
<div id="table"></div>
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
54
node_modules/feed-tables/examples/nodejs-example.js
generated
vendored
Normal file
54
node_modules/feed-tables/examples/nodejs-example.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
var http = require('http');
|
||||
var url = require('url');
|
||||
|
||||
var receive = function(dataUrl, dataReady) {
|
||||
var urlp = url.parse(dataUrl);
|
||||
var service = http.createClient(urlp.port ? urlp.port : 80, urlp.hostname);
|
||||
var req = service.request('GET', urlp.pathname + urlp.search,
|
||||
{'host': urlp.hostname});
|
||||
|
||||
var data = null, status = null, finished = false;
|
||||
|
||||
req.on('response', function (res) {
|
||||
var body = "";
|
||||
status = res.statusCode;
|
||||
|
||||
res.on('data', function (chunk) {
|
||||
body += chunk;
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
data = JSON.parse(body);
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
dataReady(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.end();
|
||||
|
||||
setTimeout(function() {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
dataReady(null);
|
||||
}
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
console.log(process.cwd());
|
||||
var ft = require(process.cwd() + '/lib/feed-tables.js');
|
||||
|
||||
receive("https://spreadsheets.google.com/feeds/cells/0AoooUkEfVrhldEpRekRVakVYWmJ2U2Z4SFBVZ0M1Nnc/od6/public/basic?alt=json",
|
||||
function(data) {
|
||||
if (data) {
|
||||
var table = new ft.CellsFeed(data);
|
||||
for (var r = 0; r < table.length; r++) {
|
||||
var row = table.getRow(r);
|
||||
console.log("name: " + row.name + ", street: " + row.street, " city: " + row.city + "\n");
|
||||
}
|
||||
} else
|
||||
console.log("Timeout while fetching the Google Spreadsheet data.");
|
||||
});
|
||||
|
||||
|
1
node_modules/feed-tables/index.js
generated
vendored
Normal file
1
node_modules/feed-tables/index.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require('./lib/feed-tables');
|
199
node_modules/feed-tables/lib/feed-tables.js
generated
vendored
Normal file
199
node_modules/feed-tables/lib/feed-tables.js
generated
vendored
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* Feed-Tables - JavaScript parser for Google spreadsheet tables
|
||||
* works for both client-side and node.js (server-side) JavaScript
|
||||
*
|
||||
* Feed-Tables provide parses for a Google spreadsheet that is in a form of a table,
|
||||
* that is, it has a header in the first row with field names and
|
||||
* values in remaining rows of the table.
|
||||
*
|
||||
* @author
|
||||
* Tomas Vitvar, http://vitvar.com
|
||||
*
|
||||
* patches and updates:
|
||||
* senorpedro - large sheets handling (translateCellname)
|
||||
*
|
||||
* @version
|
||||
* 0.1.3
|
||||
*
|
||||
* @Licesne
|
||||
* MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parser for list feeds. Since list feeds do not always contain
|
||||
* all header fields, they must be provided explicitly when creating the parser.
|
||||
* They are much smaller in size comparing to cells feeds though.
|
||||
*
|
||||
* @param data JSON data of the list feed
|
||||
* @param headers array that contains names of header fields
|
||||
*/
|
||||
var ListFeed = function(data, headers) {
|
||||
|
||||
// check if this is what we are expecting
|
||||
if (!data || !data.feed || !data.feed.category || data.feed.category.length === 0 ||
|
||||
data.feed.category[0].scheme != "http://schemas.google.com/spreadsheets/2006" ||
|
||||
data.feed.category[0].term != "http://schemas.google.com/spreadsheets/2006#list")
|
||||
throw "The data must be in Google Spreadsheet List feed format!";
|
||||
|
||||
this.data = data.feed;
|
||||
this.headers = headers;
|
||||
|
||||
for (var i = 0; i < this.headers.length; i++)
|
||||
this.headers[i] = this.headers[i].toLowerCase();
|
||||
|
||||
// gets the cell value at row:col
|
||||
this.getValueRC = function(row, col) {
|
||||
if (row < this.length && col < this.headers.length) {
|
||||
var r = this.data.entry[row];
|
||||
|
||||
if (col === 0)
|
||||
return r.title.$t;
|
||||
else {
|
||||
for (var z = col; z < this.headers.length; z++) {
|
||||
var s = ".*" + this.headers[col] + ":\s*(.*)" +
|
||||
(z < this.headers.length - 1 ? (", " + this.headers[z+1] + ".*") : "");
|
||||
var re = new RegExp(s);
|
||||
if (re.test(r.content.$t))
|
||||
return RegExp.$1;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
} else
|
||||
throw new Error('Index out of bounds (' + row + ',' + col +').');
|
||||
};
|
||||
|
||||
// gets the value in the column with label header at specified row
|
||||
this.getValue = function(header, row) {
|
||||
var col = this.headers.indexOf(header.toLowerCase());
|
||||
if (col == -1)
|
||||
throw new Error('Header with value \'' + header + '\' does not exist!');
|
||||
return this.getValueRC(row, col);
|
||||
};
|
||||
|
||||
// gets the whole row as the object
|
||||
this.getRow = function(row) {
|
||||
if (row < this.length) {
|
||||
var o = {};
|
||||
for (var inx = 0; inx < this.headers.length; inx++)
|
||||
o[this.headers[inx].toLowerCase()] =
|
||||
this.getValue(this.headers[inx], row);
|
||||
return o;
|
||||
} else
|
||||
throw new Error('Index out of bounds (' + row + ')');
|
||||
};
|
||||
|
||||
// returns the length of the table
|
||||
this.__defineGetter__('length', function() {
|
||||
return Math.floor(this.data.entry.length);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Parser for cells feed.
|
||||
*
|
||||
* @param data JSON data of the cells feed
|
||||
*/
|
||||
var CellsFeed = function(data) {
|
||||
|
||||
// check if this is what we are expecting
|
||||
if (!data || !data.feed || !data.feed.category || data.feed.category.length === 0 ||
|
||||
data.feed.category[0].scheme != "http://schemas.google.com/spreadsheets/2006" ||
|
||||
data.feed.category[0].term != "http://schemas.google.com/spreadsheets/2006#cell")
|
||||
throw "The data must be in Google Spreadsheet List feed format!";
|
||||
|
||||
this.data = data.feed;
|
||||
this.headers = [];
|
||||
|
||||
var col = 0;
|
||||
var t = this.data.entry[col].title.$t;
|
||||
|
||||
// get cells from the table's header
|
||||
while (t.substring(t.length - 1) == "1") {
|
||||
this.headers.push(this.data.entry[col].content.$t);
|
||||
col++;
|
||||
t = this.data.entry[col].title.$t;
|
||||
}
|
||||
|
||||
// gets the cells value at row:col
|
||||
// all sheet cells are ordered and empty cells are not included;
|
||||
// this uses the binary search to retrieve the value
|
||||
this.getValueRC = function(row, col) {
|
||||
var ft = this;
|
||||
|
||||
// we need to take care of cellnames like "AA203" etc
|
||||
var translateCellname = function(posStr) {
|
||||
var result = posStr.match(/([A-Z]+)(\d+)/);
|
||||
var colTxt = result[1];
|
||||
var r = result[2];
|
||||
var c = 0;
|
||||
|
||||
for (var i = 0, l = colTxt.length; i < l; ++i) {
|
||||
c += (colTxt.charCodeAt(i) - 65) + i * 26;
|
||||
}
|
||||
|
||||
return {
|
||||
r: r,
|
||||
c: c
|
||||
};
|
||||
};
|
||||
|
||||
var bin_search = function(f, t) {
|
||||
var p = (t-f)/2>>0;
|
||||
var position = f + p - 1;
|
||||
var a = ft.data.entry[position].title.$t;
|
||||
var tr = translateCellname(a);
|
||||
var c = tr.c;
|
||||
var r = tr.r;
|
||||
|
||||
var e;
|
||||
if (r == row+2)
|
||||
e = col == c ? 0 : col < c ? -1 : 1;
|
||||
else
|
||||
e = row+2 < r ? -1 : +1;
|
||||
|
||||
if (e === 0)
|
||||
return ft.data.entry[position].content.$t;
|
||||
else
|
||||
if (e < 0 && p !== 0)
|
||||
return bin_search(f, f+p);
|
||||
else
|
||||
if (e > 0 && p !== 0)
|
||||
return bin_search(f+p+1, t);
|
||||
else
|
||||
return null;
|
||||
};
|
||||
|
||||
return bin_search(this.headers.length, this.data.entry.length);
|
||||
};
|
||||
|
||||
this.getValue = function(header, row) {
|
||||
var col = this.headers.indexOf(header.toUpperCase());
|
||||
if (col == -1)
|
||||
throw new Error('Header with value \'' + header + '\' does not exist!');
|
||||
return this.getValueRC(row, col);
|
||||
};
|
||||
|
||||
this.getRow = function(row) {
|
||||
if (row < this.length) {
|
||||
var o = {};
|
||||
for (var col = 0; col < this.headers.length; col++) {
|
||||
var val = this.getValueRC(row, col);
|
||||
o[this.headers[col].toLowerCase()] = val ? val : "";
|
||||
}
|
||||
return o;
|
||||
} else
|
||||
throw new Error('Index out of bounds (' + row + ')');
|
||||
};
|
||||
|
||||
this.__defineGetter__('length', function() {
|
||||
return parseInt(this.data.entry[this.data.entry.length - 1].title.$t.match("[0-9]+$")) - 1;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
// asume we are in node.js
|
||||
exports.CellsFeed = CellsFeed;
|
||||
exports.ListFeed = ListFeed;
|
||||
}
|
25
node_modules/feed-tables/package.json
generated
vendored
Normal file
25
node_modules/feed-tables/package.json
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user