Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
3

Custom Calculation Script - Adobe Acrobat

New Here ,
Jun 28, 2023 Jun 28, 2023

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! 

TOPICS
How to , JavaScript , PDF , PDF forms
528
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jun 29, 2023 Jun 29, 2023
LATEST

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2023 Jun 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;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 29, 2023 Jun 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? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2023 Jun 29, 2023
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines