forked from sheetjs/sheetjs
		
	estk test [ci skip]
This commit is contained in:
		
							parent
							
								
									f1480ebd2e
								
							
						
					
					
						commit
						b7d3eae3b7
					
				@ -31,7 +31,6 @@ can be installed with Bash on Windows or with `cygwin`.
 | 
			
		||||
 | 
			
		||||
**Bundlers and Tooling**
 | 
			
		||||
- [`browserify`](browserify/)
 | 
			
		||||
- [`fusebox`](fusebox/)
 | 
			
		||||
- [`parcel`](parcel/)
 | 
			
		||||
- [`requirejs`](requirejs/)
 | 
			
		||||
- [`rollup`](rollup/)
 | 
			
		||||
@ -44,7 +43,7 @@ can be installed with Bash on Windows or with `cygwin`.
 | 
			
		||||
- [`electron application`](electron/)
 | 
			
		||||
- [`nw.js application`](nwjs/)
 | 
			
		||||
- [`Chrome / Chromium extensions`](chrome/)
 | 
			
		||||
- [`Download a Google Sheet locally`](google-sheet/)
 | 
			
		||||
- [`Google Sheets API`](https://docs.sheetjs.com/docs/getting-started/demos/gsheet)
 | 
			
		||||
- [`Adobe ExtendScript`](extendscript/)
 | 
			
		||||
- [`Headless Browsers`](headless/)
 | 
			
		||||
- [`canvas-datagrid`](datagrid/)
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ This demo is available at <https://oss.sheetjs.com/sheetjs/datagrid.html>
 | 
			
		||||
## Obtaining the Library
 | 
			
		||||
 | 
			
		||||
The `canvas-datagrid` NodeJS packages include a minified script that can be
 | 
			
		||||
directly inserted as a script tag.  The unpkg CDN also hosts this script:
 | 
			
		||||
directly inserted as a script tag.  The unpkg CDN also serves this script:
 | 
			
		||||
 | 
			
		||||
```html
 | 
			
		||||
<script src="https://unpkg.com/canvas-datagrid/dist/canvas-datagrid.js"></script>
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,7 @@ ExtendScript adds some features to a limited form of ECMAScript version 3.  With
 | 
			
		||||
the included shim, the library can run within Photoshop and other Adobe apps!
 | 
			
		||||
 | 
			
		||||
The main file is `test.jsx`.  Target-specific files prepend target directives.
 | 
			
		||||
Copy the `test.jsx` file as well as the `xlsx.extendscript.js` library script 
 | 
			
		||||
Copy the `test.jsx` file as well as the `xlsx.extendscript.js` library script
 | 
			
		||||
to wherever you want the scripts to reside.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -30,6 +30,8 @@ var workbook = XLSX.readFile(filename);
 | 
			
		||||
XLSX.writeFile(workbook, filename);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
<details><summary><b>Implementation Details</b> (click to show)</summary>
 | 
			
		||||
 | 
			
		||||
The `readFile` and `writeFile` functions use `"binary"` encoding under the hood:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -50,6 +52,7 @@ outFile.write(workbook);
 | 
			
		||||
outFile.close();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
</details>
 | 
			
		||||
 | 
			
		||||
## Demo
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								demos/fusebox/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										4
									
								
								demos/fusebox/.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,4 +0,0 @@
 | 
			
		||||
.fusebox
 | 
			
		||||
tsconfig.json
 | 
			
		||||
client.js
 | 
			
		||||
server.js
 | 
			
		||||
@ -1,17 +0,0 @@
 | 
			
		||||
.PHONY: all
 | 
			
		||||
all server.js client.js: fuse.js
 | 
			
		||||
	node fuse.js
 | 
			
		||||
 | 
			
		||||
.PHONY: test ctest
 | 
			
		||||
test: server.js
 | 
			
		||||
	@node server.js
 | 
			
		||||
	xlsx --dev sheetjsfbox.xlsb
 | 
			
		||||
 | 
			
		||||
ctest: client.js
 | 
			
		||||
	python -mSimpleHTTPServer
 | 
			
		||||
 | 
			
		||||
.PHONY: init
 | 
			
		||||
init:
 | 
			
		||||
	mkdir -p node_modules
 | 
			
		||||
	cd node_modules; ln -s ../../../ xlsx; cd -
 | 
			
		||||
 | 
			
		||||
@ -1,62 +0,0 @@
 | 
			
		||||
# FuseBox
 | 
			
		||||
 | 
			
		||||
This library is built with some dynamic logic to determine if it is invoked in a
 | 
			
		||||
script tag or in nodejs.  FuseBox does not understand those feature tests, so by
 | 
			
		||||
default it will do some strange things.
 | 
			
		||||
 | 
			
		||||
## TypeScript Support
 | 
			
		||||
 | 
			
		||||
As with most TS modules in FuseBox, the glob import form should be used:
 | 
			
		||||
 | 
			
		||||
```typescript
 | 
			
		||||
import * as XLSX from 'xlsx';
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
The included `sheetjs.ts` script will be transpiled and bundled to `server.js`
 | 
			
		||||
for the `"node"` target and `client.js` for the `"browser"` target.
 | 
			
		||||
 | 
			
		||||
## Proper Target Detection
 | 
			
		||||
 | 
			
		||||
Out of the box, FuseBox will automatically provide shims to browser globals like
 | 
			
		||||
`process` and `Browser`.  The proper way to detect `node` uses `process`:
 | 
			
		||||
 | 
			
		||||
```typescript
 | 
			
		||||
if(typeof process != 'undefined' && process.versions && process.versions.node) {
 | 
			
		||||
  /* Script is running in nodejs */
 | 
			
		||||
} else {
 | 
			
		||||
  /* Script is running in a browser environment */
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Server Target
 | 
			
		||||
 | 
			
		||||
The FuseBox documentation configuration can be used as-is:
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const fuse = FuseBox.init({
 | 
			
		||||
  homeDir: ".",
 | 
			
		||||
  target: "node",
 | 
			
		||||
  output: "$name.js"
 | 
			
		||||
});
 | 
			
		||||
fuse.bundle("server").instructions(">sheetjs.ts"); fuse.run();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Browser Target
 | 
			
		||||
 | 
			
		||||
The native shims must be suppressed for browser usage:
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const fuse = FuseBox.init({
 | 
			
		||||
  homeDir: ".",
 | 
			
		||||
  target: "browser",
 | 
			
		||||
  natives: {
 | 
			
		||||
    Buffer: false,
 | 
			
		||||
    stream: false,
 | 
			
		||||
    process: false
 | 
			
		||||
  },
 | 
			
		||||
  output: "$name.js"
 | 
			
		||||
});
 | 
			
		||||
fuse.bundle("client").instructions(">sheetjs.ts"); fuse.run();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
[](https://github.com/SheetJS/js-xlsx)
 | 
			
		||||
@ -1,26 +0,0 @@
 | 
			
		||||
const { FuseBox } = require("fuse-box");
 | 
			
		||||
const common_opts = {
 | 
			
		||||
  homeDir: ".",
 | 
			
		||||
  output: "$name.js"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const browser_opts = {
 | 
			
		||||
  target: "browser",
 | 
			
		||||
  natives: {
 | 
			
		||||
    Buffer: false,
 | 
			
		||||
    stream: false,
 | 
			
		||||
    process: false
 | 
			
		||||
  },
 | 
			
		||||
  ...common_opts
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const node_opts = {
 | 
			
		||||
  target: "node",
 | 
			
		||||
  ...common_opts
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const fuse1 = FuseBox.init(browser_opts);
 | 
			
		||||
fuse1.bundle("client").instructions(">sheetjs.ts"); fuse1.run();
 | 
			
		||||
 | 
			
		||||
const fuse2 = FuseBox.init(node_opts);
 | 
			
		||||
fuse2.bundle("server").instructions(">sheetjs.ts"); fuse2.run();
 | 
			
		||||
@ -1,36 +0,0 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<!-- xlsx.js (C) 2013-present  SheetJS http://sheetjs.com -->
 | 
			
		||||
<!-- vim: set ts=2: -->
 | 
			
		||||
<html>
 | 
			
		||||
<head>
 | 
			
		||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 | 
			
		||||
<title>SheetJS FuseBox Test</title>
 | 
			
		||||
<style>
 | 
			
		||||
a { text-decoration: none }
 | 
			
		||||
</style>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
<pre>
 | 
			
		||||
<b><a href="http://sheetjs.com">SheetJS FuseBox Demo</a></b>
 | 
			
		||||
 | 
			
		||||
<a href="https://github.com/SheetJS/js-xlsx">Source Code Repo</a>
 | 
			
		||||
<a href="https://github.com/SheetJS/js-xlsx/issues">Issues?  Something look weird?  Click here and report an issue</a>
 | 
			
		||||
 | 
			
		||||
Original script: <a href="sheetjs.ts">sheetjs.ts</a>
 | 
			
		||||
 | 
			
		||||
<b>Console Output:</b>
 | 
			
		||||
</pre>
 | 
			
		||||
<pre id="console"></pre>
 | 
			
		||||
<b>
 | 
			
		||||
<script>
 | 
			
		||||
if(typeof console !== "undefined") console = {};
 | 
			
		||||
console.__log = console.log || function(){};
 | 
			
		||||
console.log = function(x) {
 | 
			
		||||
    console.__log.apply(console, arguments);
 | 
			
		||||
    document.getElementById('console').innerText += x + "\n";
 | 
			
		||||
};
 | 
			
		||||
console.error = console.debug = console.info =  console.log
 | 
			
		||||
</script>
 | 
			
		||||
<script  type="text/javascript" src="/client.js"></script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@ -1,28 +0,0 @@
 | 
			
		||||
import * as XLSX from 'xlsx';
 | 
			
		||||
 | 
			
		||||
console.log(XLSX.version);
 | 
			
		||||
 | 
			
		||||
const bookType: string = "xlsb";
 | 
			
		||||
const fn: string = "sheetjsfbox." + bookType
 | 
			
		||||
const sn: string = "SheetJSFBox";
 | 
			
		||||
const aoa: any[][] = [ ["Sheet", "JS"], ["Fuse", "Box"], [72, 62] ];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var wb: XLSX.WorkBook = XLSX.utils.book_new();
 | 
			
		||||
var ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(aoa);
 | 
			
		||||
XLSX.utils.book_append_sheet(wb, ws, sn);
 | 
			
		||||
 | 
			
		||||
var payload: string = "";
 | 
			
		||||
var w2: XLSX.WorkBook;
 | 
			
		||||
if(typeof process != 'undefined' && process.versions && process.versions.node) {
 | 
			
		||||
	/* server */
 | 
			
		||||
	XLSX.writeFile(wb, fn);
 | 
			
		||||
	w2 = XLSX.readFile(fn)
 | 
			
		||||
} else {
 | 
			
		||||
	/* client */
 | 
			
		||||
	payload = XLSX.write(wb, {bookType: "xlsb", type:"binary"});
 | 
			
		||||
	w2 = XLSX.read(payload, {type:"binary"});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var s2: XLSX.WorkSheet = w2.Sheets[sn];
 | 
			
		||||
console.log(XLSX.utils.sheet_to_csv(s2));
 | 
			
		||||
@ -1,72 +1,9 @@
 | 
			
		||||
# Google Sheet Demo
 | 
			
		||||
# Google Sheets API
 | 
			
		||||
 | 
			
		||||
This demo is using [`drive-db`](https://github.com/franciscop/drive-db) to fetch a public Google Sheet and then `xlsx` to save the data locally as `test.xlsx`.
 | 
			
		||||
The old demo used a deprecated version of the Google Sheets API to export data
 | 
			
		||||
from Google Sheets Documents.
 | 
			
		||||
 | 
			
		||||
It uses modern Javascript; `import/export`, `async/away`, etc. To run this you need Node.js 12 or newer, and you will notice we added `"type": "module"` to the `package.json`.
 | 
			
		||||
 | 
			
		||||
Here is the full code:
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import xlsx from "xlsx";
 | 
			
		||||
import drive from "drive-db";
 | 
			
		||||
 | 
			
		||||
(async () => {
 | 
			
		||||
  const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
 | 
			
		||||
 | 
			
		||||
  /* Create a new workbook */
 | 
			
		||||
  const workbook = xlsx.utils.book_new();
 | 
			
		||||
 | 
			
		||||
  /* make worksheet */
 | 
			
		||||
  const worksheet = xlsx.utils.json_to_sheet(data);
 | 
			
		||||
 | 
			
		||||
  /* Add the worksheet to the workbook */
 | 
			
		||||
  xlsx.utils.book_append_sheet(workbook, worksheet);
 | 
			
		||||
 | 
			
		||||
  xlsx.writeFile(workbook, "test.xlsx");
 | 
			
		||||
})();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Let's go over the different parts:
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
import xlsx from "xlsx";
 | 
			
		||||
import drive from "drive-db";
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This imports both `xlsx` and `drive-db` libraries. While these are written in commonjs, Javascript Modules can usually import the commonjs modules with no problem.
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
(async () => {
 | 
			
		||||
  // ...
 | 
			
		||||
})();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
<!-- alex ignore retext-profanities -->
 | 
			
		||||
This is what is called an [Immediately Invoked Function Expression](https://flaviocopes.com/javascript-iife/). These are normally used to either create a new execution context, or in this case to allow to run async code easier.
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Using `drive-db`, fetch the data for the given spreadsheet id. In this case it's [this Google Sheet document](https://docs.google.com/spreadsheets/d/1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k/edit), and since we don't specify the sheet it's the default one.
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const workbook = xlsx.utils.book_new();
 | 
			
		||||
const worksheet = xlsx.utils.json_to_sheet(data);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
We need to create a workbook with a worksheet inside. The worksheet is created from the previously fetched data. `drive-db` exports the data in the same format that `xlsx`'s `.json_to_sheet()` method expects, so it's a straightforward operation.
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
xlsx.utils.book_append_sheet(workbook, worksheet);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
The worksheet needs to be inside the workbook, so we use the operation `.book_append_sheet()` to make it so.
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
xlsx.writeFile(workbook, "test.xlsx");
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Finally we save the workbook into a XLSX file in out filesystem. With this, now it can be opened by any spreadsheet program that we have installed.
 | 
			
		||||
[The new demo](https://docs.sheetjs.com/docs/getting-started/demos/gsheet) uses
 | 
			
		||||
the new Google Sheets API to read and write data.
 | 
			
		||||
 | 
			
		||||
[](https://github.com/SheetJS/js-xlsx)
 | 
			
		||||
 | 
			
		||||
@ -1,17 +0,0 @@
 | 
			
		||||
import xlsx from "xlsx";
 | 
			
		||||
import drive from "drive-db";
 | 
			
		||||
 | 
			
		||||
(async () => {
 | 
			
		||||
  const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
 | 
			
		||||
 | 
			
		||||
  /* Create a new workbook */
 | 
			
		||||
  const workbook = xlsx.utils.book_new();
 | 
			
		||||
 | 
			
		||||
  /* make worksheet */
 | 
			
		||||
  const worksheet = xlsx.utils.json_to_sheet(data);
 | 
			
		||||
 | 
			
		||||
  /* Add the worksheet to the workbook */
 | 
			
		||||
  xlsx.utils.book_append_sheet(workbook, worksheet);
 | 
			
		||||
 | 
			
		||||
  xlsx.writeFile(workbook, "test.xlsx");
 | 
			
		||||
})();
 | 
			
		||||
@ -1,13 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "google-sheet",
 | 
			
		||||
  "version": "1.0.0",
 | 
			
		||||
  "description": " This demo is using 'drive-db' to fetch a public Google Sheet and then `xlsx` to save the data locally as test.xlsx",
 | 
			
		||||
  "main": "index.js",
 | 
			
		||||
  "type": "module",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "start": "node ."
 | 
			
		||||
  },
 | 
			
		||||
  "keywords": [],
 | 
			
		||||
  "author": "Francisco Presencia <public@francisco.io> (https://francisco.io/)",
 | 
			
		||||
  "license": "MIT"
 | 
			
		||||
}
 | 
			
		||||
@ -10,7 +10,7 @@ This demo is available at <https://oss.sheetjs.com/sheetjs/x-spreadsheet.html>
 | 
			
		||||
## Obtaining the Library
 | 
			
		||||
 | 
			
		||||
The `x-data-spreadsheet` NodeJS packages include a minified script that can be
 | 
			
		||||
directly inserted as a script tag.  The unpkg CDN also hosts this script:
 | 
			
		||||
directly inserted as a script tag.  The unpkg CDN also serves this script:
 | 
			
		||||
 | 
			
		||||
```html
 | 
			
		||||
<script src="https://unpkg.com/x-data-spreadsheet/dist/xspreadsheet.js"></script>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										45
									
								
								estk.jsx
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										45
									
								
								estk.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
#target estoolkit
 | 
			
		||||
var thisFile = new File($.fileName);
 | 
			
		||||
var basePath = thisFile.path;
 | 
			
		||||
 | 
			
		||||
#include "dist/xlsx.extendscript.js";
 | 
			
		||||
 | 
			
		||||
var filename = "/sheetjs.xlsx";
 | 
			
		||||
 | 
			
		||||
/* Read file from disk */
 | 
			
		||||
var workbook = XLSX.readFile(basePath + filename, {cellDates:true});
 | 
			
		||||
 | 
			
		||||
/* Display first worksheet */
 | 
			
		||||
var first_sheet_name = workbook.SheetNames[0], first_worksheet = workbook.Sheets[first_sheet_name];
 | 
			
		||||
var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});
 | 
			
		||||
