Copy link to clipboard
Copied
Hi,
Using import xml i add the content in indesign 2020. Defaultly table show in left alignment. But i need the center alignment.
My xml is
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
<para ids="div" align="center"><emphasis>
<table xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" type="simple_div" aid:trows="1" aid:tcols="1">
<cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="500">
<para border="1" ids="table">
<emphasis>
<table xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" type="simple_table" aid:trows="19" aid:tcols="2">
<cell aid:table="cell" valign="top" style="width:86.4pt" type="simple_table" aid5:cellstyle="allEdgeStrokeWeight" aid:ccolwidth="115" aid:crows="1" aid:ccols="1"><para align="left" ids="Body1"><emphasis ids="normal">test</emphasis></para></cell> <cell aid:table="cell" valign="top" style="width:180.0pt" type="simple_table" aid5:cellstyle="allEdgeStrokeWeight" aid:ccolwidth="170" aid:crows="1" aid:ccols="1"><para align="left" ids="Body1"><emphasis ids="normal">test</emphasis></para></cell>
<cell aid:table="cell" valign="top" style="width:86.4pt" type="simple_table" aid5:cellstyle="allEdgeStrokeWeight" aid:ccolwidth="115" aid:crows="1" aid:ccols="1"><para align="left" ids="Body1"><emphasis ids="normal">test</emphasis></para></cell>
<cell aid:table="cell" valign="top" style="width:180.0pt;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt" type="simple_table" aid5:cellstyle="rbEdgeStrokeWeight" aid:ccolwidth="170" aid:crows="1" aid:ccols="1"><para align="left" ids="Body1"><emphasis ids="normal">test</emphasis></para></cell>
</table>
</emphasis>
</para>
</cell>
</table>
</emphasis></para>
</Root>
Current output
Expected output
1 Correct answer
If you run this code on your sample you could detect two paragraphs with tables:
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var attNodessubscriptLength = attNodessubscript.length;
for( var n=0; n<attNodessubscriptLength; n++ )
{
$.writeln( n +"\t"+ attNodessubscript[n].texts[0].tables.length );
};
Result:
/*
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1
13 0
14 1
15 0
16 0
17 0
18 0
*/
So if you want to align both
...Copy link to clipboard
Copied
Place the table in a paragraph that's set to centre-align.
Copy link to clipboard
Copied
Hi Peter,
Using script, I try this
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var j;
for (j = 0; j < attNodessubscript.length; j++)
{
try
{
var getParaAttIds = attNodessubscript[j].xmlAttributes.itemByName("ids").value;
if(getParaAttIds=='div'){
var getParaAttAlign = attNodessubscript[j].xmlAttributes.itemByName("align").value;
if (getParaAttAlign == "center")
{
attNodessubscript[j].justification = Justification.CENTER_ALIGN;
}
}
}
catch(e){
alert(e.line+": "+e.message);
}
}
But it is not working.
Copy link to clipboard
Copied
You apply centre-aligning to an attNodessubscript item, which is an XML element. Maybe you should target the paragraph contained in it. (I'm afraid that I don't have much experience with (Adobe's) XML.)
Copy link to clipboard
Copied
Hi psarun21954517c4kk,
your object attNodessubscript[j] has no property named justification.
You have to address an insertion point of that object or perhaps better texts[0] of the object.
Look up the properties of object XMLElement to see what's possible:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#XMLElement.html
This should work ( not tested )
attNodessubscript[j].texst[0].justification = Justification.CENTER_ALIGN;
Or even this:
attNodessubscript[j].insertionPoints[0].justification = Justification.CENTER_ALIGN;
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
I tried as per your suggestion. But, it is not working.
Copy link to clipboard
Copied
Hi Hi psarun21954517c4kk,
what does:
attNodessubscript[j].texst[0].characters[0].tables.length;
return?
Or asked in a different way:
Are you sure that you get some text at all with:
attNodessubscript[j]
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
Below code returns 0
attNodessubscript[j].texts[0].characters[0].tables.length
Below code returns [object XMLElement]
attNodessubscript[j]
Copy link to clipboard
Copied
You may see into what this returns:
attNodessubscript[j].texts[0].contents
My assumption was that the first character of texts[0] contains a table.
Obviously this is not the case.
Hard to tell what's wrong without having the document at hand.
Maybe your code to filter the right paragraph is failing?
Or your evaluateXPathExpression() is the cause?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
It returns
Copy link to clipboard
Copied
Hi Laubender,
I try this
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var j;
var errormesg = '';
var successmsg = '';
for (j = 0; j < attNodessubscript.length; j++)
{
try
{
var getParaAttIds = attNodessubscript[j].xmlAttributes.itemByName("ids").value;
if(getParaAttIds=='div'){
var getParaAttAlign = attNodessubscript[j].xmlAttributes.itemByName("align").value;
if (getParaAttAlign == "center")
{
var getDivTables = attNodessubscript[j].xmlElements[0].evaluateXPathExpression("//table");
for(var tj = 0; tj < getDivTables.length; tj++)
{
if(getDivTables[tj] && tj==0){
app.select(getDivTables[tj]);
}
}
}
}
}
catch(e){
alert(e.line+": "+e.message);
}
}
it is select in table structure
Copy link to clipboard
Copied
Thanks for that screenshot.
Hm. The resolution is a bit low, but I think your table sits in its own text frame that is anchored to that paragraph you are able to identify. Could you share the document? Put it on Dropbox or a similar service and post the download link.
So principally you first have to minimize the width of that text frame that it is the width of the table and then set the paragraph holding the anchored frame to justified center. Could be that both steps do not yield the right result, because of the anchored object values that are in place. E.g. if the text frame is anchored relative to a page edge.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
You need the xml file or indd file?
Copy link to clipboard
Copied
The InDesign document that is showing in your screenshot.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
I upload the xml, jsx and indd file in dropbox. Use the below link to download.
https://www.dropbox.com/sh/oplcu0e9h9qmcnj/AAASkRm3OAzzBGF0i8kbvDzma?dl=0
Copy link to clipboard
Copied
Looked into your InDesign document.
The case is a bit different that I thought.
The table you are seeing with the 12 cells showing "test" as contents is part of a table cell of a differnt table.
This table is a "nested" table:
Now the question is what you really have to do.
Center the inner table to the width of the outer cell?
Center the inner table to the width of the text frame?
And if so, should the cell that holds the table be the width of the text frame?
( Currently it is not. )
Move the inner table out of the cell and remove the table with that one cell only?
I think with that nested table something went wrong when the XML is imported.
Should there be a nested table? Really?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
Center the inner table to the width of the outer cell? - Yes
Center the inner table to the width of the text frame? - Yes
And if so, should the cell that holds the table be the width of the text frame? - Yes
Move the inner table out of the cell and remove the table with that one cell only? - No
Should there be a nested table? Really? - Yes, I need the nested table.
Copy link to clipboard
Copied
If you run this code on your sample you could detect two paragraphs with tables:
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var attNodessubscriptLength = attNodessubscript.length;
for( var n=0; n<attNodessubscriptLength; n++ )
{
$.writeln( n +"\t"+ attNodessubscript[n].texts[0].tables.length );
};
Result:
/*
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1
13 0
14 1
15 0
16 0
17 0
18 0
*/
So if you want to align both tables to center align you can do this like that:
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var attNodessubscriptLength = attNodessubscript.length;
for( var n=0; n<attNodessubscriptLength; n++ )
{
// $.writeln( n +"\t"+ attNodessubscript[n].texts[0].tables.length );
if( attNodessubscript[n].texts[0].tables.length > 0 )
{
attNodessubscript[n].texts[0].justification = Justification.CENTER_ALIGN ;
};
};
Result:
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
As per your guidance, i tried this
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var attNodessubscriptLength = attNodessubscript.length;
for( var n=0; n<attNodessubscriptLength; n++ )
{
try
{
var getParaAttIds = attNodessubscript[n].xmlAttributes.itemByName("ids").value;
if(getParaAttIds=='div' || getParaAttIds=='table' ){
if( attNodessubscript[n].texts[0].tables.length > 0 )
{
var getParaAttAlign = attNodessubscript[n].xmlAttributes.itemByName("align").value;
if (getParaAttAlign == "center")
{
attNodessubscript[n].texts[0].justification = Justification.CENTER_ALIGN;
}else if (getParaAttAlign == "right"){
attNodessubscript[n].texts[0].justification = Justification.RIGHT_ALIGN;
}else if (getParaAttAlign == "left"){
attNodessubscript[n].texts[0].justification = Justification.LEFT_ALIGN;
}
}
}
}
catch(e){
$.writeln(e.line+": "+e.message);
}
}
It is working fine.
Copy link to clipboard
Copied
"Center the inner table to the width of the outer cell? - Yes
Center the inner table to the width of the text frame? - Yes
And if so, should the cell that holds the table be the width of the text frame? - Yes
Move the inner table out of the cell and remove the table with that one cell only? - No
Should there be a nested table? Really? - Yes, I need the nested table."
Then one of your para's in your XML is missing an attribute because you need to center two tables:
If there would be align = center on the other para that is holding the inner table:
you could work with code like that:
var doc = app.activeDocument;
var attNodessubscript = doc.xmlElements[0].evaluateXPathExpression("//para");
var attNodessubscriptLength = attNodessubscript.length;
for( var n=0; n<attNodessubscriptLength; n++ )
{
if
(
attNodessubscript[n].texts[0].tables.length > 0
&&
attNodessubscript[n].xmlAttributes.itemByName("align").value == "center"
)
{
attNodessubscript[n].texts[0].justification = Justification.CENTER_ALIGN ;
};
};
to yield that result:
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Laubender,
Thanks for your guidance.

