Skip to main content
Participant
January 2, 2024
Question

Can I write a script to auto populate a text field based upon a numerical value in another field

  • January 2, 2024
  • 1 reply
  • 2256 views

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.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
January 3, 2024

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.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
January 3, 2024

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";

Participant
January 3, 2024

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. 

 

 


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.