Skip to main content
Participating Frequently
March 23, 2020
Question

Calculations in Adobe Acrobat

  • March 23, 2020
  • 3 replies
  • 2417 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
Community Expert
Community 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
Community Expert
Community 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
Community Expert
Community 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.

 

 

Thom Parker
Community Expert
Community Expert
March 24, 2020

The script provided by rbls is correct. But just to be sure you need to look in the Console window to see if any errors are reported. Here's a tutorial on the Console.

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

The problem is likely in the actual field value. The value of a dropdown is the export value of the entry.  Since you stated that "N/A" has no value for the calculation, you must have set the export value to something other than "N/A"?  If you want to count these selections, then you'll need another way to do it. 

I'd suggest using the "field.currentValueIndices" property. This will give you the index of the selected item. If N/A is always on top, it will always be 0, or you could also use this property to get the display value for the selected entry.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often