Copy link to clipboard
Copied
I am creating an evaluation document, where an average numerical score is translated to a text representation. I can do it in Excel, but I don't know how to do it in any other format.
Basically, if Score = "1", then Rating = "Fair"
I hope that this is easier than I'm making it out to be. Thanks.
Copy link to clipboard
Copied
You can use an "if" statement for this. I'll assume the text value is being displayed in a different field than the number value.
And that the number value field is named "Score", and that you are usinghte built-in average function for this.
Use this script in the Custom Calculation for the field where the text value will be displayed.
var nScore = this.getField("Score");
if(nScore == 1)
event.value = "Fair";
else if (nScore == 2)
event.value = "Good";
else
event.value = "NA";
You can read more about how to write scripts for PDF docs here:
https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm
https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm
https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm
If you want to display the text value in the same field where the calculation is happening, there is also a way to do that.
Copy link to clipboard
Copied
I have attempted to run the script, and it worked once. It isn't working anymore. Acrobat has crashed 3 times in this process as well. I don't think that is helping. Please advise about where my script is off.
Here is the script that I am attempting to run:
var nScStance = this.getField("ScStance");
if(nScStance == 1)
event.value = "Fair";
else if (nScStance == 2)
event.value = "Average";
else if (nScStance == 3)
event.value = "Achieving";
else if (nScStance == 4)
event.value = "Excellent";
else if (nScStance < 1)
event.value = "N/A";
Copy link to clipboard
Copied
There was an error in Thom script, you need to get field value to use it for comparison like this:
var nScStance = Number(this.getField("ScStance").valueAsString);
Copy link to clipboard
Copied
Thanks Nesa for catching missing value propery.
Your script fixed:
var nScStance = this.getField("ScStance").value;
if(nScStance == 1)
event.value = "Fair";
else if (nScStance == 2)
event.value = "Average";
else if (nScStance == 3)
event.value = "Achieving";
else if (nScStance == 4)
event.value = "Excellent";
else if (nScStance < 1)
event.value = "N/A";
The last else indicates that the score is not always an integer. For example, could the score be 2.5? If so, the script will need to be modified. Also, there should always be a default case.
Copy link to clipboard
Copied
Ok, I got that to work, and now I noticed a problem with the ScStance field. It is an average of 7 fields, and I have the format set to only show whole numbers. However, when the score column rounded to 4, the rating column remained at "Achieving" until all fields were at 4. Even 1/7 fields below 4 did not result the "Excellent" rating, as I want it to. Can I do a =< 1.4; >=1.5 to =<2.4; etc...?
I am also noticing that the values won't update appropriately if the scores are modified. Is there a way to catch this as well? I've attached screenshots for reference. I changed the parameters for the Score column to illustrate the rounding issue.
Copy link to clipboard
Copied
You can compare like this >= or <= there is no comparing operator =<.
Even if you formatted the field to show no decimal, the actual field value can still be decimal.
If values won't update appropriately check your field calculation order, select 'Prepare form' tool click on 'More' and select "Set field calculation order" fields that should calculate first should be on top.
Copy link to clipboard
Copied
Since there is no default selection in the "if", the field value will remain unchanged until the score is an actual integer value that is tested for in the "if".
There are two ways to handle this. One way is to use >= and <= operators, the other is to round the value before comparing. Like this:
var nScStance = Math.round(this.getField("ScStance").value);
if(nScStance == 1)
event.value = "Fair";
else if (nScStance == 2)
event.value = "Average";
else if (nScStance == 3)
event.value = "Achieving";
else if (nScStance == 4)
event.value = "Excellent";
else
event.value = "N/A";
Notice that I also added the default selection.