extendscript

This commit is contained in:
SheetJS 2022-05-30 01:04:05 -04:00
parent 403a102cb8
commit 4aafcce8be
11 changed files with 248 additions and 12 deletions

@ -0,0 +1,190 @@
---
sidebar_position: 3
---
# Adobe Apps
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Photoshop, InDesign and other Adobe Creative Suite applications offer extension
support. Over the years there have been a few different JavaScript platforms:
- "ExtendScript": This uses an old JavaScript dialect but is supported in older
versions of Creative Suite and Creative Cloud.
- "CEP": This was recommended in CS6 but eventually deprecated.
- "UXP": This is the current Adobe recommendation for new CC extensions.
This demo intends to cover the SheetJS-related parts. General setup as well as
general Adobe considerations are not covered here. A basic familiarity with
extension development is assumed.
## ExtendScript Scripts
[Installation is straightforward:](../../installation/extendscript) download a
script and move it to your project directory.
### Reading Files
`XLSX.readFile` can directly accept an absolute URI:
```js
var workbook = XLSX.readFile("~/Documents/test.xlsx");
```
The path can be user-configurable using `File.openDialog`:
```js
/* Show File Picker */
var thisFile = File.openDialog("Select a spreadsheet");
if(!thisFile) { alert("File not found!"); return; }
/* Read file from disk */
var workbook = XLSX.readFile(thisFile.absoluteURI);
```
<details open><summary><b>Complete Example</b> (click to hide)</summary>
In this example, the script will show a dialog to select a file. After reading
the file, the workbook Author property will be extracted and the Photoshop doc
author (`activeDocument.info.author`) will be changed accordingly.
This demo was verified in Photoshop CS6 64-bit on Windows 10.
```js
#target photoshop
#include "xlsx.extendscript.js";
function main_parse() {
/* Show File Picker */
var thisFile = File.openDialog("Select a spreadsheet");
if(!thisFile) { alert("File not found!"); return; }
/* Read file from disk */
var workbook = XLSX.readFile(thisFile.absoluteURI);
/* Get Workbook Author */
var Props = workbook.Props; if(!Props) { alert("Missing Author!"); return; }
var Author = Props.Author; if(!Author) { alert("Missing Author!"); return; }
/* Change Document Author to Workbook Author */
var info = activeDocument.info;
alert("Changing Author from |" + info.author + "| to |" + Author + "|");
info.author = Author;
}
main_parse();
```
0) Download the [test workbook](pathname:///files/SheetJS.xlsb).
1) Download the following scripts:
- [`xlsx.extendscript.js`](https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.extendscript.js)
- [`parse.jsx`](pathname:///live/parse.jsx)
and place in the scripts directory. For CS6 Windows 10 the path is typically
`C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts`
2) Restart Photoshop and open a file (or create a new one)
3) File > Scripts > parse and select the test workbook
4) An alert will confirm that the file was read and the author will be changed:
!["Changing Author" popup](pathname:///files/psparse.png)
5) File > File Info... should show the updated Author field!
</details>
### Writing Files
`XLSX.writeFile` can directly accept an absolute URI:
```js
XLSX.writeFile(workbook, "~/Documents/test.xlsx");
```
The path can be user-configurable using `File.saveDialog`:
```js
/* Show File Picker */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
if(!thisFile) { alert("File not found!"); return; }
/* Write file to disk */
XLSX.writeFile(workbook, thisFile.absoluteURI);
```
<details open><summary><b>Complete Example</b> (click to hide)</summary>
In this example, the script will show a dialog to select an output file. Once
selected, the library will create a new workbook with one worksheet. Cell A1
will be "Author" and cell B1 will be the active Photoshop document Author.
The PS author is available as `activeDocument.info.author`.
This demo was verified in Photoshop CS6 64-bit on Windows 10.
```js
#target photoshop
#include "xlsx.extendscript.js";
function main_write() {
/* Show File Picker */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
if(!thisFile) { alert("File not found!"); return; }
/* Create new Worksheet */
var ws = XLSX.utils.aoa_to_sheet([
["Author", activeDocument.info.author]
]);
/* Create new Workbook and add worksheet */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
/* Write file to disk */
XLSX.writeFile(wb, thisFile.absoluteURI);
alert("Created File " + thisFile.absoluteURI);
}
main_write();
```
1) Download the following scripts:
- [`xlsx.extendscript.js`](https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.extendscript.js)
- [`write.jsx`](pathname:///live/write.jsx)
and place in the scripts directory. For CS6 Windows 10 the path is typically
`C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts`
2) Restart Photoshop and open a file (or create a new one)
3) File > File Info ... and confirm there is an Author. If not, set to `SheetJS`
4) File > Scripts > write and use the popup to select the Documents folder.
Enter `SheetJSPSTest.xlsx` and hit "Save"
4) An alert will confirm that the file was created:
!["Created File" popup](pathname:///files/pswrite.png)
5) Open the generated `SheetJSPSTest.xlsx` file and compare to Photoshop author
</details>
## CEP
[The standalone scripts](../../installation/standalone) can be added to CEP
extension HTML
## UXP
UXP officially recommends `require` and NodeJS Modules for third party support.
[Use the "Frameworks" instructions to download.](../../installation/frameworks)

