• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
11

How to transfer list of list between js and jsx in Indesign panel?

Participant ,
Jun 25, 2023 Jun 25, 2023

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?

TOPICS
Scripting , SDK

Views

483

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 25, 2023 Jun 25, 2023

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:

 

https://community.adobe.com/t5/indesign-discussions/how-to-use-xlsx-extendscript-js/m-p/13782669#M52....

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 25, 2023 Jun 25, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 25, 2023 Jun 25, 2023

Copy link to clipboard

Copied

quoteAnd 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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 27, 2023 Jun 27, 2023

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.

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 27, 2023 Jun 27, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 28, 2023 Jun 28, 2023

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

}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 28, 2023 Jun 28, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines