Use Find/Change to Add Text to Many Table Cells
Will asked:
Is it possible to ‘Add/insert’ at the end of a sentence in a table?
The best and quickest way to add text at the beginning or end of many table cells is to:
- Select the table cells you want to affect. You can select an entire row or column, or, to select the entire table, just click inside the table with the Type tool and then press Command-Option-A or Ctrl-Alt-A.
- Open Edit > Find/Change and switch to the GREP tab (don’t worry, it’s easy!)
- Set the Search pop-up menu (the scope) to Selection
- Search for
./z
and change to$0ABC
(where “0” is a zero and “ABC” is whatever text you want to insert at the end of the table cell. - Click Change All
That will only place the text (“ABC” in the example) in table cells that are not empty. (I haven’t found a way to place text inside empty cells; if you know of one, type it below in the comments.)
Placing the same text in empty table cells:
I nearly got it to work with ^$ (caret dollar), from the GREP menu Locations – “Beginning of Para” and “End of Para”.
It certainly **finds** only empty cells (if the scope of the search is just the table), but I can’t get it to add anything with the Replace text.
FWIW: Finding ^$ is fine but could catch extra returns. Deactivating the multi-line search can prevent it: (?-m)^$
My best try to add something in an empty cell is to copy the text to be added (ABC in your example) then replace by ~C (clipboard contents without formatting). However, it comes with two important limitation: “Replace all” does not work. You have to keep hitting the Change/Find button. Second limitation is: for some unknown reason, it does not catch the last cell is empty…
An interesting fact is that the meta-character `\z` (backslash z) actually captures the last insertion point of any cell (or story), but since the content of this insertion point is empty the ‘replace’ feature does not function in the GUI.
However, at the scripting level those insertion points are all nicely collected and can be augmented this way:
// Assuming you selected the target cells
app.findGrepPreferences.findWhat = "\\z";
var a = app.selection[0].findGrep();
while( ip=a.pop() ) ip.contents="hello";
Best,
Marc
Hi Marc,
My version (a long time ago):
var myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements(),
C = myCells.length, c;
for ( c = 0; c < C; c++) if ( myCells[c].contents == "" ) myCells[c].contents = "–";
(^/)
This one worked for me (unfortunately only with “Change/Find” not with “Change All”):
Search for: \z
Change to: ABC$0
David, in point 4 you typed »/z« (slash z), which is wrong. Your screenshot is correct and shows »\z« (backslash z). Please correct it above.
Cheers, Tobias
We often have to fill empty table cells with an “en dash”. After trying Find/Change unsuccessfully, I found a useful Script, similar to the one already posted. This script places an en dash in each empty cell. Script can be edited to add any desired text, by replacing the en dash in the following line of the code:
mycells[i].contents = “–”
SCRIPT:
try
{
var myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
alert(“myCells:” + myCells.length)
for(i=0; i<myCells.length; i++)
{
if(myCells[i].contents == "")
{
myCells[i].contents = "–"
}
}
}
catch(myError)
{
alert("no tables found in the active document")
}
This is great, Marion! I put it in a text file with the javascript extension. Just tested it on a simple table in CC 2019, works fine. You can download it here: https://creativepro.com/downloads/fill_empty_cells.jsx.zip
Any chance to change this script to act not on the whole document but only to selected tables?