Copy link to clipboard
Copied
Hi all,
I'm an absolute beginner so apologies if this has been answered before, I couldn't find an exact solution for my problem.
I need to apply a cell style to certain cells where varying words appear in a table. It would be awesome if I could merge the row that the words appear in too, but that is a bonus for now.
I found this: How to apply a table cell style based on grep search?
Which works great, but instead of just applying the style to the word 'Banana', I need to apply it to a bunch of other words, for example 'Apple', 'Pear', etc.
Any help would be massively appreciated.
Thank you!
Copy link to clipboard
Copied
Hi,
Need to apply whole paragraphs or specific words inside the cells? U can use paragraphs or words. can you provide any samples with screenshots?
Copy link to clipboard
Copied
Hi, just whatever is in the cell, it is usually just one or two words.
Copy link to clipboard
Copied
try this.
var myDoc = app.activeDocument;
var tab = myDoc.stories.everyItem().tables[0];
tab.cells.everyItem().appliedCellStyle = "Style1"
Copy link to clipboard
Copied
Wouldn't that be for every cell in the table? I only want it applied to specific cells with certain words in them. So for example, like this:
var myDoc = app.activeDocument
app.findTextPreferences = app.changeTextPreferences = null
app.findTextPreferences.findWhat = ('Banana')
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{
if(myFound.parent.constructor.name == "Cell")
{
myFound.parent.appliedCellStyle = "Style1"
var overrides = myFound.clearOverrides() //this is the new line added in this content
}
}
So it will find where the word 'Banana' is in the table and apply the style to that. However, I want to add more words, so in addition to 'Banana', it also applies the style to cells containing the word 'Apples'. I have about 30 words I want to add to the list.
Does that make sense?
Thank you!
Copy link to clipboard
Copied
Hi,
Use findGrep instead of findText with'or' condition. Try like this.
var myDoc = app.activeDocument
app.findGrepPreferences = app.changeGrepPreferences = null
app.findGrepPreferences.findWhat = ('Banana|Apple') // add words with |
var myFound = myDoc.findGrep()
for(i=0; i<myFound.length; i++)
{
if(myFound.parent.constructor.name == "Cell")
{
myFound.parent.appliedCellStyle = "Style1"
var overrides = myFound.clearOverrides() //this is the new line added in this content
}
}
Copy link to clipboard
Copied
This works however it's created another problem haha.
I have some other cells with the word Banana, (eg. "Bemboka Banana Co.") but I don't want to apply the style to these cells. Just the ones with the word Banana, and ONLY the word Banana. Is there a way to force it to only work on the cells with exclusively the word I specify?
Thanks again!!
Copy link to clipboard
Copied
Provide the samples with all cases. Also u can try with regex patterns. pls refer indesign greps.
Copy link to clipboard
Copied
Hi Daniiiel,
Try the following code
var listtoSearch = {"Banana":1, "Apple":1}
var a = app.documents[0].stories.everyItem().tables.everyItem().cells.everyItem().getElements()
while(i = a.pop())
{
if(listtoSearch[i.contents])
{
i.appliedCellStyle = "Style1"
var overrides = i.texts[0].clearOverrides() //this is the new line added in this content
}
}
-Manan
Copy link to clipboard
Copied
Hi Manan, thank you so much for your help and apologies for the very late response.
Is there a way I can apply this scrip just to the selected column and not to the whole table?
Thank you so much!
Copy link to clipboard
Copied
Try the following changed version
var listtoSearch = {"Banana":1, "Apple":1}
var sel = app.selection[0]
if(sel.constructor.name == "Cell")
var a = sel.cells.everyItem().getElements()
while(i = a.pop())
{
if(listtoSearch[i.contents])
{
i.appliedCellStyle = "Style1"
var overrides = i.texts[0].clearOverrides() //this is the new line added in this content
}
}
-Manan
Copy link to clipboard
Copied
You could also use grep to do this, try the following as well
app.findGrepPreferences.findWhat = "^Banana$|^Apple$"
var res = app.selection[0].findGrep()
while(i = res.pop())
{
var par = i.parent
if(par.constructor.name == "Cell")
{
par.appliedCellStyle = "Style1"
par.texts[0].clearOverrides()
}
}
app.findGrepPreferences = NothingEnum.nothing;
-Manan
Copy link to clipboard
Copied
Amazing, this worked perfectly, thank you!
Do you think there's a way to use the same list to merge cells in the same row as the defined word?
For example, merge cells in the row that the word 'Banana' appears in?
Thank you!