CREATING TABLES

In our last post we focused on some of the properties of table cells. The scripts relied on a table already existing in the active document. In this post we will concentrate on creating the table and look at concepts having to do with rows and columns. The width of a table defaults to the width of its parent (a text frame). Row and column counts default to 4 rows and 4 columns. With a text frame selected a default table can be created as follows:

--assuming a text frame is selected
tell application "Adobe InDesign CC 2018"
   set frameRef to item 1 of selection
   tell frameRef
	set tableRef to make table
   end tell
end tell

To set the number of columns and rows you need to be aware that the number of rows in a table is set using the table’s body row count property. Additional header rows and footer rows are added as needed using header row count and footer row count. If no header rows and/or footer rows are defined their count is 0 by default. One might think that you could define the height of the table as part of its property list. (There is a height property for a table after all.) However, setting the height property will produce unexpected results. It is better to define the height of the table’s rows and let InDesign calculate the height for the table.

Create Table

set bodyRows to 5
set headRows to 1
set colCount to 4
--assumes text frame selected
tell application "Adobe InDesign CC 2018"
   set measurement unit of script preferences to points
   set frameRef to item 1 of selection
   tell frameRef
      set tableRef to make table 
   end tell
   tell tableRef
      set column count to colCount
      set body row count to bodyRows
      set header row count to headRows
      set height of rows to 36
   end tell
   get height of tableRef
end tell

Interestingly, even though header and footer rows are defined separate from the body rows, they are referenced using the table’s row indexing and are included as part of the table’s rows reference. Add the following tell statement after the end tell statement for tableRef in the above script:

tell row 1 of tableRef
   merge cell 1 with cell -1
end tell

This brings up an interesting property: column span. As the last statement in the script above add:

get column span of cell 1 of tableRef

If the total for the table’s row heights is greater than the height for the parent text frame, the row heights are still applied using the heights specified, but the text frame becomes overset. Try the following with a 3 inch tall text frame selected.

set bodyRows to 6
set headRows to 1
set colCount to 4
tell application "Adobe InDesign CC 2018"
   set measurement unit of script preferences to points
   set frameRef to item 1 of selection 
   set name of frameRef to "testFrame"
   tell frameRef
      set tableRef to make table
   end tell
   tell tableRef
      set body row count to bodyRows
      set column count to colCount
      set header row count to headRows
      set height of row 1 to 24
      set height of rows 2 thru -1 to 36
   end tell
   tell row 1 of tableRef
      merge cell 1 with cell -1
   end tell
end tell

When you run the script above, the text frame becomes overset, but when expanded, the last row is the correct height. You might want to save your document at this point to do some experimentation with the table created. We will use this table for our next demonstration.

Setting Fill Color/Tint

A script can set the fill color and fill tint for rows individually, by row, and by column. The following demonstrates. With the table created above (table 1 of the text frame named “testFrame), try the following:

--assumes document 1 has at least 5 color swatches
set cellList to {4, 9, 12, 17, 20, 25}
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set testColor to swatch 5 of docRef
   set tableRef to table 1 of text frame "testFrame" of docRef
   tell tableRef
	set fill color of column 1 to testColor
	set fill tint of column 1 to 10
	set fill color of row 1 to testColor
	set fill tint of row 1 to 50
	repeat with i from 1 to length of cellList
	   set cellIndex to item i of cellList
	   set properties of cell cellIndex to {fill color:testColor, fill tint:10}
	end repeat
   end tell
end tell

ON YOUR OWN

Revert your test document (saved above) and use the table to test other properties for a cell, column, and row. Try this with some of the following properties:

bottom edge stroke weight
gradient fill angle (using a gradient as the fill color)
inner column stroke weight
left edge stroke weight
right edge stroke weight
top edge stroke weight

ONWARD AND UPWARD

As you can see, setting up a table can get a little involved, and we have barely touched the idea of strokes and fills. So, if your script is going to define properties for creating a table, you might as well have the script set it up as a table style. We will get into creating styles for tables and cells in our next post.

Disclaimer:
Scripts are for demonstration purposes only. No representation is made as to their accuracy or completeness,