Skip navigation
Currently Being Moderated

[CS3/CS4 JS] How can you get the associatedXMLElement of a merged cell in a table?

Jun 16, 2009 3:36 AM

Hi!

 

Inserting a table, and autotagging it, I get a table with a number of cell elements in the XML Structure.

 

Selecting an item in the table, I can find the associated XML by the following line of code:
app.selection[0].associatedXMLElement

 

My problems begin when cells are merged. Then the associatedXMLElement for the cell returns null.
How can I find the associatedXMLElement for a merged cell?

 

Using the getElements makes no difference.
app.selection[0].getElements()[0].associatedXMLElement
(returns null)

 

In the XML structure I can see that the merged cell is still associated to an XML Element, which becomes underlined and also referrs back to the cell, selecting it when double clicking the XML Element link in the structure.

 

Is there no way to get to the xml element of a merged cell?

 

I have tested in CS3 and CS4 as well, and they act in the same way.

 

I also found a similar, unanswered, question from Anne-Laure Jallon in the "With CS3, some things have changed" ( http://forums.adobe.com/message/1105813#1105813 ):

 

Hello,
I'm working with VBscript.
Is there a difference between cell.associatedXmlElement in CS2 and CS3?
All my cells in CS2 had an associatedXmlElement.
In CS3, my table has an associatedXmlElement, but all its cells don't (The value is Nothing)
Is this a bug? Is it linked with XML evolution?   Thanks Anne Laure

 

 

Adding some more info:

 

I made a test, by selecting the XMLElement in the structure, and from that object finding the cell, and finding back to the assiciatedXMLElement:

 

app.selection[0].getElements()[0].cells[0].associatedXMLElement
Result: [object XMLElement]

 

So that kind of "chain" works.

 

But with the merged cell as only reference, I can't find its associatedXMLElement. Any ideas would be appreciated.

 


Best regards,
Andreas Jansson

 

Message was edited by: Andreas Jansson

 
Replies
  • Currently Being Moderated
    Jun 16, 2009 3:01 PM   in reply to Andreas Jansson

    If anyone can translate this AppleScript into JS then this might work

    This works with  the cell selected. I'd figure out the JS but i figure someone else will pick it up.

     

     

    tell application "Adobe InDesign CS3"
         set mycell to object reference of selection
         set parentelement to associated XML element of parent of mycell
         set myelement to object reference of every XML element of parentelement whose XML content is mycell
    end tell

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 17, 2009 12:19 AM   in reply to Randomsen

    Here is a JS version:

     

    var myCell = app.selection[0];
    var parentElement = myCell.parent.associatedXMLElement;
    var myElements = parentElement.xmlElements;
    for (i = 0; i < myElements.length; i++) {
        if (myElements[i].contents == myCell.contents) {
            var myElement = myElements[i];
        }
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 17, 2009 1:51 PM   in reply to Kasyan Servetsky

    Hey Kasyan, that's perfect, I just replaced testing the contents of the cell, to the xmlContent property so its testing for the object ref instead.

     

    So going back to the orginal question... you could evaluate the cell to make sure to only get null if there's no associated xml element.

     

     

    var myCell = app.selection[0];
    try{var myElement = myCell.associateXMLElement}
    catch(myerror){
    var parentElement = myCell.parent.associatedXMLElement;
    var myElements = parentElement.xmlElements;
    for (i = 0; i < myElements.length; i++) {
        if (myElements[i].xmlContent == myCell) {
            var myElement = myElements[i];
        }
    }
    }
    //myElement.toSource();

     

    If the table has no associated xml either, than you could put another try catch.

    ~mike

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 3, 2010 3:15 AM   in reply to Randomsen

    In my opinion, locate a cell according to his content is not so effortable. What happens if contents of more than two cells are equals?

     

    I take xml elements of associated xml element of table and put them into an array.

    This array contains associated xml elements of every cell ordered by cell positions into table.

     

    Now, locate associated xml element of a cell based on its array position (index) is more reliable:

     

    var myCell_cell = app.selection[0];

    var myElement = myCell.associateXMLElement

    if (!myElement || !myElement.isValid)  {

         var table =  myCell.parent;
         var xml_tab = table.associatedXMLElement;
         var xml_cells_arr = xml_tab.xmlElements.everyItem().getElements();

         var idx = myCell.index;

         myElement = xml_cells_arr[idx];

    }

     

     

     

    Alex ;-)

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 3, 2010 11:05 AM   in reply to Alex_Techne

    The great thing about opinions are that they're neither right nor wrong.

     

    I would point out that in my version I'm not testing the actual content, but the xmlContent object reference which has an ID and disambiguates the cells that may have the same content returned as a string. An object reference is as reliable as you can get.

     

    Did you test that? I can tell you haven't because your variable names don't match (var myCell_cell = app.selection[0]; and myCell.index ) and you misspelled the property associatedXMLElement (var myElement = myCell.associateXMLElement).

     

    ~mike

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 4, 2010 12:00 AM   in reply to Randomsen

    Hello Mike, of course you are right!

     

    Error code is a mistake of cut'n paste from my ESTK to adapt my code to exixting.

     

    This code would be correct:

     

    var myCell = app.selection[0];

    var myElement = myCell.associatedXMLElement

    if (!myElement || !myElement.isValid)  {

         var table =  myCell.parent;
         var xml_tab = table.associatedXMLElement;
         var xml_cells_arr = xml_tab.xmlElements.everyItem().getElements();

         var idx = myCell.index;

         myElement = xml_cells_arr[idx];

    }

     

    Alex

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points