I have a document that is frequently modified with a lot of tables. There are two table cell styles used in the document. One is used when a cell is empty; the other is used when it is not empty.

Right now, I apply the "has data" style to all cells, then manually apply the "disabled" style to all the cells that are empty. Typically the tables are updated by deleting their contents and copy/pasting the data from Excel.

What I'd like to happen is that the cell style is applied automatically, depending on whether it has content or not. It doesn't look like there's a "Grep table cell style" feature in CS5. Is there another way to do this?

I suspect this could be scripted, but I'm having trouble understanding how to do that. Below is my non-working attempt:

// This doesn't work
var mT = app.selection[0].tables[0];
var len = mT.cells.length;

while (len-->0) {
    if (mT.cells[len].contents == "") {
        mT.cells[len].applyCellStyle("Disabled cell");
    else {
        mT.cells[len].applyCellStyle("Has data");

1 Answer 1


You nearly got it right. - You need to loop the cells of the table with an iterator.
- Also while(len-->0) is not a JavaScript expression.
- To apply a style you need to assign it. See the code below.

Tested on macOS 10.12.1 in InDesign 2017

/* global app $ exit */

// stop the execution when nothing is selected
if(app.selection.length < 1) {
  $.writeln('no selection');
var sel = app.selection[0];
// stop the execution when there is no table
if(sel.hasOwnProperty('tables') === false) {
  $.writeln('no tables');
var table = sel.tables[0]; // get the selected table

// great we made it here - so there must be a table in the selection
// loop all the cells in that first table in the selction
for(var i = 0; i < table.cells.length; i++) {
  // check if the content is an empty string
  if (table.cells[i].contents === '') {
    // set the appliedCellStyle to the CellStyle named "foo"
    // if the styles don't exist this will through an error
    table.cells[i].appliedCellStyle = 'foo';
  } else {
    // if the cells content is not an empty string
    // set the appliedCellStyle to the CellStyle named "bah"
    // if the styles don't exist this will through an error
    table.cells[i].appliedCellStyle = 'bah';

// we are done
  • This works, and the comments are greatly appreciated. I found this exceedingly slow (which isn't the fault of the script). To speed it up, I removed the else { ... } condition for applying the "bah" style. So that only empty cells need to be touched. That greatly increased the speed. Dec 7, 2016 at 14:57
  • 1
    You are welcome and - Yes that's right. It makes the script slow when checking each cell. If you want to have a CellStyle applied to all prior to checking for content you could add the following statement before the loop. table.cells.everyItem().appliedCellStyle = 'bah'; Dec 8, 2016 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.