alert(data);
 | 
			
		||||
 | 
			
		||||
var outfmts = [
 | 
			
		||||
  ["xlsb",  "testw.xlsb"],
 | 
			
		||||
  ["biff8", "testw.xls"],
 | 
			
		||||
  ["xlml",  "testw.xml"],
 | 
			
		||||
  ["fods",  "testw.fods"],
 | 
			
		||||
  ["csv",   "testw.csv"],
 | 
			
		||||
  ["txt",   "testw.txt"],
 | 
			
		||||
  ["slk",   "testw.slk"],
 | 
			
		||||
  ["eth",   "testw.eth"],
 | 
			
		||||
  ["htm",   "testw.htm"],
 | 
			
		||||
  ["dif",   "testw.dif"],
 | 
			
		||||
  ["ods",   "testw.ods"],
 | 
			
		||||
  ["xlsx",  "testw.xlsx"]
 | 
			
		||||
];
 | 
			
		||||
for(var i = 0; i < outfmts.length; ++i) {
 | 
			
		||||
  alert(outfmts[i][0]);
 | 
			
		||||
  var fname = basePath + "/" + outfmts[i][1];
 | 
			
		||||
 | 
			
		||||
  /* Write file to disk */
 | 
			
		||||
  XLSX.writeFile(workbook, fname);
 | 
			
		||||
 | 
			
		||||
  /* Read new file */
 | 
			
		||||
  var wb = XLSX.readFile(fname, {cellDates:true});
 | 
			
		||||
 | 
			
		||||
  /* Display first worksheet */
 | 
			
		||||
  var f_sheet_name = wb.SheetNames[0], f_worksheet = wb.Sheets[f_sheet_name];
 | 
			
		||||
  var data = XLSX.utils.sheet_to_json(f_worksheet, {header:1, cellDates:true});
 | 
			
		||||
  alert(data);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user