Copy link to clipboard
Copied
Hi all
I've searched widely but have been unable to find an answer that works for me.
I have a task to repeat on hundreds of documents.
There is a table in each document that contains cells of two different heights - 4mm & 5mm.
I would like to reduce the depth of each cell (every cell in the table) by 1mm to end up with cell heights of 3mm & 4mm.
There are no header footer cells in table.
would someone be able to help me with a script to achieve this change?
MTIA Steve
1 Correct answer
Hi,
I can't believe I never spotted that problem before here is updated code for that problem.
...var myDoc = app.activeDocument;
var myRows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements();
for ( var i = 0; i < myRows.length; i++)
{
var myCells = myRows.cells;
for ( var j = 0; j < myCells.length; j++)
{
if ( j === 0)
{
myCells
.height = myCells .height -1 ; }
else
{
myCells
.height = myCells[
Copy link to clipboard
Copied
HI,
When you say you have cells of differing heights, is that per row, i.e. one row of cells is 5mm and another is 4 mm?
Regards
Malcolm
Copy link to clipboard
Copied
Hi Malcolm,
A good job you asked. It's actually two tables, one on top of the other - they contain data from Excel named ranges.
On some pages it's one table, some pages two tables (as below) and some pages x3 tables - all following same 'style'.
The depth of each row is either 5mm or 4mm, giving a look of a 1mm paragraph space below.
I'd like to reduce the depth of all rows by 1mm - all 4mm to 3mm, all 5mm to 4mm.
Steve
Copy link to clipboard
Copied
Hi,
This is the code I use to resize cells in a table, there may be better ways but I find this works pretty consistently.
var myCells = app.activeDocument.textFrames.everyItem().tables.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < myCells.length; i++)
{
myCells.height = myCells.height -1 ;
}
app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
Please note that once you get to a certain cell height, the cell wont get any smaller.
Hope this helps.
Malcolm
Copy link to clipboard
Copied
Hi Malcolm,
Thanks for the script, it works nicely - up to a point.
If you look at the image I posted yesterday you'll see that the table has
i) rows of a single cell and
ii) rows of two cells (split vertically).
All of the 5mm, single cell rows, reduce perfectly to 4mm.
The script is reducing some (but not all) of the 4mm (x2 cell) rows to 2mm?
I'm trying to decipher what is causing what seems like an odd inconsistency.
Is it possible to change the script to only run on a selected table?
Steve
Copy link to clipboard
Copied
Hi,
I can't believe I never spotted that problem before here is updated code for that problem.
var myDoc = app.activeDocument;
var myRows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements();
for ( var i = 0; i < myRows.length; i++)
{
var myCells = myRows.cells;
for ( var j = 0; j < myCells.length; j++)
{
if ( j === 0)
{
myCells
.height = myCells .height -1 ; }
else
{
myCells
.height = myCells[0].height; }
}
}
app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
It is possible to run it on a selected table, I will try and get a chance to create a sample for that today if possible.
Regards
Malcolm
Copy link to clipboard
Copied
Hi,
consider that some cells are perhaps set with autoGrow to true.
And also consider inset values like topInset and bottomInset as well.
Also see into minimumHeight and maximumHeight.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Malcolm
unfortunately it doesn't like that!
I do love the lengths people go to help out on these forums, but always feel guilty when things turn out more involved than first appear.
Thank you!
Copy link to clipboard
Copied
Hi Steve,
you saved the script in the wrong format!
You need plain text only, not as formatted text as the error suggests: {\rtf
See:
Indiscripts :: Indiscripts for Dummies
Regards,
Uwe
Copy link to clipboard
Copied
Thanks Uwe - now I've remedied that it works perfectly!
Copy link to clipboard
Copied
Hey you can try this,
var ad = 0;
var allDoc = app.documents;
while(ad < allDoc.length){
var stories = app.documents[ad].stories;
var st = 0;
while(st < stories.length){
var allTab = stories[st].tables;
var tb = 0;
while(tb < allTab.length){
var maxHeight = null;
var rw = 0;
var rows = allTab[tb].rows;
while(rw < rows.length){
maxHeight = rows[rw].height;
if(maxHeight == 4 || maxHeight == 5){
rows[rw].height = rows[rw].height-1;
}
rw++;
}
tb++;
}
st++;
}
ad++;
}
Regards,
Payal
Copy link to clipboard
Copied
Copy link to clipboard
Copied
use try catch in that place
try{
var ad = 0;
var allDoc = app.documents;
while(ad < allDoc.length){
var stories = app.documents[ad].stories;
var st = 0;
while(st < stories.length){
var allTab = stories[st].tables;
var tb = 0;
while(tb < allTab.length){
var maxHeight = null;
var rw = 0;
var rows = allTab[tb].rows;
while(rw < rows.length){
maxHeight = rows[rw].height;
if(maxHeight == 4 || maxHeight == 5){
rows[rw].height = rows[rw].height-1;
}
rw++;
}
tb++;
}
st++;
}
ad++;
}
}
catch(e){}
Copy link to clipboard
Copied
Thanks guys,
I now have two options to perform the changes I need!
Malcolm - I look forward to seeing the option to select & change individual tables, as I have other tables on the page containing images that I need to remain 'as is'.
Many thanks all
Steve
Copy link to clipboard
Copied
Hi,
To use the selected Page items, means you could select multiple items and if they have tables they should be changed. although if there is an easy rule to stop a table from being processed that might be easier to build into the script, for example: if table contains an image, do not change.
but here is the code, works for the simple 2 table sample document I used for testing.
var myDoc = app.activeDocument;
var mySelectedPageItems = myDoc.selectedPageItems;
// replaced to allow for the selected page item
//var myRows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements();
for ( var k = 0; k < mySelectedPageItems.length; k++)
{
var curPageItem = mySelectedPageItems
; var myRows = curPageItem.tables.everyItem().rows.everyItem().getElements();
for ( var i = 0; i < myRows.length; i++)
{
var myCells = myRows.cells;
for ( var j = 0; j < myCells.length; j++)
{
if ( j === 0)
{
myCells
.height = myCells .height -1 ; }
else
{
myCells
.height = myCells[0].height; }
}
}
}
app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
Hope this helps
Malcolm
Copy link to clipboard
Copied
Thanks very much Malcolm,
I'm looking forward to trying it out Monday morning!
Now Monday am - It works like a dream!
Thanks very much for your help
Steve

