Copy link to clipboard
Copied
I have the script below, but when I run it, nothing happens and I can't understand why. It literally worked about 5 days ago and now after i updated yesterday its not working. Please Help. I am trying to apply a cell style based on cell content.
var myDoc = app.activeDocument
app.findTextPreferences = app.changeTextPreferences = null
app.findTextPreferences.findWhat = "1"
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{
if(myFound.parent.constructor.name == "Cell")
{
myFound.parent.appliedCellStyle = "Green"
}
}
Hi Liphou ,
after seeing into the sample document I'd prefer a GREP find to isolate cell contents like "D" vs "DP".
And you can use method clearCellStyleOverrides() on a cell.
Code below is using all this:
...app.doScript
(
main,
ScriptLanguage.JAVASCRIPT,
[],
UndoModes.ENTIRE_SCRIPT,
"Apply Cell Style to found Cell | Uwe | SCRIPT"
);
function main()
{
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat
Copy link to clipboard
Copied
Hi,
best make it a habit to end every single statement with a: ;
Like that:
var myDoc = app.activeDocument;
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = "1";
var myFound = myDoc.findText();
for(var i=0; i<myFound.length; i++)
{
if(myFound.parent.constructor.name == "Cell")
{
myFound.parent.appliedCellStyle = "Green";
};
};
I see no principle problem in the code.
What can go wrong then?
An error could be thrown if the cell style is hosted by a cell style group.
Regards,
Uwe
EDIT: To make that clear. Tested the code above with InDesign CC 2018.1 13.1.0.76 on Windows 10.
No problem found if the cell style named "Green" is in the root of the Cell Styles Panel.
Copy link to clipboard
Copied
Moving to InDesign Scripting forum
Copy link to clipboard
Copied
I second Laubender, the called cell style may be invalid as it.
Set a breakpoint and look at its status.
Copy link to clipboard
Copied
I have it in the cell styles panel, Di i need it to be anywhere else?
Copy link to clipboard
Copied
Hi graphicsguy85 ,
call the cell style in a different way and test if it is valid:
var cellStyle = app.documents[0].cellStyle.itemByName("Green");
if( !cellStyle.isValid ){ /*do something here*/ };
What can be done if the cell style is not valid?
Look after it in a nested structure of the Cell Styles Panel or add a new one if it still cannot be found.
Do something here can be written this way:
var allCellStylesOfDoc = app.documents[0].allCellStyles; // Returns array of cell styles
for(var n=0;n<allCellStylesOfDoc.length;n++)
{
if(allCellStylesOfDoc
.name == "Green") { cellStyle = allCellStylesOfDoc
; break } }
if( !cellStyle.isValid )
{
app.documents[0].cellStyles.add
(
{
name : "Green" ,
/*
add other properties here. Look after read/write values you want to see.
I inserted only pseudo value VALUE below.
Look them up here: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CellStyle.html
appliedParagraphStyle : VALUE,
basedOn : VALUE ,
bottomEdgeStrokeColor : VALUE ,
…
verticalJustification : VALUE
*/
}
)
};
Then work on with the rest of your script.
At the point you apply the cell style use the variable name cellStyle instead of the name "Green".
Regards,
Uwe
Copy link to clipboard
Copied
I apologize I am still very new at this. The entire reason for the script is to be able to run it on the attached file, and it automatically fill in the color based on the cell input. are you able to make the script according the comment above with your new script?
Copy link to clipboard
Copied
Hi,
you cannot attach a file to a forum post.
If you want to share it, just use a service like Dropbox and post the link.
Best,
Uwe
Copy link to clipboard
Copied
Hi
Actually I just tested the script no problem, a file will be welcome to help you.
Effectivement je viens de tester le script pas de problème, un fichier serai le bien venu pour vous aidée .
Copy link to clipboard
Copied
below is a link to file I am trying to use. I am trying to turn lets just say the cells with "D" or 1* to a green background. It did work a few days ago but now won't work for some reason. My computer is running High Sierra 10.13.4 , Indesign 13.1
Copy link to clipboard
Copied
it still works but the style is not forced "Green +".
//----
ça fonction toujours mais le style n'est pas forcé "Green+".
myFound.parent.fillColor = "C=75 M=5 Y=100 K=0";
Copy link to clipboard
Copied
where should I put that line of code? Does that replace the cell style line?
Copy link to clipboard
Copied
I do not force the style (I do not know if it's possible on a Cell Style) but place the color.
Attention, there is no control over the existence of the runner (it must be there)
/--------
Je ne force pas le style (je ne sais pas si c'est possible sur un Style de cellule) mais place la couleur.
Attention, il n'y a pas de contrôle sur l'existence de la coureur (elle doit être là)
for(i=0; i<myFound.length; i++) {
if(myFound.parent.constructor.name == "Cell") {
myFound.parent.appliedCellStyle = "Green";
myFound.parent.fillColor = "C=75 M=5 Y=100 K=0";
}
}//
Copy link to clipboard
Copied
Hi Liphou ,
after seeing into the sample document I'd prefer a GREP find to isolate cell contents like "D" vs "DP".
And you can use method clearCellStyleOverrides() on a cell.
Code below is using all this:
app.doScript
(
main,
ScriptLanguage.JAVASCRIPT,
[],
UndoModes.ENTIRE_SCRIPT,
"Apply Cell Style to found Cell | Uwe | SCRIPT"
);
function main()
{
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "^D$";
var myFound = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
for(var i=0; i<myFound.length; i++)
{
if(myFound.parent.constructor.name == "Cell")
{
var cell = myFound.parent;
cell.clearCellStyleOverrides( true );
cell.appliedCellStyle = "Green";
};
};
};
FWIW: The script's action can be undone in one go.
Regards,
Uwe
Copy link to clipboard
Copied
Hi Laubender, and thank you
Actually I noticed that it was more prudent to search in GREP for not locating another "D".
Thank you for the command ".clearCellStyleOverrides (true)" (it will serve me again),
I did not search further, because forced cell style, here, removed the double lines of cell cards.
Very good example of code thank you (I keep)
Effectivement j'avais remarqué qu'il étais plus prudent de faire une recherche en GREP pour ne pas localisé d'autre "D".
Merci pour la commande ".clearCellStyleOverrides( true )" (elle me servira encore),
je n'avais pas recherche plus loin, car forcé le style de cellule, ici, va supprimé les double lignes des carde de cellule.
Très bonne exemple de code merci ( je garde)
Philippe
Copy link to clipboard
Copied
I will try this as soon as I can get back to my computer, thank you and everyone else who has been so extremely helpful. This will save me hours of work if this works.
Copy link to clipboard
Copied
The way that Mac and Windows deal with addressing styles differs slightly. To avoid problems with applying styles, reference the object, don't use a string. So instead of this:
myFound.parent.appliedCellStyle = "Green";
use this:
myFound.parent.appliedCellStyle = app.documents[0].cellStyles.item ("Green");
P.
Copy link to clipboard
Copied
I tried this in Indesign 12.1.0.56 mac 10.10.5 and it works, I will try on CC 2018 when I get home. Thank you again! Wherever you are in the world. I appreciate your help.
Copy link to clipboard
Copied
Am I able to search for numbers with special characters like 1* or 2*
Copy link to clipboard
Copied
You can look for whole numbers using this:
app.findGrepPreferences.findWhat = '\\d+';
To match numbers with decinals and thousands separators, use this:
app.findGrepPreferences.findWhat = '\\d[\\d.,]+';
Copy link to clipboard
Copied
graphicsguy85 wrote
Am I able to search for numbers with special characters like 1* or 2*
Do you mean you are looking for 1* as contents of a cell ?
Then you could do this:
app.findGrepPreferences.findWhat = "^1\\*$";
Regards,
Uwe
Copy link to clipboard
Copied
Tank you again for the help, would you be so kind to assist me a little further?
How would I search for the following: (1)
(G*)
I am having trouble for it searching for the Parentheses
Copy link to clipboard
Copied
Look for '\\(1\\)'
Parentheses, brackets, braces, asterisks, and a clutch of other symbols have a special meaning in Grep, so you need to escape them, as it's called. And in strings you need to escape the backslash because it too is a special character in Grep.
Go on the net and read up on Grep. It's worth it.
P.
Copy link to clipboard
Copied
when it runs it applies the text style to what I want it to but it doesn’t apply the cell style, it basically ignores it