Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Size column to fit text?

270 views
Skip to first unread message

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 10:22:29 AM12/10/07
to
Just wanted to make sure I'm not missing the easy way. :^)

I've got a couple million little tables in a linguistics book, where two
different-language sentences are compared one above the other. Each word
in the top sentence has to align with its counterpart in the bottom
sentence. The author did this with lots of little tables, each word in
one cell.

This works okay, except that the manuscript was prepared in 12 pt type
and the book has to be done in 10 pt type, so every column in every
table has to be resized smaller. Framemaker has a "resize column to fit
text" command. It would be great if I could just swipe the table and
make it resize all the columns to fit. Does ID have something similar
buried somewhere, or is there a plugin or script somewhere that does this?

--
Kenneth Benson
Pegasus Type, Inc.
www.pegtype.com

Peter...@adobeforums.com

unread,
Dec 10, 2007, 10:56:49 AM12/10/07
to
I'm sure you do more work with tables than I do, so you probably already know you can resize all columns together by holding the shift key. Beyond that, I suspect you'd need a script.

Peter

Scott Falkner

unread,
Dec 10, 2007, 10:59:45 AM12/10/07
to
Rows can have their height set to a minimum. The text will force the row to expand beyond that it it needs more space.

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 11:29:27 AM12/10/07
to
No, I'm dealing only with width here. Row height is (or should be) set
to an At Least value, so that the row opens vertically if something runs
over, but almost none of these run over to a second line. I just need
something that will look at, e.g., "incontrato" in 10 pt Sabon and
decide that it occupies about 46 pts, add 3 more points for a space
after the word, and set the column width at 49 pts. Is this something
that could be scripted?

Peter Kahrel

unread,
Dec 10, 2007, 11:48:24 AM12/10/07
to
I use the column snapper, below. It runs only in the selected table. The script assusmes points as the default measurement; the "3" in the second line specifies 3 points space between the longest entry in a column and the right-hand side of that column.

Peter

#target indesign;

snap_all_columns (get_table(), 3)

function snap_all_columns (tbl, space)
{
for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;
var rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;
for ( j = 0; j < rightmargin.length ; j++ )
longest = Math.max (rightmargin[j], longest);
var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset
tbl.columns[i].width = ((longest - lm) + space)
}
}

function get_table()
{
if (app.selection.length > 0)
if (app.selection[0].parent instanceof Table)
return app.selection[0].parent;
else if (app.selection[0].parent.parent instanceof Table)
return app.selection[0].parent.parent;
alert ('Cursor not in a table\ror illegal selection.');
exit ();
}

Peter...@adobeforums.com

unread,
Dec 10, 2007, 11:59:00 AM12/10/07
to
Peter,

That looks pretty cool. Would it be difficult to make this traverse the document looking for tables? Ken said he had "a couple million" of them to do. :)

Peter S.

Peter Kahrel

unread,
Dec 10, 2007, 12:23:47 PM12/10/07
to
Sure:

#target indesign;

tables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (i = 0; i < tables.length; i++ )
snap_all_columns (tables[i], 3)

function snap_all_columns( tbl, space )

{
for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;

rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;

for ( var j = 0; j < rightmargin.length ; j++ )

longest = Math.max (rightmargin[j], longest);
var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset
tbl.columns[i].width = ((longest - lm) + space)
}
}

Peter

Peter Kahrel

unread,
Dec 10, 2007, 12:29:13 PM12/10/07
to
Ken,

In that case, under the last line in the script add a new line, so that it looks as follows:

tbl.columns[i].width = ((longest - lm) + space);
tbl.columns[0].width = 24
}
}

Why it should set the first columns to 363 points I don't know, doesn't happen to me. Anyway, by adding that new line the problem is irrelevant.

Peter

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 12:25:26 PM12/10/07
to
This is great, Peter. Thank you very much.

It does something funky with the first column (sets it to 363 pts),
which usually has just a number in parens like (12), but that doesn't
really affect things, because I have to consistently size that column at
24 pts regardless of the contents.

Very nice.

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 1:25:01 PM12/10/07
to
> tbl.columns[i].width = ((longest - lm) + space);
> tbl.columns[0].width = 24
> }
> }


This produces an error about too many braces, so I took the last one
out, and it works, but it doesn't resize the first column to 24 pts.

The "last line" that I should place this after, that's the exit line?

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 1:27:10 PM12/10/07
to
Note that I'm not using the second script you wrote. These all require a
certain amount of work before I can run your script (mostly applying
cell styles, paragraph styles, table style), so I'm running it one by one.

Peter Kahrel

unread,
Dec 10, 2007, 2:49:25 PM12/10/07
to
Ah, 'last line' refererred to the second script. Here's the first script in full with that line added.

Peter

#target indesign;

snap_all_columns (get_table(), 3)

