using js-xlsx inside adobe extendscript #603

Closed
opened 2017-03-23 19:33:27 +00:00 by firas3d · 20 comments
firas3d commented 2017-03-23 19:33:27 +00:00 (Migrated from github.com)

Is it possible to include the js-xlsx library and scripts inside adobe JavaScript environment ?
Can i include js-xlsx in a non web application ?

Need an advise for resources on this topic.
Thank you all for keeping this place active.

Is it possible to include the js-xlsx library and scripts inside adobe JavaScript environment ? Can i include js-xlsx in a non web application ? Need an advise for resources on this topic. Thank you all for keeping this place active.
reviewher commented 2017-03-23 19:52:19 +00:00 (Migrated from github.com)

@firas3d if it is a standard JS runtime, you should be able to add the shim.js and dist/xlsx.core.min.js and it should just work. The library works in IE6, so I assume it'll work elsewhere.

If you need international support (like Chinese/Japanese/Korean characters), use dist/xlsx.full.min.js.

I don't think any of the devs are familiar with extend script so it would be helpful if you can produce a small demo that loads the script. And if it does work, please add a note in the wiki :)

@firas3d if it is a standard JS runtime, you should be able to add the [`shim.js`](https://github.com/SheetJS/js-xlsx/blob/master/shim.js) and [`dist/xlsx.core.min.js`](https://github.com/SheetJS/js-xlsx/blob/master/dist/xlsx.core.min.js) and it should just work. The library works in IE6, so I assume it'll work elsewhere. If you need international support (like Chinese/Japanese/Korean characters), use [`dist/xlsx.full.min.js`](https://github.com/SheetJS/js-xlsx/blob/master/dist/xlsx.full.min.js). I don't think any of the devs are familiar with extend script so it would be helpful if you can produce a small demo that loads the script. And if it does work, please add a note in the wiki :)
firas3d commented 2017-03-23 20:07:05 +00:00 (Migrated from github.com)

Thanks @reviewher, will do my best tonight to load the shim.js and xlsx.core.min.js
Which funtion/method can i use that permits to read a cell or a range ?

If we access xlsx inside adobe extedscript, then that would be awesome achievement to all adobe workflow. (Currently all adobe tools developers are converting tables to text files, but you can imagine how its unfriendly for data visualizations)

Thanks @reviewher, will do my best tonight to load the shim.js and xlsx.core.min.js Which funtion/method can i use that permits to read a cell or a range ? If we access xlsx inside adobe extedscript, then that would be awesome achievement to all adobe workflow. (Currently all adobe tools developers are converting tables to text files, but you can imagine how its unfriendly for data visualizations)
reviewher commented 2017-03-23 20:18:54 +00:00 (Migrated from github.com)

You need to somehow "get the file" then "read it".

https://github.com/SheetJS/js-xlsx#parsing-workbooks has some examples. I'm not sure how extend script works but they probably some function to read a file into a string or array. Once you have that, you can call XLSX.read and it will give you a workbook object. Options are described in https://github.com/SheetJS/js-xlsx#parsing-options . The most important one is type which tells the library how the data was encoded (as a string or array): https://github.com/SheetJS/js-xlsx#input-type

XLSX.read will return a workbook object. The format is described in the README: https://github.com/SheetJS/js-xlsx#workbook--worksheet--cell-object-description

There are some utilities for converting to JS objects. For example, to convert the first worksheet to an array of arrays like [["A1","B1","C1"],["A2","B2","C2"],["A3","B3","C3"]]:

// assume workbook is the result of XLSX.read(...)
var first_sheet_name = workbook.SheetNames[0];
var first_worksheet = workbook.Sheets[first_sheet];
var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});
You need to somehow "get the file" then "read it". https://github.com/SheetJS/js-xlsx#parsing-workbooks has some examples. I'm not sure how extend script works but they probably some function to read a file into a string or array. Once you have that, you can call `XLSX.read` and it will give you a workbook object. Options are described in https://github.com/SheetJS/js-xlsx#parsing-options . The most important one is `type` which tells the library how the data was encoded (as a string or array): https://github.com/SheetJS/js-xlsx#input-type `XLSX.read` will return a workbook object. The format is described in the README: https://github.com/SheetJS/js-xlsx#workbook--worksheet--cell-object-description There are some utilities for converting to JS objects. For example, to convert the first worksheet to an array of arrays like `[["A1","B1","C1"],["A2","B2","C2"],["A3","B3","C3"]]`: ```js // assume workbook is the result of XLSX.read(...) var first_sheet_name = workbook.SheetNames[0]; var first_worksheet = workbook.Sheets[first_sheet]; var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1}); ```
firas3d commented 2017-03-23 21:03:48 +00:00 (Migrated from github.com)

