From f9b311517737ecfa05f7d692a10f2eb49b6a7a36 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Sun, 16 Apr 2017 04:17:21 -0400 Subject: [PATCH] html --- .gitignore | 1 + index.html | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 24 +++++++------------ package.json | 1 + 4 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.html b/index.html new file mode 100644 index 0000000..808222f --- /dev/null +++ b/index.html @@ -0,0 +1,68 @@ + + +SheetJS Spreadsheet Conversion Service + + + + +
+SheetJS Spreadsheet Conversion Service
+
+send a request to /data?url=<url>&N=<idx> to convert the spreadsheet at `url` to a simpler format
+
+parameters:
+- url=<url>      the url to request
+- N=<idx>        the sheet index to use (-1 returns a list of sheet names in the workbook)
+- t=<type>       export type: "json" for json, "html" for html, default is CSV
+
+examples:
+
+

+
+
+This service is powered by SheetJS
+
+Source Code:
+
+- Source code for this service
+
+- Source code for the js-xlsx spreadsheet library
+
+ + + diff --git a/index.js b/index.js index b5794e8..a2488fb 100644 --- a/index.js +++ b/index.js @@ -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=&N= to convert the data at [URL] to CSV', - '', - 'parameters:', - '- url= the url to request', - '- N= the sheet index to use (-1 for sheet list)', - '- t= 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"); diff --git a/package.json b/package.json index b4973a1..c3519b5 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dependencies": { "micro":"", "request":"", + "send":"", "xlsx":"" }, "scripts": {