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

[JS-CS4] Associated XML element...

Explorer ,
Jun 01, 2010 Jun 01, 2010

Hi,

I'm going crazy with this.

I have a table built with XML import and I need to get the XML element associated with a cell.

I use this code:

cell.associatedXMLElement

but it always returns "null".

Obviously that cell has a content in XML structure...

Any idea why?

Thanks.

TOPICS
Scripting
2.8K
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
Mentor ,
Jun 01, 2010 Jun 01, 2010

How do you get your cell? This works for me:

$.writeln(app.activeDocument.textFrames.item(0).tables.item(0).cells.item(0).associatedXMLElement.markupTag.name);

Dirk

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 ,
Jun 01, 2010 Jun 01, 2010

no indesign to test around here.

there is a similar problem with characters/texts which are not positioned in the layout view. indesign can only access the associatedXMLElements property when the xml is placed. could be the same with cells!

this does not work:

xmlElement.characters[10].associatedXMLElements[0]

(by the way this makes sense if you check for child elements for a certain range)

gregor

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
Explorer ,
Jun 02, 2010 Jun 02, 2010

Here's my script:

var my_doc = app.activeDocument;

var my_table = my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem();

my_table.columns.firstitem().select();

var my_selection = doc.selection;

for(var c = 0; c < my_selection.length; c++){

     if(my_selection.associatedXMLElement.xmlAttributes.itemByName("type").value != "house"){

          my_selection.appliedCellStyle = doc.cellStyles.itemByName("column");

          my_selection.clearCellStyleOverrides(true);

     }

}

This will run once the table is placed through XML import

I need to check the "type" XML attribute value for every XML cell, that's why I'm performing that if statement.

Basically if a cell's XML element has a "type" attribute with "house" value the script will not apply the "column" style.

The problem is that

my_selection.associatedXMLElement

always return null.

The XML data structure is:

<List><Category><Table><Cell/><Cell/>....</Table></Category></List>

So I have a simple table inside a textFrame.

I know what I'm doing could sound weird, but I experienced it's the most quick way to handle a big table within ID.

Here's the background story

Thanks again.

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 ,
Jun 02, 2010 Jun 02, 2010

looks like a bug.

my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem().associatedXMLElement;

works fine.

i think you do not need a selection (which looks llike a performance killer):


var my_doc = app.activeDocument;


var _tableXML= my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem().associatedXMLElement;;


for(var c = 0; c < _tableXML.xmlElements.length; c++){


     if(_tableXML.xmlElements.xmlAttributes.itemByName("type").value != "house"){


        _tableXML.xmlElements.applyCellStyle (my_doc.cellStyles.itemByName("column"), true);


     }


}

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
Explorer ,
Jun 02, 2010 Jun 02, 2010

Thanks for your suggestions Grefel.

I'm testing all kinds of approach to format an XML table and I have to say that the selection method is the one that require less time when it comes to applying cell styles.

Basically I do the following:

1.     I import the XML (as you wisely suggested me) without applying styles (2 minutes);

2.     Once the table is placed I run a script to select every column and apply a style to all the cells inside the selection (3 minutes);

3.     I run another script that adapt culmns width to their contents (15 minutes).

Consider that I'm working with a table with 25460 cells and to loop with a FOR cycle through such many items takes a lot of time (I already tested and it tooks 5 hours).

By now this looks like the most efficent solution in therms of time and performance.

I just have to solve that XMLElement issue...

Thanks again.

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 ,
Jun 02, 2010 Jun 02, 2010

ok. sounds interesting...

solving the associatedXML problem:

_selection.texts[0].associatedXMLElements[0]

works around the bug, if you've got inline elements you have to check for this.

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
Explorer ,
Jun 02, 2010 Jun 02, 2010

The problem is that when you select a column the first item in the selection object is not an array of cells but an array of columns.

So within the selection if you want to reach a single cell you need to use this expression:

my_selection[0].cells

where c is the cell index.

my_selection[0].cells.associatedXMLElement

returns the XML element.

Hope this will help.

Bye.

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
Mentor ,
Jun 06, 2010 Jun 06, 2010

You can save some loop iterations if you use the collection feature of InDesign.

Basically this way you can assign the same value to a whole set of cells with one command:

myTable.columns.item(0).cells.everyItem().appliedCellStyle = redCellStyle;

Use that approach for the majority, and only run a loop* across the few exceptions.

*Unfortunately evaluateXPathExpression() yields an array, not a collection.

var rootXE = doc.xmlElements.item(0);
var houseXEs = rootXE.evaluateXPathExpression('//Cell[@type="house"]');
for( var i=0; i< houseXEs.length; i++ )
      houseXEs.xmlContent.appliedCellStyle = null;

Dirk

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
Explorer ,
Jun 14, 2010 Jun 14, 2010

Thanks for your suggestion Dirk but I came to the conclusion that when it comes to apply styles to such a huge table it's quicker to use the selection method. From my test I have to say it's about 20 or 30 times faster and, you know, I'm working with a 600 rows table so...

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
LEGEND ,
Jun 14, 2010 Jun 14, 2010

You can try using the x/y name of the cell rather thanresolving the reference. I believe it's much faster...

Harbs

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
Explorer ,
Jun 14, 2010 Jun 14, 2010

Sorry Harbs, but what do you mean by "using the x/y name of the cell" exactly?

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
LEGEND ,
Jun 14, 2010 Jun 14, 2010
LATEST

Select a cell and run this:

alert(app.selection[0].name);

It'll display the cell coordinates.

You can use table.cells.itemByName(coordinates), to specify a cell.

Of course tables with merged cells and columns can get a bit tricky...

Harbs

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