docs.sheetjs.com/docz/static/deno/s2c.ts
2024-01-03 01:47:00 -05:00

119 lines
3.4 KiB
TypeScript

// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.1/package/types/index.d.ts"
import { read, utils, set_cptable, version } from 'https://cdn.sheetjs.com/xlsx-0.20.1/package/xlsx.mjs';
import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
import * as Drash from "https://cdn.jsdelivr.net/gh/drashland/drash@v2.8.1/mod.ts";
class SheetJSResource extends Drash.Resource {
public paths = ["/"];
public OPTIONS(request: Drash.Request, response: Drash.Response) {
const allHttpMethods: string[] = [ "GET", "POST", "PUT", "DELETE" ];
response.headers.set("Allow", allHttpMethods.join());
response.headers.set("Access-Control-Allow-Methods", allHttpMethods.join());
response.headers.set("access-control-allow-origin", "*");
response.status_code = 204;
return response;
}
public POST(request: Drash.Request, response: Drash.Response) {
const file = request.bodyParam<Drash.Types.BodyFile>("file");
const type = request.bodyParam<string>("type");
try { response.headers.set("access-control-allow-origin", "*"); } catch(e) {}
if (!file) throw new Error("File is required!");
var wb = read(file.content, {type: "buffer", dense: true});
return response.html((type == "csv" ? utils.sheet_to_csv : utils.sheet_to_html)(wb.Sheets[wb.SheetNames[0]]));
}
public GET(request: Drash.Request, response: Drash.Response): void {
try { response.headers.set("access-control-allow-origin", "*"); } catch(e) {}
return response.html(`\
<!DOCTYPE html>
<html>
<head>
<title>SheetJS Spreadsheet to HTML Conversion Service</title>
<meta charset="utf-8" />
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html, body {
width: 100vw;
max-width: 100%;
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
-webkit-font-smoothing: antialiased;
background: white;
color: black;
}
body {
padding-top: 5px;
max-width: 760px;
margin-left: auto;
margin-right: auto;
}
table {
border-collapse: collapse;
}
table, tr, th, td { border: 1px solid; }
div {
padding: 5px;
}
li { margin: 5px 0;}
</style>
</head>
<body>
<h2><a href="//sheetjs.com/">SheetJS</a> Spreadsheet Conversion Service</h2>
<br/>
<h3>API</h3>
<br/>
Send a <code>POST</code> request to <a id="u">https://s2c.sheetjs.com</a> with the file in the <code>file</code> body parameter:<br/>
<br/>
<pre>
curl -X POST -F"file=@pres.numbers" <span id="v">https://s2c.sheetjs.com/</span>
</pre>
<br/>
The response will be an HTML TABLE generated from the first worksheet.
<br/><br/>
For CSV data, pass the parameter <code>type=csv</code>:<br/>
<br/>
<pre>
curl -X POST -F"file=@pres.numbers" -F"type=csv" <span id="w">https://s2c.sheetjs.com/</span>
</pre>
<br/><br/>
<h3>Try it out!</h3>
<br/>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
Use the file input element to select a file, then click "Submit"<br/><br/>
<button type="submit">Submit</button><br/><br/>
</form>
SheetJS Library Version: <code>${version}</code>
</body>
<script>w.innerHTML = v.innerHTML = u.innerHTML = u.href = window.location;</script>
</html>`,
);
}
}
const server = new Drash.Server({
hostname: "",
port: 3000,
protocol: "http",
resources: [
SheetJSResource,
],
});
server.run();
console.log(`Server running at ${server.address}.`);