Skip to main content
Participating Frequently
March 23, 2020
Question

Calculations in Adobe Acrobat

  • March 23, 2020
  • 3 replies
  • 2409 views

I am trying to calculate the number of times N/A is selected from a drop down menu on each page to a total field on a form in Adobe Acrobat Pro DC. The current drop down options are 0, 1 and N/A. On one page, there is 1 column of down downs and I have the selection appearing in the total column to tally. The next page is 3 columns of 7 rows which total from left to write to a column to total the selection.

This topic has been closed for replies.

3 replies

JR Boulay
Adobe Expert
March 27, 2020

Is it "N/A" or "N / A"?

Can you share (part of) this form?

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Adobe Expert
March 24, 2020

Place this script as a calculation script in the field that display the result:

 

var total = 0;
for (var i = 0; i < this.numFields; i++) {
var oFld = this.getField(this.getNthFieldName(i));
if ((oFld.type == "combobox") && (oFld.valueAsString == "N/A")) {total++;}
}

event.value = total;

 

Acrobate du PDF, InDesigner et Photoshopographe
HunniesAuthor
Participating Frequently
March 24, 2020

Thank you!  I initially received an error message and had to add ")" on line 4 however, I must have N/A still does not tally.  I even deleted the old total field that displays the results and created a new one and added the code.  I honestly don't know what else to do at this point. 

 

I do truly appreciate everyone for helping.

ls_rbls
Adobe Expert
March 23, 2020

Hi,
You need to use a "for loop" operation.


For this to work you need to make sure that allof the dropdown field names are prefixed; the prefixed part of the dropdown fieldname is used to perform the looping function correctly.


In your case, lets say that you have 3 columns of seven rows each, and each dropdown fieldname will be prefixed as "dropdown1" for the first 7 rows , "dropdown2" second set of rows, and "dropdown3 for the third set of rows.


The line of javascript code below is an example of how it may be used to calculate total number of dropdowns with "N/A" as the selected item.:

 

NOTE: I have not tested this successfully on my end but you can give it a try.

 

var v = "N/A"; 
var total = 0; 
for (var i=1; i<=7; i++) { 
if (this.getField("Dropdown1"+i).valueAsString==v) total++; 

} 
event.value = total;

 

HunniesAuthor
Participating Frequently
March 24, 2020

Thank you for helping me!  I've tried the code and it doesn't add the N/A's.  Just in case I'm not explaining it correctly.  Although N/A has no value in the total field, nor does 0 of course, near the bottom, I need to total the times N/A is selected from the drop downs.  The JavaScript code provided, I placed in properties for "Total N/A in 618" field in Custom Calculation Script.

 

 

HunniesAuthor
Participating Frequently
March 24, 2020

In the script that I posted earlier, see this part:

 

 

if (this.getField("Dropdown1"+i).valueAsString==v) total++; 

 

 

I am using "valueAsString" not .value.

 

The loop function above is executed to count how many times the string value of "N/A" appears on each dropdown menu.

 

It is my understanding (due to personal experience figuring this one out in one of my forms) that you can't assign an export value to each listed item in your dropdowns or it won't work.

 

If you're going to assign export values, then that changes the whole intent of what you're trying to calculate since the javascript will be dealing wiht just that, export values; which is not the same as counting how many times a specific text string was selected from the dropdowns.

 

Here is an example that I applied to one of my PDFs with the help of another Adobe forum developer that, just like Thom Parker, I follow his guidance religiously:

 

 

var v = "stuff";  
var total = 0;  
for (var i=1; i<=15; i++) {  
    if (this.getField("stuff"+i).valueAsString==v) total++;  
 
}  
event.value = (((total)*(this.getField("HEADCOUNT").value))/12);

 

 

As you can see, I am using the same loop operation to get how many times the item "stuff" was selected in a total of 15 dropdown menus; each dropdown row was a day consisting of 3 meals (breakfast, lunch, and dinner).

 

However, "stuff" in  my example above represents a box with 12 consumable meal items inside.

 

So I needed to know how many boxes  I needed to order each day  taking into account how much people (HEADCOUNT field) would eat  breakfast, lunch and dinner each day.

 

So in my event.value I came up with this last line: 

 

 

event.value = (((total)*(this.getField("HEADCOUNT").value))/12);

 

 

 

which totals how many boxes I needed to order.

 

In summary, when I assigned export values in my dropdowns, this code would simply didn't work at all.

 

My point is that you don't need to assign an export value if you manipulate the formula of what you're trying to calculate for the intended  event value.


I actually did not add an export value for N/A or any of the other fields because I was afraid that once I write the script, it would read that exported value.  The codes won't display value.  I did test the field by adding the sum of a few other fields in the total column and value populated but I'm not updating something correctly with the code(s) provided.  I checked to make sure I am writing the field name exactly the same as it's displayed also.