Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Sizing Tables With a Script

Free script sets table properties that cannot be controlled by InDesign table styles, including table width and row height.

Any time you find yourself repeating the same steps while working in software like InDesign, there’s probably an easier way. Years ago, someone got tired of applying the same characteristics to text (font, font style, size, etc.), so styles were created. Paragraph and Character Styles allow you to set multiple attributes to text with the click of a button. Likewise, Object Styles allow you to set a plethora of attributes to objects in your InDesign projects. In my case, I was working on a project that contained all kinds of scientific tables that needed to be set a specific way. Table Styles to the rescue!

InDesign’s Table Styles let you set multiple options to any table, such as Row Strokes, Column Strokes, and Fills. They also let you choose which Cell Styles to use for Headers, Footers, and Body Rows.

TableStyles

Those Cell Styles let you choose which Paragraph Style you want applied to the text in the cell. You can also set other options, such as whether the cell has an inset, or uses diagonal lines.

CellStyles

With all my Table and Cell Styles set, I was off to make quick work of the tables.

Or so I thought!

The tables I was working on were already done in Microsoft Word—I just needed to flow them into my template and finish the styling. The problem I encountered—the problem anyone encounters when they’re flowing tables that come from outside of InDesign—was that the table was always a different width than the text frame (in my case, almost always wider). I needed the tables to be the same width as the text frame, but there’s no way to set the width of a table in the Table Style. Likewise, I needed a specific height for the table rows. But again, there’s no way to set the height of cells in a Table Style, and with my Table Style applied, my rows were all squished together. Faced with the task of manually resizing the height and width of some 20 tables, and seeing that the built-in tools (Styles) weren’t going to help me, I figured the best way to tackle this repetitive task was with a script.

WideTable

See also: Tackling Tables through Scripting and The Truth About Cell Styles

Tables Sized to Frame

The Tables Sized to Frame.jsx script is a JavaScript that sets three aspects of a table that can’t be controlled by Styles: Table Width, the Row Height setting (At Least or Exactly), and the actual Row Height. The script works by resizing the columns of the table in equal measure so it is the width of the frame, the same as if you were to resize the table while holding the Shift Key. It sets the Row Height setting to “Exactly” and the measurement to 1 pica.

TablePanel

See below for instructions on how to download the script.

To install the script, download it and move the file to your Scripts folder found in Applications > Adobe InDesign CC 2015 > Scripts > Scripts Panel. To run the script, simply select the frame of your table and double-click the script name in your Scripts Panel and the table will be resized to the width of the frame and it’s Row Height settings set to Exactly 1 pica.

You can edit the script as needed if you don’t want the Row Height settings adjusted. To do so, once the script is installed in your Scripts folder, right-click on the script and select the Edit Script option. This will launch the ExtendScript Toolkit for writing and editing Java Scripts (don’t worry—what we’re about to do is super easy). Look for lines 56 and 57 in the script:

tables[i].rows.everyItem().autoGrow = false;

tables[i].rows.everyItem().height = 12;

ExtendScriptToolkit

These two lines control the Row Height adjustments. To disable these two settings, we will “comment out” the lines, effectively disabling them, by adding two forward slashes to the front of the lines:

// tables[i].rows.everyItem().autoGrow = false;

// tables[i].rows.everyItem().height = 12;

You’ll notice as soon as you do that, the lines turn green, indicating they are comments to the script. Save the script and you’re good to go—the only adjustment made will be to the width of the table.

While you can set the width of a text frame with Object Styles, there is unfortunately no way to set the width or height of a table using a style. Until those features are added to InDesign, the Tables Sized to Frame script may speed up the process of setting unruly tables.

Note: Special thanks to Peter Kahrel for his generosity with writing this script! If you are logged in with either a free or paid InDesignSecrets membership, the download link appears below.

Not a member yet? Sign up here!
Jamie McKee

Jamie McKee

Jamie McKee has been designing books/journals and setting type for over 20 years, getting his start at Indiana University Press. His clients still include Indiana, as well as Duke University Press, Johns Hopkins University Press, and University of Wisconsin Press. You can find out more about him at mackeycomposition.com.
Jamie McKee

Latest posts by Jamie McKee (see all)

  • - November 30, -0001
Related Articles
Comments

