Demos [ci skip]

This commit is contained in:
reviewher 2022-03-06 23:36:36 -08:00
parent 6ede9dcfb9
commit ba3280ee8a
22 changed files with 135 additions and 41 deletions

View File

@ -37,7 +37,7 @@ Deno.writeFileSync("test.xlsb", u8);
## Demos
**Complete Example**
**Complete Examples**
`sheet2csv.ts` is a complete command-line tool for generating CSV text from
workbooks. Building the application is incredibly straightforward:
@ -48,6 +48,10 @@ $ ./sheet2csv test.xlsx # print the first worksheet as CSV
$ ./sheet2csv test.xlsx s5s # print worksheet "s5s" as CSV
```
The [`server` demo](../server) includes a sample Deno server for parsing uploads
and generating HTML TABLE previews.
**Module Import Scenarios**
All demos attempt to read a file and write a new file. [`doit.ts`](./doit.ts)

View File

@ -1,5 +1,6 @@
{
"env": {
"es6": true,
"browser": true,
"node": true
},

View File

@ -1,13 +1,9 @@
# Electron
This library is compatible with Electron and should just work out of the box.
The demonstration uses Electron 14.0.0. The library is added via `require` from
The demonstration uses Electron 17.1.0. The library is added via `require` from
the renderer process.
Electron 9.0.0 and later require the preference `nodeIntegration: true` in order
to `require('XLSX')` in the renderer process. Electron 12.0.0 and later also
require `worldSafeExecuteJavascript: true` and `contextIsolation: true`
The library can also be required from the main process, as shown in this demo
to render a version string in the About dialog on OSX.
@ -37,6 +33,18 @@ var o = dialog.showSaveDialog();
XLSX.writeFile(workbook, o);
```
## Breaking Changes in Electron
The first version of this demo used Electron 1.7.5.
Electron 9.0.0 and later require the preference `nodeIntegration: true` in order
to `require('XLSX')` in the renderer process.
Electron 12.0.0 and later also require `worldSafeExecuteJavascript: true` and
`contextIsolation: true`.
Electron 14+ must use `@electron/remote` instead of `remote`.
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)

View File

@ -1,6 +1,6 @@
/* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
const XLSX = require('xlsx');
const electron = require('electron').remote;
const electron = require('@electron/remote');
const EXTENSIONS = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|");

View File

@ -3,6 +3,7 @@
var electron = require('electron');
var XLSX = require('xlsx');
var app = electron.app;
require('@electron/remote/main').initialize();
var win = null;
@ -18,6 +19,7 @@ function createWindow() {
}
});
win.loadURL("file://" + __dirname + "/index.html");
require('@electron/remote/main').enable(win.webContents);
win.webContents.openDevTools();
win.on('closed', function () { win = null; });
}

View File

@ -4,8 +4,9 @@
"version": "0.0.0",
"main": "main.js",
"dependencies": {
"xlsx": "*",
"electron": "^9.0.5"
"@electron/remote": "^2.0.5",
"electron": "^17.1.0",
"xlsx": "^0.18.3"
},
"scripts": {
"start": "electron ."

View File

@ -1,7 +1,7 @@
.PHONY: app
app:
parcel build index.html --public-url ./
npx parcel build index.html
.PHONY: ctest
ctest:
parcel index.html
npx parcel index.html

View File

@ -1,10 +1,10 @@
# Parcel
Parcel Bundler starting from version 1.5.0 should play nice with this library
out of the box. The standard import form can be used in JS files:
out of the box:
```js
import XLSX from 'xlsx'
import { read, write, utils } from 'xlsx'
```
Errors of the form `Could not statically evaluate fs call` stem from a parcel

View File

@ -43,6 +43,6 @@ Output Format: <select name="format" onchange="setfmt()">
<pre id="out"></pre>
<div id="htmlout"></div>
<br />
<script src="./index.js"></script>
<script src="./index.js" type="module"></script>
</body>
</html>

View File

@ -1,6 +1,6 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/*jshint browser:true */
import X from '../../'
import * as X from '../../';
console.log(X.version);

View File

@ -0,0 +1 @@
{ "source": "index.html", "scripts": { "build": "parcel build" } }

View File

