Skip to main content
Known Participant
January 12, 2012
Answered

Set table row height

  • January 12, 2012
  • 1 reply
  • 10726 views

I have multiple InDesign files with linked .xls tables. About 150 tables per file, and these files update quite frequently (adding and removing rows etc).

And I was looking for a plugin that could do that, and was told that this should be scriptable.

And since I am not that deep into the scripting scene,does anyone know of script that can do the following?

What I would want to happen is a hight of a row of empty cells to be 7pt.

The hight of a cell with content to be a minimum of 14pt, unless this cell contains multiple lines it should increase by 7pt till it can fit all content (and decrease by 7pt if a line is removed in an update of the .xls file). The reason why we would need 7pt increments is that multiple lines often won't fit in a 14pt cell hight. And most designs call for a grid to work in.

A cell with a certain Cell-style to have a hight of 21pt.

Hope the attached example helps to clarify the question.

This topic has been closed for replies.
Correct answer John Hawkinson

Hi John,

Thanks again for your great post, and I stand corrected in one of my questions. I did not make myself clear.. but yes, a pony would be nice

Mark:

Also we have cells that should have a set height (subtotal and top-value)

In the above I am talking about cell-styles (subtotal, top-value)

I will try your script the first thing tomorrow when I am back in the office on the test document.

Thanks again for your great help!


Also we have cells that should have a set height (subtotal and top-value)

In the above I am talking about cell-styles (subtotal, top-value)

Whoops. I suppose I should have checked, sorry!

Add ", styleName" to the var declaration on line 5, and then add this at line 13, before the "if (initial) {":


        styleName = row.cells[0].appliedCellStyle.name;

        if (styleName === "top-value" ||

             styleName === "subtotal"

        ) {

            continue;

        }

1 reply

John Hawkinson
Inspiring
January 16, 2012

Hi, the desmodus:

  I apologize for sending you here from the regular forum (Advanced cell-style plugin?) on Wednesday and then not having the time to respond.

It would have been easier, though, if you had provided an actual document sample to work with, because there are implicit assumptions about font size and whatnot that aren't completely clear from your screenshot.

I'm also unclear why you need to incrementally increase the size of the non-empty rows. Is it not sufficient to set the rows to "at least" 7pt and ensure their leading is 7pt? (Err, I guess you the leading less the vertical cell insets, but still). It's annoying to have the script interact with the composition engine, because it gets slower and you have to force InDesign to do recalculations, and that can be tricky. I would rather avoid it.

Here's a script that basically implements this. Starts with a fitTable() function that, given a table, iterates over the rows and checks for non-zero content. If zero, it sets the height to p7 and turns off autoGrow (unnecessary, I guess, and maybe counterprodutive). If non-zero, it sets the height to p14 and turns on autogrow.

Then, there's the main() function which applies the fitTable() function to every table in every story in the document.

function fitTable(table) {

    var i, row, contentLength;

    for (i=0; i<table.rows.length; i++) {

        row = table.rows;

        contentLength = row.contents.join("").length;

        // $.writeln("Row "+i+" has height "+  row.height+" "+contentLength+" <"+table.rows.contents+">");

        if (contentLength === 0) {

            row.height = "p7";

            row.autoGrow = false;

        } else {

            row.height = "p14";

            row.autoGrow = true;

        }

    }

}

function main() {

    var d=app.activeDocument,

        tables=d.stories.everyItem().tables.everyItem().getElements();

    for (i=0; i<tables.length; i++) {

        fitTable(tables); 

    }

}

main();

I also exported my test table as InDesign Tagged Text, so you can import that and see what I used it on. If you have troubles that relate to your particular document, a exporting as IDTT or a Snippet and pasting it in here is a great way to communicate about it. Use the Advanced Editor and >> Syntax Highlighting: XML so that the forum doesn't corrupt it.

<ASCII-MAC>

<vsn:7><fset:InDesign-Roman><ctable:=<Black:COLOR:CMYK:Process:0,0,0,1><Paper:COLOR:CMYK:Process:0,0,0,0>>

<dps:NormalParagraphStyle=<Nextstyle:NormalParagraphStyle>>

<dtbls:\[Basic Table\]=>

<pstyle:NormalParagraphStyle>before table

<pstyle:NormalParagraphStyle>

<pstyle:NormalParagraphStyle><tstyle:\[Basic Table\]><tStart:15,2:0:0<tcdct:Text>><coStart:<tcaw:187>><coStart:<tcaw:187>><rStart:<trah:7><tramirs:7><trag:0>><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Non-current assets<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>12<cs:><cl:><cf:><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Inventories<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>14<cs:><cl:><cf:><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Financial assets<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:7><tramirs:7><trag:0>><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Cash<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Assets held for sale<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Current assets<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Total assets<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:19.5><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Equity to the

<cs:><cl:><cf:><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>owners of the company<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Interest<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Total equity<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Interst-bearing borrowings<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Provisions / employee benefits<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><rStart:<trah:14><tramirs:14><trag:1>><estyle:\[None\]><stypri:0><clStart:1,1><pstyle:NormalParagraphStyle><cs:6.000000><cl:7.000000><cf:Helvetica>Other non-current liabilities<cs:><cl:><cf:><clEnd:><estyle:\[None\]><stypri:0><clStart:1,1><clEnd:><rEnd:><tEnd:>

Known Participant
January 16, 2012

Hi John,

No need for apologies at all, this is an open forum and you are going out of your way as is.

This script does the trick, except the growing by 7pt increments. But from what I understand from your post, this messes with the 'composition engine' of InDesign.

Let me try to ellaborate why we want this increment.

In a cell we have a paragraph-style that has lets say an 9pt font with a 10,5 leading. So if there are 2 lines it would be 2x9pt + a 10,5 leading. In theory this would result in a cell height of 28,5pt (depending on the top and bottom padding). In our grid this cell should be a multiple of 7pt, so 35pt.

At the moment we have a list of cell heights (for the non math wizards), from where the operators can choose the height of the cell that would fit the content.

Also we have cells that should have a set height (subtotal and top-value)

I placed a sample table at: http://www.hofhuis.org/transfer/table_sample.zip . Please don't mind the colors in the design, these are for demo purposes here only.

Thank you soo much for your time.

Mark

Known Participant
January 17, 2012

This makes the script work on every table I fire it on. The only thing that it now fails on is the empty rows, instead of setting them to 7pt, it sets them to 14pt.

Did I paste the addition to the code on the correct place?

You're welcome. Yes, you did paste it in the right place.

Set debug=2 and see what is printed out for the empty rows, I guess (or provide a sample document).

Perhaps they are not really "empty" — does Type > Show Hidden Characters show them to really be empty?


It prints out that it makes them 14pt.

And they are really empty, I even generated a new row

I placed a sample at: http://www.hofhuis.org/transfer/table_sample.zip

Hope this helps