25 Comments on “Sizing Tables With a Script

  1. This is a great time saver. I often have projects that have several different table treatment and this would be very helpful. However, Can I suggest a pop-up panel for the script options rather than editing the code. Bad things happen, like freezes and crashes, when I spend too much time in ExtendScript.

    I suppose I could save multiple versions of the script for each table treatment in large or long document projects. But for small projects a modal window would be much more efficient.

    Keep up the awesome work!!!

    • Samantha-

      Thanks for you comments on the script. As the script only does a few things, I don’t feel a pop-up panel is necessary. I recognize that even setting the measurements may be more specific to my needs, which is why I included the part about editing the script. I also wanted to educate users and show that working with scripts (using, looking at the code, editing, etc.) does not have to be a scary thing. Just about anyone can add forward slashes to the lines to comment out, and thus disable, those lines. So yes, your suggestion to save multiple versions of the script is a good one if you only need to set the table width to the width of the frame.

  2. Jamie,

    The limitation of table and cell styles that you address with your script is why I continue to use (and pay for) Woodwing’s Smart Styles for styling complex tables (imported, in my case, most often from Excel). I don’t think your script is sophisticated enough to replace Smart Styes but, if you would like sample tables for experiments, you can contact me at crych [at] telus [dot] net.

    Lindsey

  3. hi guys
    few years back i had a huge project that contained 700 tables designed for A4 size; than the client wanted an inch version of the catalog for the US market and i needed to change the table width and hight. someone at the forums (maybe even peter) wrote a few lines of javascript magic that did the trick and changed my 700 tables in less than a minute

    row width:
    for (s=0; s<app.activeDocument.stories.length; s++)
    {
    for (t=0; t<app.activeDocument.stories[s].tables.length; t++)
    {
    for (r=0; r<app.activeDocument.stories[s].tables[t].column.length; r++)
    {
    if (app.activeDocument.stories[s].tables[t].column.width == 10)
    app.activeDocument.stories[s].tables[t].column.width = 5;
    }
    }
    }

    change the 10 to current width and the 5 to wanted width

    row height:

    for (s=0; s<app.activeDocument.stories.length; s++)
    {
    for (t=0; t<app.activeDocument.stories[s].tables.length; t++)
    {
    for (r=0; r<app.activeDocument.stories[s].tables[t].rows.length; r++)
    {
    if (app.activeDocument.stories[s].tables[t].rows[r].height == 5)
    app.activeDocument.stories[s].tables[t].rows[r].height = 4;
    }
    }
    }

    change the 5 to current height and 4 to wanted width

    same here
    works like a charm
    it worked on cs4-6 and i think even cc

    if any of you wants to update this you are more than welcome

    Gil Halevi Israel

    • Hello, can this be plugged in to the script available for download here? Or how would I use it? The one here isn’t working for my needs, and I’m not sure what I need to change. I need both the row height and column width changed, but for some reason this download doesn’t work for me. Thank you!

  4. Hi and thanks for this. For the record, it seems to work in Windows.

    I’m wondering what I can edit in the script to control the table width? I’m not a complete noob to programming or scripts, but I am a noob to Indesign scripts.

    Glenn

    • Glenn, you can do anything with this
      i know s*** about scripting but i do understand that the ==height is the original table height and the second = is the destination height. that goes to the width as well

      have fun!
      i saved 3 month of manual labor in changing height and width and also the auto change of mm to inch and vice versa

    • You should be able to see the download link whether you are a free or premium member… I have tested it and it appears to be working. The link is just above Jamie’s bio, at the end of the article. Do you not see it? (Maybe clear your browser cache or try a different browser? Or log out and log back in?)

  5. Thank you for the script! I wish I could say it’s working for me as it has others, but it makes my table much smaller and turns my objects and text within the table into little red dots. I’m not sure what I need to do? Would a screenshot be of any help? I really appreciate any advice! Thank you!

    • No need for a screen shot: the red dots indicate that the rows are two shallow or the columns too narrow or both to display the content you have in your table. (Command-Y for the Story Editor to see that the content is still there.) Try increasing the height of the rows or the width of the columns to see which constraint is hiding the content.

      If the height of the columns is the problem, try commenting out lines 56 and 57 as described in the article to see if that produces a table that displays the content.

      If the columns are too narrow, and since the script sets the overall width of the table to the text column, you may have too much content in too many columns to be displayed in a table that fits the text column. Reducing the size of the type or the insets of the cells may help.

      • Thank you so much for your help. It is indeed the height that is the issue. I can change it to not resize the height, but I need my table to fit the page. Is there any way I can adjust *how much* it adjusts the height, or is manually changing the content to fit the new height the only way? Thanks so much!

      • Kaila, you should probably move your questions to the forums but I’ll make two suggestions, though I’m not much of a scripter. Remove the commenting out; set the value of line 56 to ‘true’. If your content is variable, this should allow the rows to expand until they fit the content. It will not size the table to fit the height of the column exactly, unless you are very lucky.

        If you have a number of tables with similar content and the amount of content in each row is much the same, work out the height for the rows that will fill your text column vertically and enter that amount in points as the value for line 57. I think the script should then resize your other tables to fit the page, given content of similar size.

  6. Ow, should have tried i got it. With this code you can also set the maximum height for cell heights.

    tables[i].rows.everyItem().maximumHeight = 74;

    • Tim-
      You need to be logged in to see the download link for the script. You can sign up for a free InDesignSecrets login. (I believe this was made necessary because of the amount of spam we were getting.)

Leave a Reply

Your email address will not be published. Required fields are marked *