Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
}
}
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Awesome that worked! Thanks so much Manan!
Copy link to clipboard
Copied
Thats great Mike that it has worked, i think this post can be marked answered then, you need to do that.
Copy link to clipboard
Copied
Hi,
The code above could be simplified to
var myDocument = app.documents.item(0);
var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < allCells.length; i++)
{
if ( allCells.contents === '')
{
allCells.appliedCellStyle = 'EmptyCell';
allCells.clearCellStyleOverrides();
}
}
This removes the need for so many loops, as we just get a list of all the cells in the whole document, and cycle through them once.
(let me know if you want this over more than one document as this can be done easily too)
Regards
Malcolm