Skip to main content
NicolaCardi
Participating Frequently
February 6, 2020
Answered

Script to fit text to cell

  • February 6, 2020
  • 3 replies
  • 3368 views

I am rather new to InDesign scripting.

I have made a script that gets a table and, cell by cell, goes into the text, sees if it overflows and if so decreases the HorizontalScale by 1%, as many times as necessary to fit the text so removing any overset.

It works perfectly ONLY IF there is an alert that stops temporarily the execution.

If I let it go without it, it is not working.

This is my script 

 

 

 

function processTable(table) {
for (i=0; i<table.cells.length; i++)
{
horscale = 100;
while ((table.cells[i].overflows) && (horscale >=50)) {
//alert ("newTextSize: "+horscale); //if I leave this alert it works perfectly, if I take it out it doesn't
table.cells[i].texts[0].horizontalScale = parseFloat(horscale);
horscale = horscale - 1;
}
}
}
 
If I let it go without the alert (like above it is commented) the horscale gets always to 50, showing that it cannot check if text is overflowing in all steps.
Anyone can help? I would just need to "pause" an instant, I guess, or redraw or refresh or anything that lets InDesign re-check if the text is still overflowing during that iteration.
Thanks in advance
This topic has been closed for replies.
Correct answer Manan Joshi

In the same scrit I need to know what's the page number where the table is...

I read the parent of a table is a story...but how do you get the page it is placed on? Sorry for this rather silly question but as I said I am rather new to scripting in Id.

Thanks

Nicola


Table's parent would be a textframe. So for a non nested table the following should give you the page on which the table lies

app.selection[0].parent.parentPage

 

If the table is nested one level then the parent of the table would be a cell, parent of the cell would be the outer table and then parent of it will be the textframe. So in this case the following should work

 

app.selection[0].parent.parent.parent.parentPage

 

-Manan 

3 replies

Known Participant
January 17, 2022

Apologies for (a) jumping in as an amateur and (b) dredging up the past! I seem to be at a similar stumbling point as the original post, and I'm attempting to follow similar steps to success. (Related, a great big thanks to Jongware's summary of tackling tables! It's helped me immensely just getting to this point!)

I think I've got the same code as what was suggested here, but my script seems to disregard the 'overflows' condition of the WHILE loop; all cells get their horizontal scale incrementally decreased regardless of overset text. I'm not sure what I might be doing wrong...! Ideally, this script will only reduce instances of overset text as minimally as required... and later, I'm hoping to add a condition where if the cell contains too much text, it allows multiple lines in that cell only.

Any help/advice would be greatly appreciated!

var table = app.documents[0].pages[0].textFrames[0].tables.everyItem();

var horscale = 100;

for (i=0; i<table.cells.length; i++) {
horscale = 100;
while ((table.cells[i].overflows) && (horscale >=50)) {
table.cells[i].texts[0].horizontalScale = parseFloat(horscale);
horscale = horscale - 5;
app.documents[0].recompose();
}
}

 

Community Expert
January 17, 2022

Instead of

 

app.documents[0].recompose();

 

use 

 

table.cells[i].recompose();

 

P.

Known Participant
January 17, 2022

Thanks for the nudge, Peter... replacing that line still resulted in all cells being affected. It's like the code is completely skipping this condition:

(table.cells[i].overflows)
brian_p_dts
Community Expert
February 6, 2020

Hmm, not sure about that behavior. I might try taking the second while argument and moving it inside the loop to force a break: 

 

if (horscale < 50) { break; }

 

Also, try resetting the horscale variable at the start of the for loop by throwing a "var" in front of it. It might be reading horscale from the previous iteration in the for loop.

 

Edit: Or just use recompose() as Jongware suggested. 🙂

Jongware
Community Expert
February 6, 2020

Add a 'recompose' after your scale operation to give InDesign a chance to update -- http://jongware.mit.edu/idcs6js/pc_Text.html#recompose

Community Expert
February 6, 2020

Yep, the .recompose() is required.

Manan JoshiCorrect answer
Community Expert
February 7, 2020

In the same scrit I need to know what's the page number where the table is...

I read the parent of a table is a story...but how do you get the page it is placed on? Sorry for this rather silly question but as I said I am rather new to scripting in Id.

Thanks

Nicola


Table's parent would be a textframe. So for a non nested table the following should give you the page on which the table lies

app.selection[0].parent.parentPage

 

If the table is nested one level then the parent of the table would be a cell, parent of the cell would be the outer table and then parent of it will be the textframe. So in this case the following should work

 

app.selection[0].parent.parent.parent.parentPage

 

-Manan