TABLE AND CELL STYLES

In our previous blog post our scripts created a table and added some styling to the cells. This is all well and good, but not as efficient as using styles. When it comes to cell and table styles, these are not, because of the amount of information they can define, created using a script. But should you have the need, and, for the purpose of topic completeness, we will introduce the topics here.

CELL STYLES

To create a table style, one needs cell styles—one to define each table region for the table (more on that later). In a real world situation one would need to add a custom dialog or rely on a plain text file similar to that used in Adobe’s FindChangeByList sample script to define the values needed by the script. Our sample script below will have the person creating or modifying the script place the values at the top of the script using the AppleScript editor. The script assumes there is an active document having a color swatch named “Blue.”

Create Cell Style

--value record returned from custom dialog or other
set returnValues to {styleName:"BlueCell",swatchName:"Blue", colorTint:20}
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set sColor to swatchName of returnValues
   set myColor to swatch sColor of docRef
   set myTint to colorTint of returnValues
   set styleName to styleName of returnValues
   set propList to {fill color:myColor, fill tint:myTint}
   set cellStyle to my getCellStyle(docRef, stylename, propList)
end tell
(*Arguments: reference to document, name of style, and property record*)
on getCellStyle(docRef, styleName, propList)
   tell application "Adobe InDesign CC 2018"
      tell docRef
         if not (exists cell style styleName) then
            set thisStyle to make cell style with properties {name:styleName}
         else
            set thisStyle to cell style styleName
         end if
         set properties of thisStyle to propList
      end tell
   end tell
   return thisStyle
end getCellStyle

TABLE STYLES

If you ran the script above with a document open, you will have two cell styles listed in the Cell Styles panel: “[None]”(by default) and “BlueStyle.”

For practice—and to prepare the document for our next demonstration script—use the script above to create more two cell styles for the document, one named “BodyCell” and one named “GrayCell”. Use your own values for the returnValues property record.

Hint: the first line of the script above is all you need to change. For the BodyCell it could read similar to:

set returnValues to {styleName:"BodyCell",swatchName:"Paper", colorTint:0}

After creating your styles, save the document with the cell styles for later use.

Now that you have cell styles in place for your document, the following script can create a table style. (Double check to make sure the active document has the required table cell styles in place.)

Create Table Style

A table style defines cell styles for up to five regions of the table, The table style properties that define the cell styles for these regions are: body region cell style, header region cell style, left column region cell style, right column region cell style, and footer region cell style. If a cell style is not designated for a region, its value is nothing and assumes the same style as that for the body style region. The following script demonstrates.

--assumes BodyCell, BlueCell and GrayCell exist for the document.
set tableStyleName to "TestTableStyle"
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set headStyle to cell style "BlueCell" of docRef
   set columnStyle to cell style "GrayCell" of docRef
   set bodyStyle to cell style "BodyCell" of docRef
   set styleList to {bodyStyle, headStyle, columnStyle, missing value, missing value}
   set tableStyleRef to my makeTableStyle(docRef, tableStyleName, styleList)
end tell
(*Cell styles are passed to the handler in the following order:
body region, header region, left column region, right column region, footer region
Requires open document*)
on makeTableStyle(docRef, styleName, cellStyles)
   tell application "Adobe InDesign CC 2018"
	tell docRef
	   if not (exists table style styleName) then
		set tableStyleRef to make table style with properties {name:styleName}
	   else
		set tableStyleRef to table style styleName
	   end if
	end tell
	tell tableStyleRef
	   if item 2 of cellStyles is not missing value then
		set header region cell style to item 2 of cellStyles
	   end if
	   if item 3 of cellStyles is not missing value then
		set left column region cell style to item 3 of cellStyles
	   end if
	   if item 4 of cellStyles is not missing value then
		set right column region cell style to item 4 of cellStyles
	   end if
	   if item 5 of cellStyles is not missing value then
		set footer region cell style to item 5 of cellStyles
	   end if
	end tell
   end tell
   return tableStyleRef
end makeTableStyle

After running the script you should see a listing for TestTableStyle in the Table Styles panel for InDesign (Window > Styles > Table Styles). To test your table style, create a text frame in the same document and run the following script with the text frame selected:

Table From Table Style

tell application "Adobe InDesign CC 2018"
   set myStyle to table style "TestTableStyle" of document 1
   set selRef to item 1 of selection
   tell selRef
	set tableRef to make table
   end tell
   tell tableRef
	set body row count to 4
	set column count to 4
	set header row count to 1
	set applied table style to myStyle
   end tell
end tell

Save your document somewhere convenient. We will be using it (or one like it) for our next blog post.

ONWARD AND UPWARD

As you can imagine, creating a custom dialog or text file to provide the values for anything but a simple cell or table style could be very complex. For that matter most styles will likely be created using InDesign’s user interface. But once you have a table style defined, you will want to save it for later use. Our next blog post will tackle that subject, so watch for it. (Make sure you have the document with the styles above saved somewhere convenient.)

DID YOU KNOW?

InDesign 2019 now supports footnotes in tables. The footnote number continues with the story’s numbering. The footnote text appears at the bottom of the text frame.

Disclaimer:
Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.