Copy link to clipboard
Copied
hi all, I need a little help modifying a script so I can apply changes to the text paragraph style and also changes to the parent cell it belongs to.
I found this code on here which works well for searching the selected table for a specific text (in this case "?" ) and applying a given paragraph style to the text.
function checkWhichTable() {
//ensure user made a selection
if (app.selection.length != 1) return null;
var currentTable = app.selection[0];
if (currentTable.hasOwnProperty("baseline")) currentTable = app.selection[0].parent;
while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
if (!(currentTable instanceof Table)) return null;
return currentTable;
}
app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");
function checkUserSelection() {
var a_table = checkWhichTable();
if (a_table == null) {
if (confirm("No table selected. Do you want to process *all* tables?") == false)
return;
allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (var a = 0; a < allTables.length; a++) processTable (allTables);
} else processTable (a_table);
}
function processTable(myTable) {
var myParas = myTable.cells.everyItem().texts[0].paragraphs.everyItem().getElements(),
P = myParas.length,
myTextInCell = ["?",],
myParaStyle = "table H2",
myRegEx = new RegExp("\\b(" + myTextInCell.join("|") + ")\\b");
for ( var p = 0; p < P; p++) if (myParas
.contents.match(myRegEx)) myParas
.appliedParagraphStyle = myParaStyle;
}
for example after this:
myTextInCell = ["?",],
myParaStyle = "table H2",
I would like to then apply some changes to it's parent cell, such as
height = "10mm";
appliedCellStyle = "Cell Style 1";
It's a little beyond me to figure out the code but would save a huge amount of effort when laying out tables so any help towards this end would be greatly appreciated!
Thanks!
This method uses grep and should be much quicker.
I just looked at the processTable function
...function checkWhichTable() {
//ensure user made a selection
if (app.selection.length != 1) return null;
var currentTable = app.selection[0];
if (currentTable.hasOwnProperty("baseline")) currentTable = app.selection[0].parent;
while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
if (!(currentTable insta
Copy link to clipboard
Copied
The code is very messy but you could change your code to.
function checkWhichTable() {
//ensure user made a selection
if (app.selection.length != 1) return null;
var currentTable = app.selection[0];
if (currentTable.hasOwnProperty("baseline")) currentTable = app.selection[0].parent;
while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
if (!(currentTable instanceof Table)) return null;
return currentTable;
}
app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");
function checkUserSelection() {
var a_table = checkWhichTable();
if (a_table == null) {
if (confirm("No table selected. Do you want to process *all* tables?") == false)
return;
allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (var a = 0; a < allTables.length; a++) processTable(allTables);
} else processTable(a_table);
}
function processTable(myTable) {
var myParas = myTable.cells.everyItem().texts[0].paragraphs.everyItem().getElements(),
P = myParas.length,
myTextInCell = ["\\?",],
myParaStyle = "table H2",
myRegEx = new RegExp("\\b(" + myTextInCell.join("|") + ")\\b"),
cell;
for (var p = 0; p < P; p++) {
if (myParas
.contents.match(myRegEx)) {
myParas
.appliedParagraphStyle = myParaStyle;
cell = myParas
.parent; // could do checks on this
cell.height = "15mm"; // change
cell.appliedCellStyle = "Cell Style 1";
}
}
}
You would be better off using grep than standard regex
Copy link to clipboard
Copied
This method uses grep and should be much quicker.
I just looked at the processTable function
function checkWhichTable() {
//ensure user made a selection
if (app.selection.length != 1) return null;
var currentTable = app.selection[0];
if (currentTable.hasOwnProperty("baseline")) currentTable = app.selection[0].parent;
while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
if (!(currentTable instanceof Table)) return null;
return currentTable;
}
app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");
function checkUserSelection() {
var a_table = checkWhichTable();
if (a_table == null) {
if (confirm("No table selected. Do you want to process *all* tables?") == false)
return;
allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (var a = 0; a < allTables.length; a++) processTable(allTables);
} else processTable(a_table);
}
function processTable(myTable) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.changeGrepPreferences.appliedParagraphStyle = "table H2";
app.findGrepPreferences.findWhat = "\\?"
var changes = myTable.cells.everyItem().texts[0].paragraphs.everyItem().changeGrep();
var cell, l, oldCell;
oldCell = false;
changes = [].concat.apply(null,changes); // flatten array
l = changes.length;
while (l--) {
cell = changes
.parent; if (cell !== oldCell){ // only make changes to cell if not already changed. Not sure if this makes a difference with InDesign
cell.height = "20mm";
cell.appliedCellStyle = "Cell Style 1";
}
oldCell = cell;
}
}
Copy link to clipboard
Copied
Ah, thankyou Trevor this is perfect, and works very smoothly indeed.
Can you please tell me how I might change it to apply to the entire row of cells containing the text?
Many thanks!!
Copy link to clipboard
Copied
Don't try this
(Deleted) ...
Copy link to clipboard
Copied
Tested this.
Had dumb copy paste error in the above so, I'll delete it.
This is tested
function checkWhichTable() {
//ensure user made a selection
if (app.selection.length != 1) return null;
var currentTable = app.selection[0];
if (currentTable.hasOwnProperty("baseline")) currentTable = app.selection[0].parent;
while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
if (!(currentTable instanceof Table)) return null;
return currentTable;
}
app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");
function checkUserSelection() {
var a_table = checkWhichTable();
if (a_table == null) {
if (confirm("No table selected. Do you want to process *all* tables?") == false)
return;
allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (var a = 0; a < allTables.length; a++) processTable(allTables);
} else processTable(a_table);
}
function processTable(myTable) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.changeGrepPreferences.appliedParagraphStyle = "table H2";
app.findGrepPreferences.findWhat = "\\?"
var changes = myTable.cells.everyItem().texts[0].paragraphs.everyItem().changeGrep();
var row, l, oldRow;
oldRow = false;
changes = [].concat.apply(null,changes); // flatten array
l = changes.length;
while (l--) {
row = changes
.parent.parentRow.cells.everyItem(); if (row !== oldRow){ // only make changes to row if not already changed. Not sure if this makes a difference with InDesign
row.height = "20mm";
row.appliedCellStyle = "Cell Style 1";
}
oldRow = row;
}
}
Copy link to clipboard
Copied
ah that's perfect. thanks Trevor!
this is just what I need for now, just thinking of other uses for this in the future....
how easy would it be to then apply the given paragraph style to the whole row, as opposed to just the single cell?
Copy link to clipboard
Copied
Ok for any more request on this one please contact me here Contact | Creative-Scripts.com for my paid custom script service.
function processTable(myTable) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\?"
var finds = myTable.cells.everyItem().texts[0].paragraphs.everyItem().changeGrep();
var row, l, oldRow;
oldRow = false;
finds = [].concat.apply(null,finds); // flatten array
l = finds.length;
while (l--) {
row = finds
.parent.parentRow.cells.everyItem(); if (row !== oldRow){ // only make finds to row if not already changed. Not sure if this makes a difference with InDesign
row.height = "20mm";
row.appliedCellStyle = "Cell Style 1";
row.texts[0].appliedParagraphStyle = "table H2";
}
oldRow = row;
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now