Wednesday, February 21, 2007

 

Locating a Table Cell

They say that necessity is the mother of invention. Yesterday I was faced with needing to know the exact location of the top of a cell in a table so I could precisely position an anchored object. The x-coordinate of the cell was no problem because this table filled the column from side to side, so my anchored rectangle was easy to position horizontally, but vertically is another thing entirely.

It's worth stepping back a bit to explain what I was trying to achieve. I had two documents each with a huge table in it, over 300 rows and 7 or 8 columns. I wanted to have alternating fills but I could use the regular table features because the fills needed to be transparent so that the complex background could still be seen.

I conceived of the idea of putting an exactly sized rectangle over every other row by inserting it as an anchored item in the first character position of the first cell of each affected row. This would allow the "fill" to be transparent without the rest of the table also having to be transparent. It was the work of a moment to create the necessary object style to apply the fill and transparency and to set up the anchored object settings. But the heights of the rows vary and so the offset from the baseline of the text also needed to be calculated for each row. Here's the function I constructed to do that:
  function sizeAndPostionRect(table, row, rect) {
row.cells[0].texts[0].recompose();
var width = table.width;
var height = row.cells[0].height;
var myBounds = rect.geometricBounds;
myBounds[0] = myBounds[2] - height;
myBounds[3] = myBounds[1] + width;
rect.geometricBounds = myBounds;
var myCell = row.cells[0];
var tI = myCell.topInset
var fB = myCell.firstBaselineOffset;
myCell.firstBaselineOffset = FirstBaseline.fixedHeight;
myCell.minimumFirstBaselineOffset = 0;
myCell.verticalJustification = VerticalJustification.topAlign;
var myCellTop = myCell.texts[0].characters[1].baseline - tI;
myCell.verticalJustification = VerticalJustification.centerAlign
myCell.firstBaselineOffset = fB;
rect.anchoredObjectSettings.anchorYoffset = myCellTop - myCell.texts[0].characters[1].baseline;
}
The trick is to maneuver the baseline of the text in the cell so it sits on the top edge of the cell, then having grabbed the value, put everything back the way it was.

Comments:
Hy Dave,
I am simply amazed by you... it´s so impressive what you are doing, especially at your age.
I´ve been searching the Internet for a little bit help conceirning Javascript and Indesign, and I´m doing some scripting my self. My current project is exporting and Indesign document to an XML file.

My problem is that by default when I open a new document in Indesign, no other tag except the root tag shows up in the Structure Pane of the Document.
To get the structure pane populated with the elemenets(items) I have to go to Structure -> Add untagged Items. I don´t want them to appear in a particular order or with a particular tag, I just want that all objects in the document should appear in the structure view. Can I do this from a script?
I´ve gone trough the reference myself.... I can show/hide the structure pane, hide/show the attributes, I can set the exporting options... but I can´t find a single property or method so I can add all items to the structure pane.
Do I have to parse in js the entire document and add all the items myself in the structure or is there any method/property whatever that would do this for me???

Once again, conngrats for you activity! It´s very good to know there are people out there enjoying programming :-)
Wish you a great day...
 
You can't add untagged items to the structure. It's the tagging that makes them part of the structure. You could create a single tag and just tag everything with that, I suppose.

XML is one of those gray areas for me. I've not made much use of it in my work and so I have some insights into how to work it but I am uncomfortable giving advice in that area -- at least for now.

I think perhaps a part of the problem is the very word "structure" -- with respect to XML it has a specific technical meaning, represented by what you see in the structure pane when you import XML or tag parts of your document. For the newcomer, "structure" sounds as though it refers to the structure of your document -- the manifestation of the design, if you like -- but that is a colloquial use of the word that offends XML people. Indeed, when I recently had an exchange with a very well known InDesign expert where I spoke of the XML structure and the design structure of a document, he almost became abusive of my conceptual view.

Thanks for the kind words. To quote a famous English comic: when you get my age, it helps to pass the time.
 
Hello Dave,
I need to select unformatted tables that were placed from a Word doc and then apply "Table Style 1" to them all.

There are hundreds of little tables in this doc and I'm having to apply the style one-by-one...please help me :-)
 
Hi,
I'm trying to apply a TABLE STYLE to a table, with javascript. I'm working with indesign cs3.

Can you help me??? thanks a lot!!!
Mick from Italy
 
I am also interested in using JavaScript to select all tables imported to Indesign CS4 from Word. Please.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?