AutoFit Columns

AutoFit Columns User Guide

Introduction

The purpose of AutoFit Columns is to automate the process of determining the narrowest possible width of a table column that will accommodate all the data in the column, without the data becoming overset or wrapping to multiple lines. Autofit considers cell insets, paragraph indents, column strokes, and merged cells in its calculation.

Installation

AutoFit columns consists of two Javascript files: AutoFitColumns.jsx and AutoFitColumns.noUI.jsx. A good tutorial on how to copy these into the correct folder on your computer can be found at https://www.danrodney.com/scripts/directions-installingscripts.html

Running the script for the first time

After installing the script, display InDesign’s Scripts panel on the screen (Window > Utilities > Scripts), select one or more table columns, an entire table, a text frame, or nothing at all, and double-click on the AutoFitColumns.jsx script in the Scripts panel.

If you click About, and then select Create menu item, the next time that you quit and then start up InDesign, you’ll see an Id-Extras menu added to your menu bar. You can then choose AutoFit Columns from this menu to run the script if you wish.

Using the script

Clicking the AutoFit button in the dialog window will adjust the width of the columns to fit their contents. Which columns are adjusted depends on what is selected when you click the AutoFit button, as follows:

  • Nothing selected: If nothing is selected in InDesign (Edit > Deselect All), all tables in the active document will be autofit. The script will prompt you to confirm your choice before progressing further.
  • A single text frame selected: If you select a text frame, all tables in the story that that text frame belongs to will be autofit.
  • A complete table, or column, or range of cells selected: The columns containing the selected cells will be autofit.
  • Nothing is selected, but the text cursor is placed in a single table cell: The column containing that cell will be autofit when you run the script.

AutoFit Dialog Box

When you run the script, the dialog below appears. This dialog box is “sticky” and will remain on the screen until you click the Close button. If you have enough room on your screen, you can just leave it on the screen, much like a panel, and continue working on your layout. That way the Autofit button is just a single click away.

AutoFit dialog box

When you click the Autofit button, the width of the selected column(s) in your table are adjusted to the minimum value that will accommodate the longest line of text in each cell without wrapping to a second line.

financial table before and after running AutoFit

A financial table before (left) and after (right) running AutoFit Columns on all four columns.

 

Effects of the “Preserve Existing Line Breaks” Option

If you don’t select the Preserve Existing Line Breaks option, any wrapping text in columns will be “unwrapped” into a single row. If you select this option, the wrap will be maintained. If a line break or paragraph return has been typed into a cell, this break will always be honored, and never overridden.

A table showing the effects of the Preserve Existing Line Breaks option

The original table (left), all columns AutoFit without Preserve Existing Line Breaks selected (center) and with Preserve Existing Line Breaks selected (right)

 

Table Width Adjustments Feature

The other option in the AutoFit dialog box is the Table Width Adjustments feature. When this is selected, AutoFit will do a secondary adjustment to the table after the selected column(s) have been adjusted to the minimum width. It widens (or narrows) each column proportionally to make the table fit the current table width, the table’s frame, a portion of the frame, or a specific number of text columns. It can be helpful to understand the method by which this is done.

When the column widths must be increased to fit the table, frame, etc., the width of every column, including the selected columns that were just autofit will be increased proportionally to fit the target.

When the column widths must be decreased to fit the table, frame, etc., the width of every column except the selected columns that were just autofit will be decreased proportionally to fit the target.

Undo

Every operation of the script is completely undo-able. Because of the “non-modal” nature of the dialog box that is necessary to make the box “sticky” on the screen, you will usually need to click away from the dialog box before the undo keyboard shortcut will function properly.

The “NoUI” Version

In the download package, you will find a script with the code “noUI” (no user-interface, i.e, no dialog box) in the file name. This script is identical to the main script, but, when run, will bypass the dialog that is normally displayed. It will use the options that you have previously set when running the version with the dialog.

Important: The “no-UI” version has the same exact functionality as the UI version. Everything you can do with the UI version, you can do with the no-UI version. To set the options that the no-UI version uses, first run the regular UI version and set your preferences there. Now when you run the no-UI version it will use those options.

The advantage of having this available is that you can assign an InDesign shortcut key to this version of the script, and quickly run the script on a selected table or text frame by pressing the shortcut key. For documents with dozens of tables, this can be a more efficient way of working than having to click the Autofit button with the mouse repeatedly.

Known Limitations

The script doesn’t honor breaks in Rotated column heads, and the “width” of these rotated heads isn’t always calculated correctly by the script.

If you are using a decimal tab stop to align numerals on their decimal point within a column, the tab stop is ignored.

 

Article Comments

  1. Raphaël Freeman

    March 22, 2023 10:36 am Reply

    This is insanely fast but unfortunately does not work with Optical Margin Alignment switched on. Since I always use optical margin alignment, it would be useful to update the script accordingly!

    • Ariel

      March 23, 2023 2:47 am Reply

      Hi Raphael,
      Thanks for pointing this out. I’ve release version 1.0.4 which should now work with optical margin alignment switched on in table stories as well. (As well as some bugs with paragraph justification issues.)
      Ariel

Leave a Comment