meteor example [ci skip]

This commit is contained in:
SheetJS 2017-05-24 18:52:35 -04:00
parent 53e534f2cc
commit 118e9ad9cb
11 changed files with 234 additions and 0 deletions

@ -169,6 +169,7 @@ The `demos` directory includes sample projects for:
- [`angular`](demos/angular/)
- [`browserify`](demos/browserify/)
- [`Adobe ExtendScript`](demos/extendscript/)
- [`meteor`](demos/meteor/)
- [`phantomjs`](demos/phantomjs/)
- [`requirejs`](demos/requirejs/)
- [`systemjs`](demos/systemjs/)
@ -176,6 +177,9 @@ The `demos` directory includes sample projects for:
### Optional Modules
<details>
<summary><b>Optional features</b> (click to show)</summary>
The node version automatically requires modules for additional features. Some
of these modules are rather large in size and are only needed in special
circumstances, so they do not ship with the core. For browser use, they must
@ -200,6 +204,7 @@ be configured to remove support with `resolve.alias`:
}
```
</details>
### ECMAScript 5 Compatibility
@ -431,6 +436,9 @@ the buffering for you.
The full object format is described later in this README.
<details>
<summary><b>Reading a specific cell </b> (click to show)</summary>
This example extracts the value stored in cell A1 from the first worksheet:
```js
@ -447,6 +455,35 @@ var desired_cell = worksheet[address_of_cell];
var desired_value = (desired_cell ? desired_cell.v : undefined);
```
</details>
<details>
<summary><b>Adding a new worksheet to a workbook</b> (click to show)</summary>
This example uses [`XLSX.utils.aoa_to_sheet`](#array-of-arrays-input) to make a
worksheet and appends the new worksheet to the workbook:
```js
var new_ws_name = "SheetJS";
/* make worksheet */
var ws_data = [
[ "S", "h", "e", "e", "t", "J", "S" ],
[ 1 , 2 , 3 , 4 , 5 ]
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);
/* Add the sheet name to the list */
wb.SheetNames.push(ws_name);
/* Load the worksheet object */
wb.Sheets[ws_name] = ws;
```
</details>
### Complete Examples
- <https://github.com/SheetJS/js-xlsx/blob/master/bin/xlsx.njs> node

1
demos/meteor/.gitignore vendored Normal file

@ -0,0 +1 @@
.meteor

9
demos/meteor/Makefile Normal file

@ -0,0 +1,9 @@
.PHONY: start
start:
@meteor
.PHONY: init
init:
@npm install babel-runtime meteor-node-stubs
@meteor add pfafman:filesaver
@mkdir -p node_modules; cd node_modules; ln -s ../../../ xlsx; cd -

38
demos/meteor/README.md Normal file

@ -0,0 +1,38 @@
# Meteor
This library is universal: outside of environment-specific features (parsing DOM
tables in the browser, streaming write in nodejs), the core is ES3/ES5 and can
be used in any reasonably compliant JS implementation. It should play nice with
meteor out of the box.
## This demonstration
You can split the work between the client and server side as you see fit. The
obvious extremes of pure-client code and pure-server code are straightforward.
This demo tries to split the work to demonstrate that the workbook object can be
passed on the wire.
The read demo:
- accepts files from the client side
- sends binary string to server
- processes data on server side
- sends workbook object to client
- renders HTML and adds to a DOM element
The write demo:
- generates workbook on server side
- sends workbook object to client
- generates file on client side
- triggers a download.
## Environment-specific features
File-related operations (e.g. `XLSX.readFile` and `XLSX.writeFile`) will not be
available in client-side code. If you need to read a local file from the client,
use a file input or drag-and-drop.
Browser-specific operations (e.g. `XLSX.utils.table_to_book`) are limited to
client side code. You should never have to read from DOM elements on the server
side, but you can use a third-party virtual DOM to provide the required API.

@ -0,0 +1,2 @@
/* CSS declarations go here */
a { text-decoration: none }

@ -0,0 +1,27 @@
<head>
<title>meteor-xlsx</title>
</head>
<body>
<pre>
<b><a href="//sheetjs.com">SheetJS Meteor Demo</a></b>
<b>Meteor Read Demo</b>
{{> read}}
<b>Meteor Write Demo</b>
{{> write}}
</pre>
</body>
<template name="read">
<label for="upload">Parse File: </label><input type="file" id="upload" />
<div id="out"></div>
</template>
<template name="write">
<label for="dnload">Write File: </label><button id="dnload">Generate Worksheet</button>
</template>

@ -0,0 +1,50 @@
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
const XLSX = require('xlsx');
Template.read.events({
'change input' (evt, instance) {
/* "Browser file upload form element" from SheetJS README */
const file = evt.currentTarget.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const data = e.target.result;
const name = file.name;
/* Meteor magic */
Meteor.call('upload', data, name, function(err, wb) {
if(err) console.error(err);
else {
/* do something here -- this just dumps an array of arrays to console */
console.log(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], {header:1}));
document.getElementById('out').innerHTML = (XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]));
}
});
};
reader.readAsBinaryString(file);
},
});
Template.write.events({
'click button' (evt, instance) {
Meteor.call('download', function(err, wb) {
if(err) console.error(err);
else {
console.log(wb);
/* "Browser download file" from SheetJS README */
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
var wbout = XLSX.write(wb, wopts);
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "meteor.xlsx");
}
});
},
});
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}

11
demos/meteor/package.json Normal file

@ -0,0 +1,11 @@
{
"name": "meteor-xlsx",
"private": true,
"scripts": {
"start": "meteor run"
},
"dependencies": {
"babel-runtime": "^6.20.0",
"meteor-node-stubs": "~0.2.4"
}
}

@ -0,0 +1,22 @@
import { Meteor } from 'meteor/meteor';
const XLSX = require('xlsx');
Meteor.methods({
upload: (bstr, name) => {
/* read the data and return the workbook object to the frontend */
return XLSX.read(bstr, {type:'binary'});
},
download: () => {
/* generate a workbook object and return to the frontend */
const data = [
["a", "b", "c"],
[ 1 , 2 , 3 ]
];
const ws = XLSX.utils.aoa_to_sheet(data);
const wb = {SheetNames: ["Sheet1"], Sheets:{Sheet1:ws }};
return wb;
}
});
Meteor.startup(() => { });

@ -28,6 +28,7 @@ The `demos` directory includes sample projects for:
- [`angular`](demos/angular/)
- [`browserify`](demos/browserify/)
- [`Adobe ExtendScript`](demos/extendscript/)
- [`meteor`](demos/meteor/)
- [`phantomjs`](demos/phantomjs/)
- [`requirejs`](demos/requirejs/)
- [`systemjs`](demos/systemjs/)
@ -35,6 +36,9 @@ The `demos` directory includes sample projects for:
### Optional Modules
<details>
<summary><b>Optional features</b> (click to show)</summary>
The node version automatically requires modules for additional features. Some
of these modules are rather large in size and are only needed in special
circumstances, so they do not ship with the core. For browser use, they must
@ -59,6 +63,7 @@ be configured to remove support with `resolve.alias`:
}
```
</details>
### ECMAScript 5 Compatibility

@ -2,6 +2,9 @@
The full object format is described later in this README.
<details>
<summary><b>Reading a specific cell </b> (click to show)</summary>
This example extracts the value stored in cell A1 from the first worksheet:
```js
@ -18,6 +21,35 @@ var desired_cell = worksheet[address_of_cell];
var desired_value = (desired_cell ? desired_cell.v : undefined);
```
</details>
<details>
<summary><b>Adding a new worksheet to a workbook</b> (click to show)</summary>
This example uses [`XLSX.utils.aoa_to_sheet`](#array-of-arrays-input) to make a
worksheet and appends the new worksheet to the workbook:
```js
var new_ws_name = "SheetJS";
/* make worksheet */
var ws_data = [
[ "S", "h", "e", "e", "t", "J", "S" ],
[ 1 , 2 , 3 , 4 , 5 ]
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);
/* Add the sheet name to the list */
wb.SheetNames.push(ws_name);
/* Load the worksheet object */
wb.Sheets[ws_name] = ws;
```
</details>
### Complete Examples
- <https://github.com/SheetJS/js-xlsx/blob/master/bin/xlsx.njs> node