Saturday, July 29, 2006

 

Addressing a Selection inside a Table

I'm working on a script this morning that is intended to equalize the spare space in part of a table so that the extra space is equally distributed across the selected columns. Life would be easier if the script had to work on the whole table, but that's not what I need. I need it to work on a selection of rows and columns. So, the first thing the script needs to do is workout which part of the table is selected.

You'd think this would be easy, but there is a bug (or an implementation decision) that really bites if you go at this what seems the reasonable way:
if ((app.documents.length == 0) || (app.selection.length == 0)) { exit() }

aDoc = app.activeDocument;
aSel = app.selection[0];

try {
theCols = aSel.columns;
theRows = aSel.rows;
} catch (e) {
alert("Selection needs to be in a table"), exit();
}

alert(theCols[0].cells[0].contents);
Run this with a selection that does not include the top-left corner of the table and you'll be surprised to see that the alert gives you the contents of that top-left cell. It appears that when asking for the columns or rows of a selection within a table in this straightforward manner returns the wrong columns and rows.

So watch out. I'll report back later on the technique I end up using to get around this.

Comments: Post a Comment

<< Home

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