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

How to align table center using extend script?

New Here ,
Nov 26, 2021 Nov 26, 2021

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

psarun21954517c4kk_1-1637943528626.pngexpand image

 

Expected output

psarun21954517c4kk_0-1637943443299.pngexpand image

 

 

TOPICS
Scripting
1.3K
Translate
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

correct answers 1 Correct answer

Community Expert , Dec 03, 2021 Dec 03, 2021

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

...
Translate
Community Expert ,
Nov 28, 2021 Nov 28, 2021

Place the table in a paragraph that's set to centre-align.

Translate
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
New Here ,
Nov 29, 2021 Nov 29, 2021

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.

Translate
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 ,
Nov 30, 2021 Nov 30, 2021

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

Translate
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 ,
Nov 30, 2021 Nov 30, 2021

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 )

Translate
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
New Here ,
Dec 02, 2021 Dec 02, 2021

Hi Laubender,

I tried as per your suggestion. But, it is not working.

Translate
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 ,
Dec 02, 2021 Dec 02, 2021

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 )

Translate
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
New Here ,
Dec 03, 2021 Dec 03, 2021

Hi Laubender,

Below code returns 0

attNodessubscript[j].texts[0].characters[0].tables.length

Below code returns [object XMLElement]

attNodessubscript[j]
Translate
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 ,
Dec 03, 2021 Dec 03, 2021

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 )

Translate
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
New Here ,
Dec 03, 2021 Dec 03, 2021

Hi Laubender,

 

It returns

psarun21954517c4kk_0-1638540111162.pngexpand image

 

Translate
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
New Here ,
Dec 03, 2021 Dec 03, 2021

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

psarun21954517c4kk_0-1638541424705.pngexpand image

 

Translate
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 ,
Dec 03, 2021 Dec 03, 2021

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 )

Translate
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
New Here ,
Dec 03, 2021 Dec 03, 2021

Hi Laubender,

 

You need the xml file or indd file?

Translate
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 ,
Dec 03, 2021 Dec 03, 2021

The InDesign document that is showing in your screenshot.

 

Regards,
Uwe Laubender

( ACP )

Translate
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
New Here ,
Dec 03, 2021 Dec 03, 2021

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 

Translate
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 ,
Dec 03, 2021 Dec 03, 2021

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:

 

TableAlignmentCenter-1.PNGexpand image

 

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 )

 

Translate
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
New Here ,
Dec 06, 2021 Dec 06, 2021

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.

 

Translate
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 ,
Dec 03, 2021 Dec 03, 2021

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:

 

TableAlignmentCenter-2.PNGexpand image

 

Regards,
Uwe Laubender

( ACP )

Translate
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
New Here ,
Dec 06, 2021 Dec 06, 2021

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.

psarun21954517c4kk_0-1638782268976.pngexpand image

 

Translate
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 ,
Dec 06, 2021 Dec 06, 2021

"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:

TableAlignmentCenter-0.PNGexpand image

 

 

If there would be align = center on the other para that is holding the inner table:

 

TableAlignmentCenter-AttributeAdded-1.PNGexpand image

 

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:

 

TableAlignmentCenter-AttributeAdded-2.PNGexpand image

 

Regards,

Uwe Laubender

( ACP )

Translate
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
New Here ,
Dec 06, 2021 Dec 06, 2021
LATEST

Hi Laubender,

 

Thanks for your guidance.

Translate
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