CreativePro Forum
Join our community of graphic designers, publishers, and production artists from around the world. Our members-only forum is a great place to discuss challenges and find solutions!
- You must be logged in to reply to this topic.Login
Looping Through InDesign Table for Specific String
Tagged: Tables
- This topic has 5 replies, 3 voices, and was last updated 8 years, 11 months ago by
Ashley Parker.
-
AuthorPosts
-
-
December 20, 2016 at 2:22 pm #90644
Ashley Parker
MemberHello! I am trying to use JS to loop through a table in InDesign for specific Table of Contents entries and, if found, add additional entries to the bottom of the table. My code is below. Currently, it is adding the first additional entry, “Heating & Air Conditioning”, even though it shouldn’t be because the “Air Conditioning & Heating” entry does not exist in the table I’m testing on. Also, the code breaks as soon as it gets to the second if loop, and I can’t figure out why. Any assistance would be greatly appreciated!
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”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Basement Remodeling & Additions”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Home Additions & Basement Remodeling”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Bathroom & Kitchen Remodeling”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Kitchen & Bathroom Remodeling”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Carpet, Upholstery & Rug Cleaning”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Upholstery, Rug & Carpet Cleaning”;
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Rug, Carpet & Upholstery Cleaning”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Carpet & Upholstery Cleaning”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Upholstery & Carpet Cleaning”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Chimney & Fireplace Work”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Fireplace & Chimney Work”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Closet & Garage Organizers”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Garage & Closet Organizers”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Drain/Sewer Cleaning & Repair”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Sewer/Drain Cleaning & Repair”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Driveways – Asphalt”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Asphalt Driveways”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Driveways – Concrete”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Concrete Driveways”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Heating & Air Conditioning”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Air Conditioning & Heating”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Patio Covers & Pergolas”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Pergolas & Patio Covers”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Patios & Decks”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Decks & Patios”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Pest & Termite Control”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Termite & Pest Control”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Shutters, Shades & Blinds – Custom”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Blinds, Shutters & Shades – Custom”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Shutters & Blinds – Custom”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Blinds & Shutters – Custom”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Shutters & Window Treatments – Custom”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Window Treatments & Shutters – Custom”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Sunrooms & Patio Enclosures”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Patio Enclosures & Sunrooms”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Tile, Grout & Stone Cleaning”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Stone, Tile & Grout Cleaning”;
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Grout, Tile & Stone Cleaning”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Water Damage & Mold Remediation”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Mold & Water Damage Remediation”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Window & Door Replacement”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Door & Window Replacement”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Window Cleaning & Pressure Washing”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Pressure Washing & Window Cleaning”;
}
{
if (myTable.cells[i].texts[0].contents[0] == “Window Tinting”)
myTable.rows.add(LocationOptions.AT_END, myTable);
myTable.rows[-1].contents = “###Glass Tinting”;
} -
December 20, 2016 at 2:42 pm #90646
Anonymous
InactiveMay I suggest this approach ?
var main = function() {
var doc = app.properties.activeDocument, myTable, cells, strings, cell;
if ( !doc || app.selection.length != 1 || !(app.selection[0] instanceof Table )) {
alert(“You need to select a table to get this script up and running”);
return;
}strings = {
//Property name = expected cell content
//Property value stands for the new string value
“Air Conditioning & Heating” : “Heating & Air Conditioning”,
“Basement Remodeling & Additions” : “Home Additions & Basement Remodeling”,
//Etc.
}myTable= app.selection[0];
cells = myTable.cells.everyItem().getElements();
while ( cell = cells.shift() ) {
if ( strings[cell.contents] ) {
myTable.rows.add().cells[0].contents =”###” + strings[cell.contents];
}
}
}var u;
app.doScript ( “main()”,u,u,UndoModes.ENTIRE_SCRIPT, “The Script” );
Otherwise if the table is filled with strings only without any special characters nor anchored objects, you may have take advantage of the myTable.contents property which returns an array of strings. Then you could have looped through this array, do changes and fill this array back into the table. Much much quicker ;)
-
December 20, 2016 at 2:53 pm #90652
Ashley Parker
MemberHmmm… thank you for your quick response! Unfortunately, this code doesn’t do anything for my Table of Contents. :( No new entries are populated.
-
-
December 20, 2016 at 3:33 pm #90653
Anonymous
InactiveHow did you set the TOC ? Normally it’s something you update at any time. A script won’t add entries by itself but it may add styled contents that the TOC will spot and list.
Or may I have misunderstood something ?
Apart from that my code was an attempts to simplify your code and the multiple “if” enumerations.-
December 27, 2016 at 9:49 am #90785
Ashley Parker
MemberThe ToC is pulled from an EasyCatalog panel. I didn’t write the original script, so I’m not sure what you mean when you ask how I set the ToC. Basically, I’m trying to get it to add additional names for the entries that already exist in the EasyCatalog panel it’s reading from. (Sorry it’s taken me so long to reply! Holidays and all.)
-
-
December 21, 2016 at 8:52 am #90661
Mike Dean
MemberI 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”.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
