TEXT IN TABLES

When it comes to text in tables, InDesign provides a wealth of options for placing the text as well as converting placed text to a table. In this post we will explore some of the options.

Existing Table

You can add text to any cell within a table using the cell’s contents property.

set cellIndex to 10
tell application "Adobe InDesign CC 2018"
   set tableRef to table 1 of text frame 1 of document 1
   tell tableRef
	set contents of cell cellIndex to "This is a Headline"
   end tell
end tell

The index of a cell (cellIndex above) is its position within the table counting from left to right, top to bottom. Of course you can reference a cell by its index within a row.

 

tell application "Adobe InDesign CC 2018"
   set tableRef to table 1 of text frame 1 of document 1
   tell tableRef
	set contents of cell 3 of row 2 to "This is its contents"
   end tell
end tell

Table cells also have a name value which returns its position within a table by column and row.

 

tell application "Adobe InDesign CC 2018"
   set tableRef to table 1 of text frame 1 of document 1
   tell tableRef
       set contents of cell "4:2" to "This is its contents"
       set testName to name of cell 2 of row 4
   end tell
end tell
testName

If your script has a list of values, these values can be applied to a table using cell indexing order:

set myList to {"Table Head", "1234", "2345", "3456", "4567"}
tell application "Adobe InDesign CC 2018"
   set tableRef to table 1 of text frame 1 of document 1
   tell tableRef
	set contents to myList
   end tell
end tell

All of the above examples assume there is an existing table within a text frame, but what if the text frame has text that needs to be converted to a table. The method convert to table can be called by any of the following objects: text, character, word, line, text column, paragraph, text style range, and insertion point. The following are a few examples:

 

CONVERT TO TABLE

Assuming tab-return delimited text is selected in a document:

Text Selection

tell application "Adobe InDesign CC 2018"
   if class of item 1 of selection is text then
	set textRef to a reference to item 1 of selection
	tell textRef
	   set tableRef to convert to table column separator "\t" row separator "\r"
	end tell
   end if
end tell

Note: When you compile the script, the back-slashed text for tab and row indicators disappear but the actual characters are there; you will just see a big space where the tab was and a paragraph return before the ending quote for the row. When you place text from a file, converting text to a table uses the styling of the active insertion point.

 

Text Frame Selection

set filePrompt to "Choose fab-delimited file for table"
set fileChoice to choose file with prompt filePrompt
tell application "Adobe InDesign CC 2018"
   set selRef to item 1 of selection
   if class of selRef is text frame then
	tell selRef to place fileChoice
	tell text 1 of selRef
	   convert to table column separator "\t" row separator "\r"
	end tell
   end if
end tell

To insert text to be converted to a table at the end of a text flow you can refer to the text item as the story’s last text style range (text style range -1). The trick is to set the styling for the insertion point that places the text to a paragraph style other than that used for all other text.

 

Insertion Point

(*Requires insertion point selection and paragraph style “Desc1” for document*)

set fileChoice to choose file with prompt "Select file to read"
tell application "Adobe InDesign CC 2018"
   if class of item 1 of selection is insertion point then
	set myStyle to paragraph style "Tabletext" of document 1
	set insPoint to item 1 of selection
	set storyRef to parent story of insPoint
	tell insPoint
	   set applied paragraph style to myStyle
	   set textRef to place fileChoice
	end tell
	tell text style range -1 of storyRef
	   set tableRef to convert to table column separator "\t" row separator "\r"
	end tell
   end if
end tell

To place text within a story and convert it to a table takes a little more doing. The thing to remember is that the reference to a placed object is a list. For text the first item of the reference returned is the insertion point at the end of the text placed. The following requires an insertion point selection for the active document.

set fileChoice to choose file with prompt "Select file to read"
tell application "Adobe InDesign CC 2018"
   if class of item 1 of selection is insertion point then
	set insPoint to item 1 of selection
	set beginPt to index of insPoint
	set storyRef to parent story of insPoint
	tell insPoint
	   set textRef to place fileChoice
	   set endPt to index of item 1 of textRef
	end tell
	set objRef to a reference to text from character beginPt to character endPt of storyRef
	tell objRef
	   set tableRef to convert to table column separator "\t" row separator "\r"
	end tell
   end if
end tell
objRef

ONWARD AND UPWARD

To style the text within a table the script under the heading Insertion Point above demonstrates that text placed and converted to a table inherits the styling set for its parent insertion point, In the next post we will explore more ways that text can be styled within a table.

USING INDESIGN 2019?

Although the scripts above are referencing Adobe InDesign CC 2018, they are tested in InDesign CC 2019. Just change the year and they will work the same. We will be changing our script’s application reference at the beginning of the next blog post series.

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.