Copy link to clipboard
Copied
Hi, I am a new user to Acrobat Pro and I am trying to figure out if it is possible to have the field color change when calculated value is within a specific range. I have a calculated field box and I want it to turn yellow when it reaches a specific numerical range, and red when it gets to another. A font color change would work too..
The correct code could be:
(function() {
var v = +event.value;
if (v < 16) {
event.target.fillColor = color.green;
return;
}
if (v < 27) {
event.target.fillColor = color.yellow;
return;
}
// Value is greater than 27
event.target.fillColor = color.red;
})();
This line of code in particular isn't valid:
if (16<v>27){
Something like this would be:
if (v >= 16 && v < 27) {
which translated to English is: If the value of the variable v is greater than or equa
...Copy link to clipboard
Copied
Sure it's possible. Are you currently using a custom calculation script or one of the built-in calculation options?
Copy link to clipboard
Copied
Just the built in sum of selected fields.
Copy link to clipboard
Copied
You can use a custom Validation script for the field, something like the following:
// Custom Validate script for text field
(function () {
// Set background color to white if blank
if (!event.value) {
event.target.fillColor = color.white;
return;
}
// Convert value to a number
var v = +event.value;
if (v < 5) {
event.target.fillColor = color.red;
return;
}
if (v < 10) {
event.target.fillColor = color.yellow;
return;
}
// Value is greater than 10
event.target.fillColor = color.green;
})();
Copy link to clipboard
Copied
Thank you!
Copy link to clipboard
Copied
So, here is what I have...It accepts it, but nothing happens.
(function(){
var v = +event.value;
if (v<16){
event.target.fillColor=color.green;
}
if (16<v>27){
event.target.fillColor=color.yellow;
}
//Value is greater than 26
event.target.fillColor=color.red;
})();
Copy link to clipboard
Copied
The correct code could be:
(function() {
var v = +event.value;
if (v < 16) {
event.target.fillColor = color.green;
return;
}
if (v < 27) {
event.target.fillColor = color.yellow;
return;
}
// Value is greater than 27
event.target.fillColor = color.red;
})();
This line of code in particular isn't valid:
if (16<v>27){
Something like this would be:
if (v >= 16 && v < 27) {
which translated to English is: If the value of the variable v is greater than or equal to 16 and less than 27...
So another functionally equivalent script could be:
var v = +event.value;
if (v < 16) {
event.target.fillColor = color.green;
} else if (v >= 16 && v < 27) {
event.target.fillColor = color.yellow;
} else { // Value is greater than or equal to 27
event.target.fillColor = color.red;
}
I'm not sure if it's exactly what you want, but you should be able to revise it to suit.
Copy link to clipboard
Copied
The first code you posted is not equivalent in functionality to your later code. In the former you're missing both the else-command and the more full conditions that are in later code. I would suggest to use the latter.
Copy link to clipboard
Copied
The background color of the field is set the same for the same field value, which is what I mean by functionally equivalent. Note the return statements.
Copy link to clipboard
Copied
Thank you George, woks perfect!
Copy link to clipboard
Copied
My apologies. Didn't notice the return commands...
Copy link to clipboard
Copied
Any idea why the yellow range doesn't work in Adobe Reader? Works perfect in PDF Expert and Acrobat, and when it gets to the red values it works. I am doing a Risk Assessment tool for my pilots and I have Reader as their PDF app.
Copy link to clipboard
Copied
Could be because color.yellow is a CMYK color, while the others are RGB-based. Maybe the Reader app for iOS only supports RGB-colors...
Try replacing color.yellow with this: ["RGB", 1, 1, 0]
Copy link to clipboard
Copied
That worked perfect! Thank you both very much!
Copy link to clipboard
Copied
One last question...is there a way to put a "reset" button on the form that zeros out all the fields, so that my pilot can just hit that to reset the fields and eliminate errors from them forgetting to zero them out themselves? Thanks again!
Copy link to clipboard
Copied
You can add a reset button that clears all of the values, sure, but you'll need to add additional code that changes the fill color of the fields. It would look something like this:
this.resetForm();
this.getField("Text1").fillColor = color.transparent;
this.getField("Text2").fillColor = color.transparent;
this.getField("Text3").fillColor = color.transparent;
etc.
Copy link to clipboard
Copied
Thanks again for your help....sorry I'm such a rookie at this stuff, but it is fun to learn about it.
So, do I go into each field to add the code that changes the fill color, or is there a way to put a code in for the whole project? Where would I go to put the code that you gave me?