Hello,
A script to process tables in long documents is being created and I’d like assistance to craft a more elegant solution than my novice programming capability can produce. I have utilised various bits of code and added some of my own to create a solution. The core is Jongware’s wonderful table processor, https://creativepro.com/tackling-tables-through-scripting.php. The first two tasks that I have added are described below.
The first task identifies content that is incorrectly enclosed in a table and convert the tables to text. The first row of a table is searched, a list of GREP expressions is iterated, and a match would result in that table being converted to text.
The second task clears all formatting override from the placed tables and then applies table and cell styles.
My question is how do I eliminate the two calls to the findItems() function within the processTable function? I think it would be better to have a preprocessing of tables that preforms the work of findItems() and then it calls processTable or have the findItems() function called just once from within processTable. I hope that you have understood what I have said and can help. Please ask me question. Thank you, Ian
Code:
function processTable(table)
{
RemoveBlankLeftColumns (table);
try
{
findItems(table, “Activity+.d.d”);
findItems(table, “Example”);
}
catch (_) {}
try
{
// clear all formatting prior to applying style
table.cells.everyItem().clearCellStyleOverrides();
table.cells.everyItem().texts[0].clearOverrides();
table.appliedTableStyle = “ts – main blue table”;
table.rows[0].cells.everyItem().appliedCellStyle = “ts – head cells”;
table.rows.itemByRange(1,-2).cells.everyItem().appliedCellStyle = “ts – body cell normal”;
//table.columns[0].cells.everyItem().appliedCellStyle = “ts – left col cells”;
table.everyItem().clearTableStyleOverrides(true);
}
catch (_) {}
}
function findItems(table, grepStr) {
try
{
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeLockedStoriesForFind = true;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeMasterPages = false;
app.findChangeGrepOptions.includeFootnotes = false;
app.findGrepPreferences = null;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = grepStr;
var myFoundItems = table.findGrep();
for (var i =0; i < myFoundItems.length ; i++) {
//$.write(myFoundItems[i].length + “\r”);
$.write(myFoundItems[i].contents + “\r”);
$.write(myFoundItems.constructor.name + “\r”);
myFoundItems[i].appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item(“Heads”).paragraphStyles.item(“ha – activity”);
if (myFoundItems[i].parent.constructor.name == “Table”)
{
myFoundItems[i].parent.convertToText(“”, “\r”);
return $.write(“Grand Parent Table” + “\r”);
}
else if (myFoundItems[i].parent.parent.constructor.name == “Table”)
{
myFoundItems[i].parent.parent.convertToText(“”, “\r”);
return $.write(“Great Grand Parent Table”+ “\r”);
}
}
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
}
catch (_) {}
}