Copy link to clipboard
Copied
How to transfer list of lists between js and jsx in Indesign panel?
JS side:
var listOfList = [
[1,2,3],
[2, "2", "hello"]
];
ata = JSON.stringify( listOfList )
var script = 'listTransfer( ' + ata + ');'
csInterface.evalScript(script);
JSX side:
#include "json_p.js"
function listTransfer(inpuList){
var listOfLists = JSON.parse(inpuList)
return listOfLists
}
And this above does not work.
The real list of list I need to transfer is read from excel file (content of each cell is element in list which is part of a bigger list).
Maybe I could try to export as csv from js and then import from jsx.
But any suggestion how to improve this or some other approch?
Copy link to clipboard
Copied
Hi @livl17017666 , This thread might help—script to convert an .xls table into a nested array. See @Peter Kahrel ’s simplified code at the end:
Copy link to clipboard
Copied
var xlsf = File.openDialog("Please Choose an Excel file");
alert("Contents of row 2 cell 3: \r" + getExcelArray(xlsf)[1][2]);
/**
* Converts an Excel table into a nested array
* @ param the Excel file path
* @ return a nested array for the table
*/
function getExcelArray(xf){
var doc = app.activeDocument;
var temp = doc.pages[0].textFrames.add ({geometricBounds: ["0pts","0pts","1000pts","1000pts"], textFramePreferences: {ignoreWrap: true,}});
try {
temp.place(xf);
var xta = temp.parentStory.tables[0].rows.everyItem().contents;
}catch(e) {
temp.remove()
alert(e)
}
temp.remove();
return xta;
}
Copy link to clipboard
Copied
And this above does not work.
By @livl17017666
Ok but what does this return:
csInterface.evalScript(script, function(message){
console.log(message);
});
What is it in your debugger?
Is your function even called? Easy to check:
JSX side:
alert("trying to load json_p");
#include "json_p.js"
alert("JSON is "+(typeof JSON));
function listTransfer(inpuList){
alert("I am called");
var listOfLists = JSON.parse(inpuList)
alert("listOfLists is of type:" + (typeof listOfLists));
return listOfLists
}
At the moment, you can already investigate to find the root of your problem.
Copy link to clipboard
Copied
Yes I did something similar on Sunday.
And what was sent to jsx was acctually array not string.
With this line of code from js:
ata = JSON.stringify( listOfList )
var script = "listTransfer(" + ata + ")";
csInterface.evalScript(script);
I expetcted that it will be the string.
Copy link to clipboard
Copied
So did it not work? Add in alert on both ends and see what you get. You would have to convert string to object on the jsx end as well.
-Manan
Copy link to clipboard
Copied
@Manan Joshi yes this initial code I posted worked but with removing JSON.parse(inpuList).
Couple of min after post I accidentaly discover that what jsx get is not string but array (list of list) already.
I did test typeof from js side but jsx I did not, I was confident that it will get string which was mistake.
As @Loic.Aigon suggested testing needed on both side although to see this needed before JSON.parse(inpuList).
So this is why JSON.parse(inpuList) did not worked or needed.
I though that error was about how JS library read excel but it was this..
By the way, should jsx here get string not array? If someone have coment about this welcome?
It could be that this is because I used older CSInterface.js not sure..
JS side:
var listOfList = [
[1,2,3],
[2, "2", "hello"]
];
ata = JSON.stringify( listOfList )
var script = "listTransfer(" + ata + ")";
csInterface.evalScript(script);
JSX side:
#include "json_p.js"
function listTransfer(inpuList){
...
return listOfLists
}
Copy link to clipboard
Copied
Indeed, somehow sending stringified json from JS to JSX sometimes result in a native Object in the JSX with no need to JSON.parse();. I presume that while stringifying, it makes the "string" interpreted directly.
That's why it's worth it to look at the type of the argument and decide accordingly.
Copy link to clipboard
Copied
Thanks @livl17017666 and @Loic.Aigon, this is something new that I learnt today. Somehow I did not face this so far and did not have to do this check. Anyhow, point noted, now I know when this happens.
-Manan