sheetjs-clone/demos/extendscript/README.md

87 lines
2.2 KiB
Markdown
Raw Normal View History

2017-09-24 23:40:09 +00:00
# Adobe ExtendScript
2017-09-24 23:40:09 +00:00
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!
2017-09-24 23:40:09 +00:00
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
2017-09-24 23:40:09 +00:00
to wherever you want the scripts to reside.
## ExtendScript Quirks
There are numerous quirks in ExtendScript code parsing, especially related to
Boolean and bit operations. Most JS tooling will generate code that is not
compatible with ExtendScript. It is highly recommended to `#include` the `dist`
file directly and avoid trying to minify or pack as part of a larger project.
## File I/O
Using the `"binary"` encoding, file operations will work with binary strings
that play nice with the `"binary"` type of this library. The `readFile` and
`writeFile` library functions wrap the File logic:
```js
/* Read file from disk */
var workbook = XLSX.readFile(filename);
/* Write file to disk */
XLSX.writeFile(workbook, filename);
```
The `readFile` and `writeFile` functions use `"binary"` encoding under the hood:
2017-09-24 23:40:09 +00:00
```js
/* Read file from disk without using readFile */
var infile = File(filename);
2017-09-24 23:40:09 +00:00
infile.open("r");
infile.encoding = "binary";
var data = infile.read();
var workbook = XLSX.read(data, {type:"binary"});
infile.close();
2017-09-24 23:40:09 +00:00
/* Write file to disk without using writeFile */
var outFile = File(filename);
outFile.open("w");
outFile.encoding = "binary";
outFile.write(workbook);
outFile.close();
2017-09-24 23:40:09 +00:00
```
## Demo
The demo shows:
- loading the library in ExtendScript using `#include`:
```js
#include "xlsx.extendscript.js"
```
- opening a file with `XLSX.readFile`:
```js
var workbook = XLSX.readFile("sheetjs.xlsx");
```
- converting a worksheet to an array of arrays:
```js
var first_sheet_name = workbook.SheetNames[0];
var first_worksheet = workbook.Sheets[first_sheet_name];
var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});
alert(data);
```
- writing a new workbook file:
```js
XLSX.writeFile(workbook, "sheetjs.slk");
```
2017-09-24 23:40:09 +00:00
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)