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

Nesa Nurani
Community Expert
Community Expert
January 3, 2024

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