TABLE CELLS

A table cell is an element of a table which is an element of a page item (such as text frame) within a document page or spread.

Get Reference from Selection

If the user has made a table cell selection, you can get the reference to the cell using the following:

set cellRef to missing value
tell application "Adobe InDesign CC 2018"
   set selList to selection
   set selClass to class of item 1 of selList
   if selClass is insertion point then
	set cellRef to parent of item 1 of selList
   else if selClass is in {paragraph, word, character, text} then
	set cellRef to parent of item 1 of selList
   else if selClass is cell then
	set cellRef to selClass
   end if
end tell

if, after execution, the value of cellRef is missing value, alert the user that a selection is required.

Once you have a reference to a table cell, you can get values of properties or set properties as needed. Assuming cellRef is a reference to a cell within a table:

if cellRef is not missing value
   tell cellRef to get properties
end tell

Adding this to the script from above, we can see from the result that the cell can have fill color fill tint, gradient fill, contents, and insets for graphics and texts. They can have diagonal line strokes and each side of the cell can have a stroke. Each stroke has all of the properties associated such as type, weight, color, tint, gap, and so on. Cells can be classified as a row type and/or cell type, and can be associated with an XML element. Additionally, you can set properties that control its height and rotation angle. That’s a lot of information. If you just want to set its fill color and fill tint, no problem.

tell cellRef
   set fill color to swatch 5 of docRef
   set fill tint to 100
end tell

But should you want to apply a number of properties to a number of cells, you might want to use a cell style. This handler checks for an existing cell style; if one does not exist the style is created using a property record passed as part of its arguments.

set stylename to "CellText"
tell application "Adobe InDesign CC 2018"
   set docRef to document 1
   set myColor to swatch 4 of docRef
   set propList to {fill color:myColor, fill tint:100}
   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

Get Reference using Cell’s Index

Should we know that the table cell to be targeted is defined by its index within a given table, we can get the reference to the cell using a script similar to the following:

tell application "Adobe InDesign CC 2018"
   set cellRef to cell 3 of table 1 of text frame 1 of document 1
   get properties of cellRef
end tell

An interesting property of table cells is row type which can be body row, header row, footer row, or mixed state.

tell application "Adobe InDesign CC 2018"
   set cellRef to cell 3 of table 1 of text frame 1 of document 1
   get row type of cellRef
end tell

Not to be confused with row type, the cell type property determines if the cell is text type cell or graphic type cell. By default cell type is text type cell.

tell application "Adobe InDesign CC 2018"
	set cellRef to cell 1 of table 1 of text frame 1 of document 1
	get cell type of cellRef
end tell

Prior to the release of InDesign 2015 all table cells had the behavior of text frames (text type cells). This made placing images inside a cell difficult. Placed as an inline graphic these graphics were hard to resize. As of 2015 table cells can now be one of two types: text type cell or graphic type cell

When you add an image to a cell manually or with a script, its cell type changes to graphic type cell. you can test this with the following:

set fileRef to choose file
tell application "Adobe InDesign CC 2018"
	set cellRef to cell 1 of table 1 of text frame 1 of spread 1 of document 1
	set test1 to cell type of cellRef
	tell insertion point 1 of cellRef to place fileRef
	set test2 to cell type of cellRef
end tell
{test1, test2}

Text Type Cell

Cells having cell type of text type cell have properties similar to text frames. To set the literal text contents of a cell, a script could be written as follows:

--assumes paragraph style named cellText
set myText to "Sample Text Here"
tell application "Adobe InDesign CC 2018"
	set myStyle to paragraph style "cellText" of document 1
	set cellRef to cell 2 of table 1 of text frame 1 of spread 1 of document 1
	tell cellRef
		set contents to myText
		set vertical justification to center align
		set text left inset to "10 pts"
		set text right inset to "10 pts"
		set applied paragraph style of paragraph 1 to myStyle
	end tell
end tell

Graphic Type Cell

As mentioned above, once you add a graphic to a table cell, its cell type becomes graphic type cell but you can also change cell types with your script using the convert cell type method:

tell application "Adobe InDesign CC 2018"
	set cellRef to cell 1 of table 1 of text frame 1 of spread 1 of document 1
	tell cellRef
		convert cell type final cell type graphic type cell
	end tell
end tell

Once converted you can use rectangle 1 of the cell to place the graphic:

set fileRef to choose file
tell application "Adobe InDesign CC 2018"
	set cellRef to cell 1 of table 1 of text frame 1 of document 1
	tell cellRef
		convert cell type final cell type graphic type cell
		set clip content to graphic cell to true
		tell rectangle 1
			place fileRef
			fit given proportionally
		end tell
	end tell
end tell

ONWARD AND UPWARD

KEEP TUNED AS WE INVESTIGATE WORKING WITH TABLES AND CELLS IN THE NEXT FEW BLOG POSTS.

Disclaimer

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