I am facing errors in lines scripts format, for example "expected: ;"
The xlsx.core.min.js has no new-lines, all code is one block of text :( thats make it difficult to read.

thanks.

I am facing errors in lines scripts format, for example "expected: ;" The xlsx.core.min.js has no new-lines, all code is one block of text :( thats make it difficult to read. thanks.
SheetJSDev commented 2017-03-23 21:07:36 +00:00 (Migrated from github.com)

Hello @firas3d ! Thanks for looking into this!

For development purposes, include the following files in order:

They are all unminified so you should have a useful line number

Hello @firas3d ! Thanks for looking into this! For development purposes, include the following files in order: - [`shim.js`](https://github.com/SheetJS/js-xlsx/blob/master/shim.js) - [`jszip.js`](https://github.com/SheetJS/js-xlsx/blob/master/jszip.js) - [`xlsx.flow.js`](https://github.com/SheetJS/js-xlsx/blob/master/xlsx.flow.js) They are all unminified so you should have a useful line number
firas3d commented 2017-03-23 22:22:26 +00:00 (Migrated from github.com)

Appreciate the help from a more experienced developer.
I couldnt include the js files without getting strange errors.

Can anyone help by downloading the adobe extendedscript and

#include "shim.js";
#include "jszip.js";
#include "xlsx.flow.js";
#include "xlsx.core.min.js";

http://www.adobe.com/mena_en/products/extendscript-toolkit.html

Big thanks,
Firas.

Appreciate the help from a more experienced developer. I couldnt include the js files without getting strange errors. Can anyone help by downloading the adobe extendedscript and ``` #include "shim.js"; #include "jszip.js"; #include "xlsx.flow.js"; #include "xlsx.core.min.js"; ``` http://www.adobe.com/mena_en/products/extendscript-toolkit.html Big thanks, Firas.
SheetJSDev commented 2017-03-23 23:25:45 +00:00 (Migrated from github.com)

There were a few issues with the shims assuming that self existed, as well as a strange regexp bug (apparently you can't start a regular expression literal with =). I made the requisite changes to shim.js and jszip.js here: https://gist.github.com/SheetJSDev/449989cd7b47b5c5d8a8a2d829b368c0

The ExtendScript I tested was:

#include "shim.js";
#include "jszip.js";
#include "xlsx.flow.js";

XLSX.read

and it appeared to work:

screen shot 2017-03-23 at 19 22 02

Download the shim.js and jszip.js files from the link and see if those work. Once that works, try reading the file in (as a binary string or byte array or whatever extendscript does) and call XLSX.read(data)

There were a few issues with the shims assuming that `self` existed, as well as a strange regexp bug (apparently you can't start a regular expression literal with `=`). I made the requisite changes to `shim.js` and `jszip.js` here: https://gist.github.com/SheetJSDev/449989cd7b47b5c5d8a8a2d829b368c0 The ExtendScript I tested was: ```js #include "shim.js"; #include "jszip.js"; #include "xlsx.flow.js"; XLSX.read ``` and it appeared to work: <img width="380" alt="screen shot 2017-03-23 at 19 22 02" src="https://cloud.githubusercontent.com/assets/6070939/24274381/34e3431c-0ffe-11e7-8b8e-9e28b5ca153e.png"> Download the shim.js and jszip.js files from the link and see if those work. Once that works, try reading the file in (as a binary string or byte array or whatever extendscript does) and call `XLSX.read(data)`
firas3d commented 2017-03-24 03:12:46 +00:00 (Migrated from github.com)

Big thanks @SheetJSDev , an error xlsx.flow.js when calling
XLSX.readFile('test.xlsx');

#include "shim.js";
#include "jszip.js";
#include "xlsx.flow.js";

XLSX.read;

//not sure on this line, but no errors  
if(typeof require !== 'undefined') XLSX = require('xlsx');

var workbook = XLSX.readFile('test.xlsx');

var first_sheet_name = workbook.SheetNames[0];
var first_worksheet = workbook.Sheets[first_sheet];
var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1});

image

Appreciate 👍

Big thanks @SheetJSDev , an error xlsx.flow.js when calling `XLSX.readFile('test.xlsx'); ` ``` #include "shim.js"; #include "jszip.js"; #include "xlsx.flow.js"; XLSX.read; //not sure on this line, but no errors if(typeof require !== 'undefined') XLSX = require('xlsx'); var workbook = XLSX.readFile('test.xlsx'); var first_sheet_name = workbook.SheetNames[0]; var first_worksheet = workbook.Sheets[first_sheet]; var data = XLSX.utils.sheet_to_json(first_worksheet, {header:1}); ``` ![image](https://cloud.githubusercontent.com/assets/17760039/24279041/e3e797f2-1060-11e7-89ef-6e4fc7574b3a.png) Appreciate 👍
SheetJSDev commented 2017-03-24 04:19:14 +00:00 (Migrated from github.com)
screen shot 2017-03-24 at 00 14 29

Updated the jszip script, new file at https://gist.github.com/SheetJSDev/5573c7c02c47421b17185a73dc5497f6 -- long story short, none of the usual global suspects (global, window, self) are available, so you have to poke a hole in app to pass back variables from #include'd scripts

Here's some sample code to read
sheetjs.xlsx

#include "shim.js";
#include "jszip.js";
var JSZip = app.JSZip;
#include "xlsx.flow.js";

var filename = "sheetjs.xlsx";
var infile = File(filename);
infile.open("r");
infile.encoding = "binary";
var data = infile.read();
var workbook = XLSX.read(data, {type:"binary"});
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);

For future reference: the guide is available at https://www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf
File is the extendscript object for reading files. To read as a binary string, set the encoding to "binary" and then read the file.

Test it out and let us know if it works!

<img width="354" alt="screen shot 2017-03-24 at 00 14 29" src="https://cloud.githubusercontent.com/assets/6070939/24280471/3b8530b2-1027-11e7-9e61-41f3f5c99274.png"> Updated the jszip script, new file at https://gist.github.com/SheetJSDev/5573c7c02c47421b17185a73dc5497f6 -- long story short, none of the usual global suspects (global, window, self) are available, so you have to poke a hole in `app` to pass back variables from `#include`'d scripts Here's some sample code to read [sheetjs.xlsx](https://github.com/SheetJS/js-xlsx/files/866864/sheetjs.xlsx) ```js #include "shim.js"; #include "jszip.js"; var JSZip = app.JSZip; #include "xlsx.flow.js"; var filename = "sheetjs.xlsx"; var infile = File(filename); infile.open("r"); infile.encoding = "binary"; var data = infile.read(); var workbook = XLSX.read(data, {type:"binary"}); 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); ``` For future reference: the guide is available at https://www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf `File` is the extendscript object for reading files. To read as a binary string, set the encoding to `"binary"` and then read the file. Test it out and let us know if it works!
firas3d commented 2017-03-24 06:31:47 +00:00 (Migrated from github.com)

not working on my side yet,
i guess my xlsx.flow.js is different than yours.
Can you zip the 3 files ? shim.js, jszip.js and xlsx.flow.js

image

not working on my side yet, i guess my xlsx.flow.js is different than yours. Can you zip the 3 files ? shim.js, jszip.js and xlsx.flow.js ![image](https://cloud.githubusercontent.com/assets/17760039/24283046/bf0ecf42-107c-11e7-8323-0a6be2041d27.png)
SheetJSDev commented 2017-03-24 06:42:59 +00:00 (Migrated from github.com)

extend.zip

for good measure I also included the test file I was using and the test.jsx file.

I can actually reproduce that error, it's most likely coming because extendscript is looking in the wrong directory. To test this, in the javascript console run Folder.current. If it doesn't show the directory where sheetjs.xlsx resides, either change Folder.current in the console OR change the filename variable to the full path of the excel file (error only occurs in OSX; in windows, if you double-click the test.jsx file, extendscript toolkit automatically sets the working directory)

[extend.zip](https://github.com/SheetJS/js-xlsx/files/867071/extend.zip) for good measure I also included the test file I was using and the test.jsx file. I can actually reproduce that error, it's most likely coming because extendscript is looking in the wrong directory. To test this, in the javascript console run `Folder.current`. If it doesn't show the directory where sheetjs.xlsx resides, either change `Folder.current` in the console OR change the filename variable to the full path of the excel file (error only occurs in OSX; in windows, if you double-click the test.jsx file, extendscript toolkit automatically sets the working directory)
firas3d commented 2017-03-24 06:49:30 +00:00 (Migrated from github.com)

yes, reopened extendscript, and all works fine.
again, appreciate your help.
I wonder when do you sleep?

yes, reopened extendscript, and all works fine. again, appreciate your help. I wonder when do you sleep?
SheetJSDev commented 2017-03-24 06:53:21 +00:00 (Migrated from github.com)

If you find any other issues, let us know. Going to close this issue and take a nap :)

If you find any other issues, let us know. Going to close this issue and take a nap :)
firas3d commented 2017-03-24 17:22:53 +00:00 (Migrated from github.com)

small issu when connecting target application.
All works fine if target is ExtendScript Toolkit CC

But if it is changed to Adobe Illustrator for example, or adding #target illustrator line, the error
Error : jszip does not have a constructor
inside xlsx.flow.js

#target illustrator

// this is the path of the script
var thisFile = new File($.fileName);  
var basePath = thisFile.path;  

//xlsx library from SheetJSDev >> https://github.com/SheetJS/js-xlsx/issues/603#issuecomment-288840414
#include "shim.js";
#include "jszip.js";
var JSZip = app.JSZip;
#include "xlsx.flow.js";

//xlsx file
var filename = "/sheetjs.xlsx";
var infile = File(basePath+filename);

infile.open("r");
infile.encoding = "binary";
var data = infile.read();
var workbook = XLSX.read(data, {type:"binary"});
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);

image

small issu when connecting target application. All works fine if target is **ExtendScript Toolkit CC** But if it is changed to Adobe Illustrator for example, or adding `#target illustrator` line, the error Error : jszip does not have a constructor inside xlsx.flow.js ``` #target illustrator // this is the path of the script var thisFile = new File($.fileName); var basePath = thisFile.path; //xlsx library from SheetJSDev >> https://github.com/SheetJS/js-xlsx/issues/603#issuecomment-288840414 #include "shim.js"; #include "jszip.js"; var JSZip = app.JSZip; #include "xlsx.flow.js"; //xlsx file var filename = "/sheetjs.xlsx"; var infile = File(basePath+filename); infile.open("r"); infile.encoding = "binary"; var data = infile.read(); var workbook = XLSX.read(data, {type:"binary"}); 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); ``` ![image](https://cloud.githubusercontent.com/assets/17760039/24305864/e8651d14-10d7-11e7-8af6-d02121ac49ae.png)
SheetJSDev commented 2017-03-24 19:21:28 +00:00 (Migrated from github.com)

So I downloaded a trial of Illustrator CC 2017 for OSX and I can't reproduce. It also works if i remove the target line and use Photoshop CC 2017.

Are you on windows or mac? Also, can you add the line alert(JSZip) right after the var JSZip = app.JSZip; and see that it is being loaded?

So I downloaded a trial of Illustrator CC 2017 for OSX and I can't reproduce. It also works if i remove the `target` line and use Photoshop CC 2017. Are you on windows or mac? Also, can you add the line `alert(JSZip)` right after the `var JSZip = app.JSZip;` and see that it is being loaded?
firas3d commented 2017-03-25 11:06:31 +00:00 (Migrated from github.com)

with #target illustrator
alert(JSZip);
image

without #target illustrator
a working script :
alert(JSZip);
image

using windows10

with` #target illustrator` alert(JSZip); ![image](https://cloud.githubusercontent.com/assets/17760039/24321699/7c74e068-116c-11e7-9d13-572de4973beb.png) without` #target illustrator` a working script : alert(JSZip); ![image](https://cloud.githubusercontent.com/assets/17760039/24322061/db2f135e-1175-11e7-80d3-8f73ff6ff697.png) using windows10
SheetJSDev commented 2017-03-25 14:06:11 +00:00 (Migrated from github.com)

@firas3d just to be sure: if you take out the line #target illustrator but manually change the target app to Adobe Illustrator CC 2017, do you see the first or second popup?

@firas3d just to be sure: if you take out the line `#target illustrator` but manually change the target app to `Adobe Illustrator CC 2017`, do you see the first or second popup?
firas3d commented 2017-03-25 16:18:48 +00:00 (Migrated from github.com)

same, wither i put the line #target or link it from interface, the problem occur.

from what i understand, the JSZip library doesnt like #target application
but when i run the script from Adobe Illustrator (not from Adobe ExtendScipt toolkit) , it works fine.

same, wither i put the line #target or link it from interface, the problem occur. from what i understand, the JSZip library doesnt like #target application but when i run the script from Adobe Illustrator (not from Adobe ExtendScipt toolkit) , it works fine.
SheetJSDev commented 2017-03-26 17:24:01 +00:00 (Migrated from github.com)

@firas3d we made some changes to the shim and jszip scripts. Please update both jszip.js and shim.js. We included a new test.jsx in the extendscript demo directory

@firas3d we made some changes to the shim and jszip scripts. Please update both [`jszip.js`](https://raw.githubusercontent.com/SheetJS/js-xlsx/master/jszip.js) and [`shim.js`](https://raw.githubusercontent.com/SheetJS/js-xlsx/master/shim.js). We included a new test.jsx in the [extendscript demo directory](https://github.com/sheetjs/js-xlsx/tree/master/demos/extendscript)
firas3d commented 2017-03-26 20:48:20 +00:00 (Migrated from github.com)

super, will test it,
The #target app problem was showing on one machine only, so i guess all good.

super, will test it, The **_#target app_** problem was showing on one machine only, so i guess all good.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#603
No description provided.