function snap_all_columns (tbl, space)
{

for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;

var rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;
for ( j = 0; j < rightmargin.length ; j++ )

longest = Math.max (rightmargin[j], longest);

var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset;

tbl.columns[i].width = ((longest - lm) + space);

tbl.columns[0].width = 24;

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 4:06:25 PM12/10/07
to
Yes, this assigns the first column 24 pts.

Sounds like you know this work.

Since these are tables, and since the tables came from Word, I need a
way to fix the insets. I'm using cell styles (to fix the insets) and a
table style (to adjust space above and below to keep the whole thing on
grid). If I converted all the tables to text and then back to tables, I
could avoid the cell and table styles, but it seems like just as much work.

I'm also unmerging cells, deleting extra columns, and moving the parts
of the sentence that run off the page onto 2nd lines.

Peter Kahrel

unread,
Dec 10, 2007, 4:41:50 PM12/10/07
to

Sounds like you know this work.


Yep, I do a lot of linguistic setting.

Since these are tables, and since the tables came from Word, I need a
way to fix the insets. I'm using cell styles (to fix the insets) and a
table style (to adjust space above and below to keep the whole thing on
grid).


It's probably a matter of taste, but as the format of all those tables isn't going to change, instead of using all those styles I find it easier to clean up the table and apply just a paragraph style to all cells. This is the kind of script I use for that:

#target indesign

t = get_table();
clean_table (t);

function clean_table (tbl)
{
var pStyles = app.activeDocument.paragraphStyles;
if( pStyles.item ('example') != null)
tbl.cells.everyItem().texts[0].applyParagraphStyle (
pStyles.item ('example'), false);
with (tbl.cells.everyItem ())
{
//delete all rules
topEdgeStrokeWeight = 0;
bottomEdgeStrokeWeight = 0;
leftEdgeStrokeWeight = 0;
rightEdgeStrokeWeight = 0;
//set insets to zero
topInset = 0;
rightInset = 0;
bottomInset = 0;
leftInset = 0;
}
tbl.rows.everyItem().firstBaselineOffset = FirstBaseline.leadingOffset
tbl.spaceBefore = 0;
tbl.spaceAfter = 0;
}

function get_table()
{
if (app.selection.length > 0)
if (app.selection[0].parent instanceof Table)
return app.selection[0].parent;
else if (app.selection[0].parent.parent instanceof Table)
return app.selection[0].parent.parent;
alert ('Cursor not in a table\ror illegal selection.');
exit ();
}

The script applies the paragraph style 'example' to all cells if that style is present, sets all insets to zero, removes all rules, sets the baseline offset of each cell to leading (to keep them on the grid), and sets the space before and after the table.

I'm also unmerging cells, deleting extra columns, and moving the parts
of the sentence that run off the page onto 2nd lines.


There may not be much to script here as there is probably not much system in the way the cells have been merged.

Peter

Kenneth...@adobeforums.com

unread,
Dec 10, 2007, 8:35:14 PM12/10/07
to
> The script applies the paragraph style 'example' to all cells if that
> style is present, sets all insets to zero, removes all rules, sets
> the baseline offset of each cell to leading (to keep them on the
> grid), and sets the space before and after the table.

That's exactly what I'm doing when I apply a cell style and a table
style. Another thing I'm doing is removing row height settings.

Kenneth...@adobeforums.com

unread,
Dec 11, 2007, 11:04:13 AM12/11/07
to
> There's no need to apply cell and paragraph styles: you can specify a
> paragraph style in the cell style, and a cell style in the table
> style, so when the table style is applied, the cell and paragraph
> styles are applied automatically.


This doesn't seem to be true for me. I changed the table style so that
it's based on No Table Style, and I changed the cell style so that it's
based on None, just in case that was the problem. When I apply (or
ALT-apply) the table style, it doesn't change the cell style. And when I
run your final script (which applies a table style) it also does not
change the cell style (or the paragraph style).

This is why I've been doing it in 4 steps:

1) Select all in table and apply cell style
2) Apply paragraph style
3) Apply table style
4) Set row height to At Least 3 pts.

The order is important. If cell style is set for None, it applies No
Paragraph Style (so I follow this by ALT-applying the correct paragraph
style).

Including the paragraph style in the cell style means that steps 1 and 2
could be just one step, but if I use a different paragraph style, then
the cell that contains that different paragraph will show an exception
to the style. In addition, applying the paragraph style through the cell
style doesn't remove paragraph style overrides. Maybe it would through
the script; I can't tell.

Peter Kahrel

unread,
Dec 11, 2007, 12:14:57 PM12/11/07
to

I changed the table style so that it's based on No Table Style, and I
changed the cell style so that it's based on None, just in case that was
the problem. When I apply (or ALT-apply) the table style, it doesn't change
the cell style.


But it can work only if the cell style is specified at Body Rows in the table style panel. Though some fooling around shows that it works only well if you define a table and cell style with their embedded styles and then leave them alone. When you change embedded styles it all becomes much less straightforward, which lands you in the situation that you now appear to be in.

But anyway, the four steps you mentioned can be added to the script. Just replace the beginning with these lines:

#target indesign;

tbl = get_table();
tbl.cells.everyItem().texts.everyItem().applyParagraphStyle(
app.activeDocument.paragraphStyles.item ('example_par'),false);
tbl.appliedTableStyle = app.activeDocument.tableStyles.item ('example');
tbl.rows.everyItem().autoGrow = true;
tbl.rows.everyItem().height = 3;
snap_all_columns (tbl, 3);

The rest stays the same. (Your first step, assigning cell styles, can't be scripted as it is destructive. If that's no problem, then a line can be added to do that, too.)

Peter

Kenneth...@adobeforums.com

unread,
Dec 11, 2007, 1:25:00 PM12/11/07
to
Destructive in that it removes overrides? All my local formatting is in
character styles, so something that removed overrides would be highly
welcome.

Peter Kahrel

unread,
Dec 11, 2007, 2:46:56 PM12/11/07
to

something that removed overrides would be highly welcome


In that case, use this for the beginning of the script (again, the rest remains the same).

Peter

#target indesign;

tbl = get_table();
tbl.cells.everyItem().appliedCellStyle = 'example';
tbl.cells.everyItem().texts.everyItem().appliedParagraphStyle = 'example';

0 new messages