Automating Adobe InDesign 2018


Table Styles

Tuesday, November 20, 2018

Table Styles

We presented a small sampling of what can be done with cell styles in our previous blog post. Cell styles are the foundation of working with table styles. With just a few cell styles established in a document, you can get a head start automating table production using table styles. For the sake of completeness, the following script demonstrates creating a table style using three cell styles ‘HeadCell”, “BodyCell”, and “GrayCell”.

TestTable_TableStyle

(*Expects active document with the three styles “HeadCell”, “BodyCell”, and “GrayCell”*)
set styleName to "TestTable"
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set headCell to cell style "HeadCell" of docRef
   set bodyCell to cell style "BodyCell" of docRef
   set leftCell to cell style "GrayCell" of docRef
(*styles for table regions in the following order: 
body, head, left column, right column, footer*)
   set styleList to {bodyCell, headCell, leftCell, missing value, missing value}
   set tableStyleRef to my getTableStyle(docRef, styleName, styleList)
end tell

on getTableStyle(docRef, styleName, styleList)
   tell application "Adobe InDesign CC 2019"
      tell docRef
	 if table style styleName exists then
	    set tableStyleRef to table style styleName
	 else
	    set tableStyleRef to make table style with properties {name:styleName}
         end if
      end tell
      tell tableStyleRef
	 set body region cell style to item 1 of styleList
	 if item 2 of styleList is not missing value then
	    set header region cell style to item 2 of styleList
	 end if
	 if item 3 of styleList is not missing value then
	    set left column region cell style to item 3 of styleList
	 end if
	 if item 4 of styleList is not missing value then
	    set right column region cell style to item 4 of styleList
	 end if
	 if item 5 of styleList is not missing value then
	    set footer region cell style to item 5 of styleList
	 end if
      end tell
      return tableStyleRef
   end tell
end getTableStyle

Make sure you have the three cell styles listed above in an active InDesign document. Run the script from the Script Editor. After running the script you should see the table style “TestTable” in the Table Styles panel.

And yes, the names of the cell styles are hard-coded in the script. To make the script user friendly you will need to add a dialog that provides drop boxes from which the user selects the cell style to use for each table region. You can imagine the code that would need to be added to the script for just this purpose. With all of the other settings that can be established for a table style, you can see why you might want to start thinking about creating more complex table styles using InDesign’s user interface. On the other hand a script that needs only to define a set number of values for a table style could prove to be very handy.

Once you get table styles defined, save them to a style file or a document template. (We will cover this subject in a future blog post.) Then when you have tab-delimited text in a table, just apply the table style.

We demonstrate using a very simple, but maybe eye-opening, bit of imported text.

...before running script

Create Styled Table

set errStr to "Requires text frame selected with tab-delimited text"
try
   tell application "Adobe InDesign CC 2018"
      set docRef to document 1
      if selection is not {} and class of item 1 of selection is text frame then
	 set frameRef to item 1 of selection
      else
	 error errStr
      end if
      set tableStyles to name of every table style of docRef
      set tableStyleChoice to choose from list tableStyles
      if tableStyleChoice is not false then
	 set tableStyleRef to item 1 of tableStyleChoice
      else
	 error "User cancelled"
      end if
      tell text 1 of frameRef
	 set tableRef to convert to table column separator tab row separator return
      end tell
      tell tableRef
	 tell row 1 to merge cell 1 with cell -1
	 set row type of row 1 to header row
	 set applied table style to table style "TestTable" of docRef
      end tell
      tell text of cells of tableRef to clear overrides 
   end tell	
on error
   activate
   display alert "Error " & errStr
end try
table created with script...after running script

Notice that our Create Styled Table script is more complete than the example scripts provided in previous blogs of this series. Those scripts just did not run if there was a problem. Here we add some error handling to alert the user to problems. As you can see, this adds a fair amount of code to the scripts. For the sake of brevity, we generally overlook error handling with the expectation that the reader knows how to bullet-proof a script.

Set Row Type

One little problem that occurs when converting delimited text to a table is that all rows are created as body rows by default. The applied table style needs to know if there is a header row or footer row. If the script sets the header row for the table using header row count:

tell tableRef to set header row count to 1

a new empty row is added.

Instead, change the top row, or any number of rows at the top of the table, using row type. Our Create Styled Table script (above) has an example:

set row type of row 1 to header row

For a footer row, you could use:

set row type of row -1 to footer row

Clear Overrides

Another problem in converting text to tables is that styling for text applied as part of a table style could become overridden. To prevent this the script above adds the following line of code:

tell text of cells of tableRef to clear overrides

Most likely this statement would not be needed, but is added as a good precaution.

Microsoft Excel

InDesign’s support for formatted Excel files is pretty awesome but, depending on the styling applied, can prove to be somewhat limited. If you have an Excel file that someone has spent a lot of time styling it may be worth your time to see how well the file does import. For this, set table formatting for excel import preferences to formatted table. Notice that in the following example script, the original value for the preference is saved so it can be restored after the script has completed. Keep in mind that Excel import preferences are persistent (they stay with the application until either reset or application preferences are trashed). Always be a good neighbor: when setting application preferences have the script restore their values to the original state.

tell application "Adobe InDesign CC 2018"
   if selection is not {} and class of item 1 of selection is text frame then
      tell excel import preferences
	 set origPrefs to table formatting
	 set table formatting to excel formatted table
      end tell
      set fileRef to choose file with prompt "Choose Excel file"
      set frameRef to item 1 of selection
      tell frameRef to place fileRef
      tell excel import preferences to set table formatting to origPrefs
   end if
end tell

Your script can even dictate the range of cells to be imported, In the above script, substitute the following:

tell excel import preferences
   set origPrefs to table formatting
   set table formatting to excel formatted table
   set decimal places to 2
   set range name to "A1:E5"
   set use typographers quotes to true
end tell

Other values for Excel table formatting (excel import preferences) are excel unformatted table, excel unformatted tabbed text, excel format only once).

Upward and Onward

You are probably bored silly with all all we have had to say about creating tables from text and styling. We promise to let things get a little more colorful when we start importing graphics to the tables. Stay tuned.

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.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18391&PostID=1527920&A=Trackback
Trackbacks
Post has no trackbacks.