Skip to main content
mikefwd
Known Participant
November 2, 2018
Question

Assign cell style to empty table cells + clear cell style overrides

  • November 2, 2018
  • 1 reply
  • 1476 views

I’m data merging into a table which will create a multi page document with some empty cells.

I’m trying to write a script that applies a cell style to empty cells and then clears cells style overrides for empty cells as well.

So far I’ve written the following (where 'EmptyCell' is the cell style I want to apply to all empty cells):

var myDocument = app.documents.item(0);

var myTable = myDocument.stories.item(0).tables.item(0);

for(var i = 0; i < myTable.cells.length; i++) {

  if (myTable.cells.contents === '') {

    myTable.cells.appliedCellStyle = 'EmptyCell';

  }

}

for(var i = 0; i < myTable.cells.length; i++) {

  if (myTable.cells.contents === '') {

    myTable.cells.clearCellStyleOverrides();

  }

}

This does what I want it to do, but only to the first table in the multi page document.

What do I need to do to make this cell work for all tables in the multi page document??

I’m a total beginner at scripting so any help would be much appreciated.

Cheers

Mike

This topic has been closed for replies.

1 reply

Community Expert
November 2, 2018

Hi Mike,

You just to iterate every story and within every story iterate the tables it contains and then run you code over it. I have enhanced your code with this and it should work fine now

var myDocument = app.documents.item(0);

var stories = myDocument.stories//item(0).tables.item(0);

for(var sc = 0 ; sc < stories.length; sc++)

{

     for(var tc = 0; tc < stories[tc].tables.length; tc++)

     {

          var myTable = stories[sc].tables[tc]

          for(var i = 0; i < myTable.cells.length; i++)

          {

               if (myTable.cells.contents === '') {

                    myTable.cells.appliedCellStyle = 'EmptyCell';

               }

          }

          for(var i = 0; i < myTable.cells.length; i++)

          {

               if (myTable.cells.contents === '') {

                    myTable.cells.clearCellStyleOverrides();

               }

          }

     }

}

-Manan
Community Expert
November 2, 2018

Also you could optimize your code a bit, instead of using two loops one for applying the cell style and other for clearing the overrides try to see if it can be done using just a single loop

-Manan

-Manan