Skip to main content
Monteigo99
Inspiring
Jun 29, 2016 5:40 PM UTC
Answered

Script for counting both Yes and No's in a column and returning the # of Yes's out of the Total

(Acrobat Pro)

I have 5 fields (DRDropdown1-5) in which have 3 options (Y, N, NA). I only
want to count the number of Y's and N's and then return the # of Y's out of the
total Y's+N's.

Any assistance is appreciated. I figure if I see enough code, that I'll pick
it up.

Thanks

This topic is closed to new replies. Start a new post to keep the conversation going.
Karl Heinz  Kremer
Karl Heinz KremerCorrect answer
Community Expert
Jun 29, 2016 8:18 PM UTC

We don't usually present complete solutions, given a description or an example of how to do the hard part, you should be able to take your JavaScript knowledge and add the missing pieces. If you are not that familiar with JavaScript, do yourself a favor and look at some introductory material (I like this for JavaScript in Acrobat: Beginning JavaScript for Adobe Acrobat ).

Looking at your script, you already have most of the functionality in place. You should be able to change the output (assuming that your field can take a string and is not just a numeric field), by changing just the last line:

Change "event.value = totalY / total;" to the following:

event.value = totalY + " of " + total;

For easier readability, you may want to reformat the last two lines to this:

if (total==0)

    event.value = ""; 

else

    event.value = totalY + " of " + total;

4 replies

Karl Heinz  Kremer
Community Expert
Community Expert
Jun 29, 2016 7:17 PM UTC

Take a look at this blog post I did a while ago, which explains how to do that: How to Count Radio Button Choices in Acrobat JavaScript - KHKonsulting LLC

Translate
Monteigo99
Inspiring
Jun 29, 2016 7:48 PM UTC

Thank you Karl,


Unfortunately, that doesn't work.

1) it only accounts for Y; I need Y & N total

2) I have 20 fields with the same name with 1-20 tacted on. But I only need 1-5, then 6-10, etc. So your group function doesn't work effectively

3) the final output is a single #. I need '# of #'; as in '3 of 4'.

This is the script that I used to produce a %. Can this be modified to produce '# of #'?

var totalY = 0; 

var total = 0; 

for (var i=1; i<=5; i++) { 

    var f = this.getField("CRDropdown"+i); 

    if (f.valueAsString=="Y") { 

        totalY++; 

        total++; 

    } else if (f.valueAsString=="N") { 

        total++; 

    } 

} 

if (total==0) event.value = ""; 

else event.value = totalY / total;

Translate
Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
Jun 29, 2016 8:18 PM UTC

We don't usually present complete solutions, given a description or an example of how to do the hard part, you should be able to take your JavaScript knowledge and add the missing pieces. If you are not that familiar with JavaScript, do yourself a favor and look at some introductory material (I like this for JavaScript in Acrobat: Beginning JavaScript for Adobe Acrobat ).

Looking at your script, you already have most of the functionality in place. You should be able to change the output (assuming that your field can take a string and is not just a numeric field), by changing just the last line:

Change "event.value = totalY / total;" to the following:

event.value = totalY + " of " + total;

For easier readability, you may want to reformat the last two lines to this:

if (total==0)

    event.value = ""; 

else

    event.value = totalY + " of " + total;

Translate