@ -1,8 +1,12 @@
# Rollup
This library presents itself as a CommonJS library, so some configuration is
required. The examples at <https://rollupjs.org> can be followed pretty much in
verbatim. This sample demonstrates a bundle for browser as well as for node.
This library has a proper ESM build that is enabled by default:
```js
import { read, utils } from 'xlsx';
```
This sample demonstrates a bundle for browser as well as for node.
This demo uses the `import` form to expose the whole library, enabling client
code to access the library with `import XLSX from 'xlsx'`. The JS code from
@ -10,19 +14,17 @@ the root demo was moved to a separate `app.js` script.
## Required Plugins
The `rollup-plugin-node-resolve` and `rollup-plugin-commonjs` plugins are used:
The `rollup-plugin-node-resolve` plugin is used:
```js
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
export default {
/* ... */
plugins: [
resolve({
module: false, // <-- this library is not an ES6 module
browser: true, // <-- suppress node-specific features
}),
commonjs()
})
],
/* ... */
};
@ -38,7 +40,7 @@ worker script should import the module:
```diff
-importScripts('dist/xlsx.full.min.js');
+import XLSX from 'xlsx';
+import * as XLSX from 'xlsx'; // or do named imports
```
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)

View File

@ -1,7 +1,6 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/*jshint browser:true */
/*global XLSX */
import XLSX from 'xlsx';
import { read, write, utils } from 'xlsx';
var global_wb;
@ -19,7 +18,7 @@ var process_wb = (function() {
var to_json = function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
var roa = utils.sheet_to_json(workbook.Sheets[sheetName]);
if(roa.length) result[sheetName] = roa;
});
return JSON.stringify(result, 2, 2);
@ -28,7 +27,7 @@ var process_wb = (function() {
var to_csv = function to_csv(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);
var csv = utils.sheet_to_csv(workbook.Sheets[sheetName]);
if(csv.length){
result.push("SHEET: " + sheetName);
result.push("");
@ -41,7 +40,7 @@ var process_wb = (function() {
var to_fmla = function to_fmla(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var formulae = XLSX.utils.get_formulae(workbook.Sheets[sheetName]);
var formulae = utils.get_formulae(workbook.Sheets[sheetName]);
if(formulae.length){
result.push("SHEET: " + sheetName);
result.push("");
@ -54,7 +53,7 @@ var process_wb = (function() {
var to_html = function to_html(workbook) {
HTMLOUT.innerHTML = "";
workbook.SheetNames.forEach(function(sheetName) {
var htmlstr = XLSX.write(workbook, {sheet:sheetName, type:'string', bookType:'html'});
var htmlstr = write(workbook, {sheet:sheetName, type:'string', bookType:'html'});
HTMLOUT.innerHTML += htmlstr;
});
return "";
@ -81,7 +80,7 @@ var b64it = window.b64it = (function() {
var tarea = document.getElementById('b64data');
return function b64it() {
if(typeof console !== 'undefined') console.log("onload", new Date());
var wb = XLSX.read(tarea.value, {type:'base64', WTF:false});
var wb = read(tarea.value, {type:'base64', WTF:false});
process_wb(wb);
};
})();
@ -112,7 +111,7 @@ var do_file = (function() {
var data = e.target.result;
data = new Uint8Array(data);
if(use_worker) xw(data, process_wb);
else process_wb(XLSX.read(data, {type: 'array'}));
else process_wb(read(data, {type: 'array'}));
};
reader.readAsArrayBuffer(f);
};

View File

@ -1,3 +1,3 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
import XLSX from 'xlsx';
import * as XLSX from 'xlsx';
export default XLSX;

View File

@ -1,6 +1,5 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: 'app.js',
output: {
@ -13,6 +12,5 @@ export default {
module: false,
browser: true,
}),
commonjs()
],
};

View File

@ -1,6 +1,5 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: 'main.js',
output: {
@ -12,7 +11,6 @@ export default {
resolve({
module: false,
browser: true,
}),
commonjs()
})
],
};

View File

@ -1,6 +1,5 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: 'xlsxworker.js',
output: {
@ -13,6 +12,5 @@ export default {
module: false,
browser: true,
}),
commonjs()
],
};

View File

@ -1,11 +1,11 @@
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
import XLSX from 'xlsx';
import { read } from 'xlsx';
postMessage({t:"ready"});
onmessage = function (evt) {
var v;
try {
v = XLSX.read(evt.data.d, {type: evt.data.b});
v = read(evt.data.d, {type: evt.data.b});
postMessage({t:"xlsx", d:JSON.stringify(v)});
} catch(e) { postMessage({t:"e",d:e.stack||e}); }
};

View File

@ -23,3 +23,7 @@ hapi: init ## hapi demo
.PHONY: nest
nest: init ## nest demo
bash -c ./nest.sh
.PHONY: drash
drash: ## drash demo
deno run --allow-net drash.ts

View File

@ -195,4 +195,14 @@ This demo creates a module and a controller. The controller handles the actual
requests (creating the endpoint) while the module is used to configure `multer`.
## Deno
[`Drash`](https://drash.land/drash/) is a Deno framework for Deno's HTTP server.
The `drash.ts` demo responds to POST requests and responds with HTML previews.
<https://s2c.deno.dev> is a live deployment of the service.
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)

68
demos/server/drash.ts Normal file
View File

@ -0,0 +1,68 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
import { read, utils, set_cptable } from 'https://deno.land/x/sheetjs@v0.18.3/xlsx.mjs';
import * as cptable from 'https://deno.land/x/sheetjs/dist/cpexcel.full.mjs';
set_cptable(cptable);
import * as Drash from "https://deno.land/x/drash@v2.5.4/mod.ts";
// Create your resource
class HomeResource extends Drash.Resource {
public paths = ["/"];
public POST(request: Drash.Request, response: Drash.Response) {
const file = request.bodyParam<Drash.Types.BodyFile>("file");
if (!file) throw new Error("File is required!");
var wb = read(file.content, {type: "buffer"});
return response.html( utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]));
}
public GET(request: Drash.Request, response: Drash.Response): void {
return response.html(`\
<!DOCTYPE html>
<html>
<head>
<title>SheetJS Spreadsheet to HTML Conversion Service</title>
<meta charset="utf-8" />
</head>
<body>
<pre><h3><a href="//sheetjs.com/">SheetJS</a> Spreadsheet Conversion Service</h3>
<b>API</b>
Send a POST request to https://s2c.deno.dev/ with the file in the "file" body parameter:
$ curl -X POST -F"file=@test.xlsx" https://s2c.deno.dev/
The response will be an HTML TABLE generated from the first worksheet.
<b>Try it out!</b><form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
Use the file input element to select a file, then click "Submit"
<button type="submit">Submit</button>
</form>
</pre>
</body>
</html>`,
);
}
}
// Create and run your server
const server = new Drash.Server({
hostname: "",
port: 3000,
protocol: "http",
resources: [
HomeResource,
],
});
server.run();
console.log(`Server running at ${server.address}.`);