Skip to main content
birdinatree
Participant
December 1, 2016
Answered

Script to search table in selected text frame

  • December 1, 2016
  • 1 reply
  • 2817 views

Hello,

I'm trying to get a script for applying some cell styles based on content to work. Since I'm really new to scripting I can't get this to work as I want to.

The following script does what I want, but it applies to every table in the document. I just want it to apply it on the tables in a selected text frame.

Can anyone help me with this?

Also, is it possible to have more than one word to search for?

var curDoc = app.activeDocument; 

var allTables = curDoc.stories.everyItem().tables.everyItem();  

 

app.findTextPreferences = app.changeTextPreferences = null; 

app.findTextPreferences.findWhat = "Vev"; 

var allFounds = allTables.findText(); 

app.findTextPreferences = app.changeTextPreferences = null; 

 

for ( var i = 0; i < allFounds.length; i++ ) { 

    var tableFound = allFounds

    if ( tableFound.length > 0 ) { 

        for ( var j = 0; j < tableFound.length; j++ ) { 

            var curFound = tableFound

            var cellsInRow = curFound.parent.parentRow.cells.everyItem();          

            cellsInRow.appliedCellStyle = curDoc.cellStyles.itemByName( "Reglage line" ); 

            cellsInRow.clearCellStyleOverrides( true ); 

        }

    }

}

This topic has been closed for replies.
Correct answer Jongware

1. Don't use curDoc.stories.everyItem().tables.everyItem();, use app.selection[0].tables.everyItem();

(where 'app.selection[0]' is your current selection - so make sure a text frame is selected).

2. Just add it to the find text:

app.findTextPreferences.findWhat = "Word1 word2";

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
December 1, 2016

1. Don't use curDoc.stories.everyItem().tables.everyItem();, use app.selection[0].tables.everyItem();

(where 'app.selection[0]' is your current selection - so make sure a text frame is selected).

2. Just add it to the find text:

app.findTextPreferences.findWhat = "Word1 word2";

birdinatree
Participant
December 1, 2016

Hello Jongware,

Thank you very much! app.selection[0].tables.everyItem() worked perfectly

In my own attempts I missed [0] after app.selection.

I realized that I was a bit unclear on the second question.

What I want to achieve is to have different search phrases. So I guess I have to create some kind of loop to first search for "phrase 1" and then apply the cell style, then "phrase 2" and apply cell style for that row and so on.

Thanks again and I will mark the question as answered since it solved my main issue.

Jongware
Community Expert
Community Expert
December 2, 2016

Ah, no problem either.

You could issue two separate findText commands, but then you'd have to run the loop twice as well. In this case, you are better off with a GREP search instead, which can look for more than one phrase.

The main difference between GREP and the regular find is that GREP is case sensitive by default, whereas findText is case insensitive. So to do the same (if necessary), add '(?i)' at the front of the expression. Also, in findText you can instruct it to find entire words only (although you don't set that in the findTextPreferences in your own word), and in GREP you have to add a few more codes.

Try this:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "(?i)\\b(word1|word2)\\b";

var allFounds = allTables.findGrep();

You don't need to set changeGrepPreferences because you use findGrep and not changeGrep.

The \b codes are the word delimiters; they need their backslashes doubled because this is a JavaScript string, and if you don't, the backslashed code would be parsed before it gets handed over to GREP. (A common pitfall; there must be hundreds of questions about this here! So best to get used to it right away.)

The parenthesized "word1|word2" is a regular GREP "x or y".