Reply To: Looping Through InDesign Table for Specific String

#90661
Mike Dean
Member

I think Loic’s approach will be simpler and more efficient, but as a learning exercise I thought I’d point out a couple of reasons why your original code isn’t working. First, the curly brace {} nesting isn’t quite right. Everything inside the for loop needs to be in a single set of curly braces. Then, everything that depends on an if statement needs to be in its own set of braces. So you’d want something like this:
for (i=0;i<myTable.cells.length;i++){
if (myTable.cells[i].texts[0].contents[0] == “Air Conditioning & Heating”){
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Heating & Air Conditioning”;
}
[[[Additional if statements here]]]
}

In your existing code, the for loop ends at the first closing curly brace, then errors because the next line begins with an opening brace that it doesn’t know what to do with. Also, it’s always placing “Heating & Air Conditioning” because an if statement that doesn’t have a set of curly braces after it only applies to the next line. So myTable.rows.add depends on the if statement, but myTable.rows[-1].contents will always run.

Second, I think the if statement test might not come out quite right. I’m pretty sure .contents[0] will give you the first letter of the contents, so your first test winds up being something like “H” == “Air Conditioning & Heating”.

This article was last modified on December 21, 2016

Comments (0)

Loading comments...