Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using js-xlsx inside adobe extendscript #603

Closed
firas3d opened this issue Mar 23, 2017 · 20 comments
Closed

using js-xlsx inside adobe extendscript #603

firas3d opened this issue Mar 23, 2017 · 20 comments

Comments

@firas3d
Copy link

firas3d commented Mar 23, 2017

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
Copy link
Contributor

@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
Copy link
Author

firas3d commented Mar 23, 2017

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
Copy link
Contributor

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});

@firas3d
Copy link
Author

firas3d commented Mar 23, 2017

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
Copy link
Contributor

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

@firas3d
Copy link
Author

firas3d commented Mar 23, 2017

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
Copy link
Contributor

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)

@firas3d
Copy link
Author

firas3d commented Mar 24, 2017

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 👍

@SheetJSDev
Copy link
Contributor

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!

@firas3d
Copy link
Author

firas3d commented Mar 24, 2017

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

@SheetJSDev
Copy link
Contributor

SheetJSDev commented Mar 24, 2017

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
Copy link
Author

firas3d commented Mar 24, 2017

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

@SheetJSDev
Copy link
Contributor

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

@firas3d
Copy link
Author

firas3d commented Mar 24, 2017

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

@SheetJSDev
Copy link
Contributor

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?

SheetJSDev added a commit that referenced this issue Mar 25, 2017
- `aoa_to_sheet` function (fixes #314 h/t @fonzy2013 @rvdwijngaard)
- `writeFileAsync` function (fixes #396 h/t @barbalex)
- `sheet_to_json` tests + docs + blankrows (fixes #602 h/t @EEaglehouse)
- write number format scan now includes every index >= 50
- propagate SSF IE8 fixes (fixes #171 h/t @SheetJSDev)
- update shim for extendscript (see #603 h/t @firas3d)
- more flow type definitions
@firas3d
Copy link
Author

firas3d commented Mar 25, 2017

with #target illustrator
alert(JSZip);
image

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

using windows10

@SheetJSDev
Copy link
Contributor

@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
Copy link
Author

firas3d commented Mar 25, 2017

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
Copy link
Contributor

SheetJSDev commented Mar 26, 2017

@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
Copy link
Author

firas3d commented Mar 26, 2017

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

saarCiklum pushed a commit to Folcon/js-xlsx that referenced this issue Aug 19, 2020
- `aoa_to_sheet` function (fixes SheetJS#314 h/t @fonzy2013 @rvdwijngaard)
- `writeFileAsync` function (fixes SheetJS#396 h/t @barbalex)
- `sheet_to_json` tests + docs + blankrows (fixes SheetJS#602 h/t @EEaglehouse)
- write number format scan now includes every index >= 50
- propagate SSF IE8 fixes (fixes protobi#171 h/t @SheetJSDev)
- update shim for extendscript (see SheetJS#603 h/t @firas3d)
- more flow type definitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants