This commit is contained in:
SheetJS 2017-04-16 04:17:21 -04:00
parent 1d4e7265af
commit f9b3115177
4 changed files with 78 additions and 16 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

68
index.html Normal file
View File

@ -0,0 +1,68 @@
<html>
<head>
<title>SheetJS Spreadsheet Conversion Service</title>
<link rel="icon" type="image/png" href="//rawgit.com/SheetJS/SheetJS.github.io/master/assets/img/logo.png"/>
</head>
<body>
<style> a { text-decoration: none } </style>
<pre>
<b><a href="http://sheetjs.com">SheetJS</a> Spreadsheet Conversion Service</b>
send a request to /data?url=&lt;url&gt;&amp;N=&lt;idx&gt; to convert the spreadsheet at `url` to a simpler format
<b>parameters:</b>
- url=&lt;url&gt; the url to request
- N=&lt;idx&gt; the sheet index to use (-1 returns a list of sheet names in the workbook)
- t=&lt;type&gt; export type: "json" for json, "html" for html, default is CSV
<b>examples:</b>
</pre>
<pre id="examples"></pre>
<script>
var URLS = [
["[CSV] ", {url:"https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls"}],
["[JSON]", {url:"https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls", t:"json"}],
["[HTML]", {url:"https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls", t:"html"}],
[],
["[LIST]", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:-1}],
["[N=1] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:1}],
["[N=2] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:2}],
["[N=3] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:3}],
["[N=4] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:4}],
["[N=5] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:5}],
["[N=6] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:6}],
["[N=7] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:7}],
["[N=8] ", {url:"https://www.ers.usda.gov/webdocs/DataFiles/Feed_Grains_Yearbook_Tables__18027/Feed%20Grains%20Yearbook%20Tables-All%20Years.xls?v=42780",N:8}]
];
var parms = ["N", "t"];
for(var i = 0; i < URLS.length; ++i) {
if(URLS[i].length < 2) { document.getElementById("examples").innerHTML += (URLS[i][0]||"") + "\n"; continue; }
var path = '/data?url=' + URLS[i][1].url;
for(var j = 0; j < parms.length; ++j) if(URLS[i][1][parms[j]]) path += '&' + parms[j] + '=' + URLS[i][1][parms[j]];
document.getElementById("examples").innerHTML += '- ' + URLS[i][0] + ' <a href="' + path + '">' + path + '</a>\n';
}
</script>
<pre>
<b>This service is powered by <a href="http://sheetjs.com">SheetJS</a></b>
<b>Source Code:</b>
- <a href="https://github.com/SheetJS/sheetaki">Source code for this service</a>
- <a href="https://github.com/SheetJS/js-xlsx">Source code for the js-xlsx spreadsheet library</a>
</pre>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36810333-1']);
_gaq.push(['_setDomainName', 'sheetjs.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>

View File

@ -1,6 +1,7 @@
var XLSX = require('xlsx');
var URL = require('url');
var request = require('request');
var send = require('send');
var micro = require('micro');
function do_url(req, url, res) {
@ -23,28 +24,19 @@ function do_url(req, url, res) {
var ws = wb.Sheets[wb.SheetNames[N]];
switch(url.query.t) {
case "json": return micro.send(res, 200, JSON.stringify(XLSX.utils.sheet_to_json(ws, {header:1, raw:true})));
case "html": return XLSX.stream.to_html(ws).pipe(res);
default: XLSX.stream.to_csv(ws).pipe(res);
}
});
}
var msg = [
'request /data?url=<url>&N=<idx> to convert the data at [URL] to CSV',
'',
'parameters:',
'- url=<url> the url to request',
'- N=<idx> the sheet index to use (-1 for sheet list)',
'- t=<type> export type: "json" for json',
'',
'examples: ',
'- /data?url=https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls',
'',
'js-xlsx version: ' + XLSX.version
].join("\n");
send.mime.default_type="text/html";
module.exports = function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
var url = URL.parse(req.url, true);
if(url.pathname == "/") return msg;
if(url.pathname == "/") {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
return send(req, "index.html").pipe(res);
}
res.setHeader('Access-Control-Allow-Origin', '*');
var mode = -1;
if(url.query.url) mode = 0;
if(mode == -1) return micro.send(res, 500, "Must issue command");

View File

@ -7,6 +7,7 @@
"dependencies": {
"micro":"",
"request":"",
"send":"",
"xlsx":""
},
"scripts": {