Tables

InDesign’s tables are pretty powerful and there’s plenty of use for them. However, there are some gaps (such as the inability to snap columns to their contents), but fortunately, tables are also relatively easy to script. In this section we’ll go into some details of table scripting.

Anatomy of a Table

A table consists of columns and rows. Tables have many properties, some of which we list here, as we’ll use them a lot. With a reference myTable to a table, here are a few examples of some of the table’s many available properties (consult the OMV for full details):

myTable.columns
myTable.rows
myTable.cells                // all cells in the table
myTable.columns[n].cells     // all cells in column n
myTable.rows[n].cells        // all cells in row n
myTable.columns[n].cells[p]  // cell p in row n
myTable.rows[n].contents     // contents of a row returns an array
myTable.columns[n].cells[p].contents

As you can see, you can address all of the cells in a table, all of the cells in a column, or all of the cells in a row. Apart from that, you can also address individual cells. In a table, each cell has a name in the form of what is by now a standard way of referencing cells—namely, the column and row number separated by a colon. As is so often true in InDesign, you can address a certain object in different ways. Thus, the following lines refer to the same cell:

myTable.cells.item ("0:3")
myTable.rows[0].cells[3]
myTable.columns[3].cells[0]

Each refers to the fourth cell in the first column (or the first cell in the ...

Get Scripting InDesign CS3/4 with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.