@ -33,7 +33,7 @@ The demo projects include small runnable examples and short explainers.
- [`NW.js`](https://github.com/SheetJS/SheetJS/tree/master/demos/nwjs/)
- [`Chrome / Chromium Extension`](https://github.com/SheetJS/SheetJS/tree/master/demos/chrome/)
- [`Google Sheets API`](./gsheet)
- [`ExtendScript for Adobe Apps`](https://github.com/SheetJS/SheetJS/tree/master/demos/extendscript/)
- [`ExtendScript for Adobe Apps`](./extendscript)
- [`Headless Browsers`](https://github.com/SheetJS/SheetJS/tree/master/demos/headless/)
- [`Other JavaScript Engines`](https://github.com/SheetJS/SheetJS/tree/master/demos/altjs/)
- [`"serverless" functions`](https://github.com/SheetJS/SheetJS/tree/master/demos/function/)

@ -338,7 +338,7 @@ request({url: url, encoding: null}, function(err, resp, body) {
});
```
[`axios`](https://npm.im/axios) works the same way in browser and in NodeJS:
[`axios`](https://axios-http.com/) works the same way in browser and in NodeJS:
```js
const XLSX = require("xlsx");

@ -296,10 +296,10 @@ data grid for previewing and modifying structured data in the web browser. The
<details>
<summary><b>Previewing data in a React data grid</b> (click to show)</summary>
[`react-data-grid`](https://npm.im/react-data-grid) is a data grid tailored for
react. It expects two properties: `rows` of data objects and `columns` which
describe the columns. For the purposes of massaging the data to fit the react
data grid API it is easiest to start from an array of arrays.
[`react-data-grid`](https://adazzle.github.io/react-data-grid) is a data grid
built for React. It uses two properties: `rows` of data objects and `columns`
which describe the columns. For the purposes of massaging the data to fit the
`react-data-grid` API it is easiest to start from an array of arrays.
This demo starts by fetching a remote file and using `XLSX.read` to extract:
@ -336,8 +336,9 @@ export default function App() {
<details>
<summary><b>Previewing data in a VueJS data grid</b> (click to show)</summary>
[`vue3-table-lite`](https://github.com/linmasahiro/vue3-table-lite) is a simple
VueJS 3 data table. It is featured [in the VueJS demo](https://github.com/SheetJS/SheetJS/tree/master/demos/vue/modify/).
[`vue3-table-lite`](https://linmasahiro.github.io/vue3-table-lite/dist/) is a
simple VueJS 3 data table. It is featured in the
[VueJS demo](https://github.com/SheetJS/SheetJS/tree/master/demos/vue/modify/).
</details>

@ -67,9 +67,9 @@ XLSX.write(wb, {Props:{Author:"SheetJS"}});
<details>
<summary><b>Format Support</b> (click to show)</summary>
**Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML, SYLK
**Simple Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML, ODS, SYLK
**Unicode Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML
**Unicode Defined Names**: XLSX/M, XLSB, BIFF8 XLS, XLML, ODS
**Defined Name Comment**: XLSX/M, XLSB, BIFF8 XLS

@ -8,7 +8,7 @@ hide_table_of_contents: true
![License](https://img.shields.io/github/license/SheetJS/sheetjs)
[![Build Status](https://img.shields.io/github/workflow/status/sheetjs/sheetjs/Tests:%20node.js)](https://github.com/SheetJS/sheetjs/actions)
[![Snyk Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/github/SheetJS/sheetjs)](https://snyk.io/test/github/SheetJS/sheetjs)
[![npm Downloads](https://img.shields.io/npm/dm/xlsx.svg)](https://npmjs.org/package/xlsx)
[![npm Downloads](https://img.shields.io/npm/dm/xlsx.svg)](https://cdn.sheetjs.com/)
SheetJS Community Edition offers battle-tested open-source solutions for
extracting useful data from almost any complex spreadsheet and generating new
@ -67,7 +67,7 @@ document.getElementById("sheetjsexport").addEventListener('click', function() {
function Table2XLSX(props) {
/* Callback invoked when the button is clicked */
const xport = React.useCallback(() => {
const xport = React.useCallback(async () => {
/* Create worksheet from HTML DOM TABLE */
const table = document.getElementById("Table2XLSX");
const wb = XLSX.utils.table_to_book(table);

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

@ -0,0 +1,22 @@
#target photoshop
#include "xlsx.extendscript.js";
function main_parse() {
/* Show File Picker */
var thisFile = File.openDialog("Select a spreadsheet");
if(!thisFile) { alert("File not found!"); return; }
/* Read file from disk */
var workbook = XLSX.readFile(thisFile.absoluteURI);
/* Get Workbook Author */
var Props = workbook.Props; if(!Props) { alert("Missing Author!"); return; }
var Author = Props.Author; if(!Author) { alert("Missing Author!"); return; }
/* Change Document Author to Workbook Author */
var info = activeDocument.info;
alert("Changing Author from |" + info.author + "| to |" + Author + "|");
info.author = Author;
}
main_parse();

@ -0,0 +1,23 @@
#target photoshop
#include "xlsx.extendscript.js";
function main_write() {
/* Show File Picker */
var thisFile = File.saveDialog("Select an output file", "*.xlsx;*.xls");
if(!thisFile) { alert("File not found!"); return; }
/* Create new Worksheet */
var ws = XLSX.utils.aoa_to_sheet([
["Author", activeDocument.info.author]
]);
/* Create new Workbook and add worksheet */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
/* Write file to disk */
XLSX.writeFile(wb, thisFile.absoluteURI);
alert("Created File " + thisFile.absoluteURI);
}
main_write();