Skip to main content
jennifer60254621
Participant
April 21, 2017
Answered

Can a calculated field ignore fields that do not currently contain a numeric value?

  • April 21, 2017
  • 1 reply
  • 744 views

I am creating a form in Adobe Acrobat and I need to average a column of numbers. The problem is, not all rows of that column will always have a numeric value but the basic calculation function includes the row in the average anyway.

Here is a simplified version of my table:

CriteriaRating
1.13
1.24
1.3
1.42
1.53

The average of the four rows that contain a value is 3. But the calculate field function counts row 1.3 as a zero, even though no zero is present and generates an average value of 2.4.

I don't know Javascript very well. Is there a custom calculation script that can help me with this?

This topic has been closed for replies.
Correct answer Joel Geraci

Name the fields in the "Rating" column using hierarchical field names like "rating.1","rating.2", "rating.3" etc. then add the following script to the calculation of the average result field.

var aRatings = [];

var nTotal = 0;

var aRatingFields = this.getField("rating").getArray();

for (var i=0; i < aRatingFields.length; i++) {

  var value = aRatingFields.value

  if (value != "") {

  aRatings.push(value);

  nTotal += value;

  }

}

event.value = nTotal/aRatings.length

1 reply

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
April 21, 2017

Name the fields in the "Rating" column using hierarchical field names like "rating.1","rating.2", "rating.3" etc. then add the following script to the calculation of the average result field.

var aRatings = [];

var nTotal = 0;

var aRatingFields = this.getField("rating").getArray();

for (var i=0; i < aRatingFields.length; i++) {

  var value = aRatingFields.value

  if (value != "") {

  aRatings.push(value);

  nTotal += value;

  }

}

event.value = nTotal/aRatings.length

jennifer60254621
Participant
April 21, 2017

Thank you! That worked.

Joel Geraci
Community Expert
Community Expert
April 21, 2017

Great - Please mark my answer as correct.