Skip to main content
Participant
June 28, 2023
Answered

Custom Calculation Script - Adobe Acrobat

  • June 28, 2023
  • 1 reply
  • 636 views

Greetings! I’m creating a rating form in Adobe Acrobat that has several fields in each section. Each field has a dropdown menu with the following options: (Choose one, N/A, 1, 2, 3, 4, 5). If I choose N/A or leave it as “Choose one,” Adobe sees it as zero and includes it in the average rating, lowering the overall score. What formula do I create so that only the numbers get averaged out? 

 

Please note: I looked at other similar questions, but suggested formulas did not work for me. Help! 

This topic has been closed for replies.
Correct answer try67

That means some of the values of the fields could not be converted to a number.

Are you sure the value is actually "N/A" and not something else? To avoid such issues change these two lines:

var v = this.getField("Q"+i).valueAsString;
if (v!="Off" && v!="N/A") {

To:

var v = Number(this.getField("Q"+i).valueAsString);
if (!isNaN(v)) {

1 reply

try67
Community Expert
Community Expert
June 29, 2023

Let's say the question fields are called Q1 to Q10. You can then use this code as the custom calculation script of your average field:

 

var total = 0;
var n = 0;
for (var i=1; i<=10; i++) {
	var v = this.getField("Q"+i).valueAsString;
	if (v!="Off" && v!="N/A") {
		n++;
		total+=Number(v);
	}
}
if (n==0) event.value = "";
else event.value = total/n;
J.Doe09Author
Participant
June 29, 2023

This worked! But NaN is now showing up in the area where I would like it blank (where the average is supposed to appear). How do I fix this? 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 29, 2023

That means some of the values of the fields could not be converted to a number.

Are you sure the value is actually "N/A" and not something else? To avoid such issues change these two lines:

var v = this.getField("Q"+i).valueAsString;
if (v!="Off" && v!="N/A") {

To:

var v = Number(this.getField("Q"+i).valueAsString);
if (!isNaN(v)) {