sheetjs_sheetjs/demos/extendscript/README.md
2022-05-20 04:56:18 -04:00

2.3 KiB

Adobe ExtendScript

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 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:

/* Read file from disk */
var workbook = XLSX.readFile(filename);

/* Write file to disk */
XLSX.writeFile(workbook, filename);
Implementation Details (click to show)

The readFile and writeFile functions use "binary" encoding under the hood:

/* Read file from disk without using readFile */
var infile = File(filename);
infile.open("r");
infile.encoding = "binary";
var data = infile.read();
var workbook = XLSX.read(data, {type:"binary"});
infile.close();

/* Write file to disk without using writeFile */
var outFile = File(filename);
outFile.open("w");
outFile.encoding = "binary";
outFile.write(workbook);
outFile.close();

Demo

The demo shows:

  • loading the library in ExtendScript using #include:
#include "xlsx.extendscript.js"
  • opening a file with XLSX.readFile:
var workbook = XLSX.readFile("sheetjs.xlsx");
  • converting a worksheet to an array of arrays:
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:
XLSX.writeFile(workbook, "sheetjs.slk");

Analytics