Your prescription for increased productivity and profitability
In our previous post we styled the text in the table cells using a paragraph style with emphasis on using the document’s default paragraph style for the majority of the text. We could have created a cell style instead and styled the text based on the cell style. Like an object style, a cell style carries with it a paragraph style designation. The text styling will be applied to all text within cells assigned the style unless overridden. Unlike a text object style, a cell style does not have the ability to use the next style property for the paragraph style.
A cell style can also establish the insets for text and graphics as well as clipping. What is interesting, is that a table style can use cell styles to set regions of the table: body region, header region, and footer region. You just need to set the row type to a header or footer row type. Body row type is a default. Let’s see how this all works with a sample table.
Requires InDesign document open that has the following table cell styles: “HeaderCellStyle”, “BodyCellStyle”, “FooterCellStyle”. It will create a table style named “Test Table Style” so make sure that a table style of that name does not exist. Spaces between words in rows one and two for the the variable testText are tabs.
set testText to "This is Header Text" & return & "This is row one" & return & "This is row two" & return & "This is footer text" tell application "Adobe InDesign CC 2015" set docRef to document 1 tell page 1 of docRef set frameRef to make text frame with properties {geometric bounds:{100, 36, 200, 336}} set contents of frameRef to testText tell text 1 of frameRef to convert to table column separator tab row separator return end tell tell row 1 of table 1 of frameRef to merge tell row -1 of table 1 of frameRef to merge tell docRef if not (exists table style "Test Table Style") then set tableStyle to make table style with properties {name:"Test Table Style", header region cell style:"HeaderCellStyle", body region cell style:"BodyCellStyle", footer region cell style:"FooterCellStyle"} end if end tell tell table 1 of frameRef set row type of row 1 to header row set row type of row -1 to footer row set applied table style to "Test Table Style" end tell tell cells of table 1 of frameRef to clear cell style overrides end tell
Of course cell styles can also be used to style the appearance of the cell: fill color, fill tint, stroke type, stroke weight, stroke color, and diagonal lines.
Add to the property settings that can be set for a table style: space before, space after, all the stroke settings for top border, left border, bottom border, and right border, not to mention all the alternating stroke and fill settings, and you have a mountain of properties that need setting to create a table style (or to style a table manually). Once you have created a table style, save it in a document so it can be imported the next time you need a table styled similarly. With this file on hand you import the table style. If you import a table style that relies on cell styles, the cell styles and the paragraph styles associated with the cell styles all import automatically. Great!
For the next demonstration script, we have saved the document with the table created using the script above in a file named “TestTableStyle.indd”. This document is placed in a folder named “Styles” in the Presets folder for the application. (If you or your user does not have access to this folder, you will need to use another protected folder on your computer–the Library folder is suggested: “Macintosh HD:Library:”) With this document saved, you can now have a script create a new document using a document preset and import the table style as part of the document creation.
set testText to "This is Header Text" & return & "This is row one" & return & "This is row two" & return & "This is footer text" --spaces are tabs tell application "Adobe InDesign CC 2015" set docRef to document 1 tell page 1 of docRef set frameRef to make text frame with properties {geometric bounds:{100, 36, 200, 336}} set contents of frameRef to testText tell text 1 of frameRef to convert to table column separator tab row separator return end tell tell row 1 of table 1 of frameRef to merge tell row -1 of table 1 of frameRef to merge tell docRef if not (exists table style "Test Table Style") then set tableStyle to make table style with properties {name:"Test Table Style", header region cell style:"HeaderCellStyle", body region cell style:"BodyCellStyle", footer region cell style:"FooterCellStyle"} end if end tell tell table 1 of frameRef set row type of row 1 to header row set row type of row -1 to footer row set applied table style to "Test Table Style" end tell tell cells of table 1 of frameRef to clear cell style overrides end tell
You can now apply the table style to any table you create manually, but I prefer using a script.
You can apply the table style to tab/return delimited text you import from a plain text file or from a table in an Excel spreadsheet. The following demonstration script imports an Excel spreadsheet. For this, you will need an Excel spreadsheet saved to your Desktop folder. Use the DocWithTableStyle script above to create your document. Then with an insertion point active, run the following script to import and style your Excel spreadsheet. The script assumes that the entire file is to be imported from “Sheet1”.
(*Requires insertion point selection in document that has table style defined by variable tableStyleName*) set tableStyleName to "Test Table Style" set colWidth to 60 set rowHeight to 24 set rangeName to missing value set sheetName to "Sheet1" --optionSettings: show options, decimal places, preserve graphics, show hidden cells, use typographers quotes set optionSettings to {2, true, true, true} set defaultLoc to path to desktop folder from user domain as alias try set docRef to getDocRef() set fileRef to chooseExcelForTable(defaultLoc) setExcelImportPrefs(rangeName, sheetName, optionSettings) placeExcelFile(docRef, fileRef, tableStyleName, colWidth, rowHeight) on error errStr activate display alert errStr return end try --======== --HANDLERS --======== (*Places the Excel file referenced, applies the table style and sets basic table properties*) on placeExcelFile(docRef, fileRef, tableStyleName, colWidth, rowHeight) tell application "Adobe InDesign CC 2015" set measurement unit of script preferences to points set selList to selection set pageRef to active page of active window if class of item 1 of selList is not insertion point then error "Requires insertion point selection for running script" end if set tableStyleRef to table style tableStyleName of docRef set insertRef to item 1 of selList tell insertRef place file fileRef --without showing options end tell set frameRef to item 1 of parent text frames of insertRef set tableRef to table -1 of frameRef tell columns of tableRef to set width to colWidth tell rows of tableRef to set height to rowHeight tell tableRef set applied table style to tableStyleRef set row type of row 1 to header row clear table style overrides end tell end tell end placeExcelFile (*Sets preferences for importing Excel files*) on setExcelImportPrefs(rangeName, sheetName, optionSettings) tell application "Adobe InDesign CC 2015" tell excel import preferences if rangeName is not missing value then set range name to rangeName end if set table formatting to excel unformatted table set alignment style to spreadsheet set decimal places to item 1 of optionSettings set preserve graphics to item 2 of optionSettings set show hidden cells to item 3 of optionSettings set use typographers quotes to item 4 of optionSettings end tell end tell end setExcelImportPrefs (*Allows user to choose Excel file to place*) on chooseExcelForTable(defaultLoc) set testIt to choose file with prompt "select Excel file for table" default location defaultLoc without multiple selections allowed set myTest to info for testIt if file creator of myTest is not "XCEL" or type identifier of myTest is not "com.microsoft.excel.xls" then error "Check to make sure file choice is an EXCEL file" end if return testIt as string end chooseExcelForTable (*Returns reference to active document; otherwise generates error.*) on getDocRef() tell application "Adobe InDesign CC 2015" if modal state = true then error "Please close dialogs before running script" end if if not (exists document 1) then error "Requires active document" else return document 1 end if end tell end getDocRef
Our sample file imported and styled
This last script is a little involved, but notice how readable the top portion of the script is because we have put most of the functionality into handlers. Not only do handlers make a script more readable, but they can be used over and over again.
Now that you have the basics for going from start to finish (creating a document and adding a table style, then placing an Excel file at the insertion point), you are well on your way to automating any project that involves creating tables. See you next time for more tips on working with tables.