: Automatically apply table cell style based on whether the cell is empty or has content I have a document that is frequently modified with a lot of tables. There are two table cell styles used
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");
}
}
More posts by @Connie430
1 Comments
Sorted by latest first Latest Oldest Best
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');
exit();
}
var sel = app.selection[0];
// stop the execution when there is no table
if(sel.hasOwnProperty('tables') === false) {
$.writeln('no tables');
exit();
}
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
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.