Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you for reviewing. With the three options of 0, 1 or N/A, I cannot add the export value of 1 for N/A otherwise when selecting N/A, the total field would be incorrect. I have to leave 1 as 1, 0 as 0 and N/A would technically be 0 as the export value within the drop down as well. Yet, I need to be able to tally the number of times someone selects N/A.
Copy link to clipboard
Copied
***Ok, so what exactly, at this time, is the export value of the "N/A" entry?
***Did you check the console window for errors?
I provided an alternate methodology for getting the selection, i.e., "field.currentValueIndices".
Here's the reference entry for this property:
Copy link to clipboard
Copied
The console window does not yield an error. The actual export value of N/A is 0 throughout the form however I need to tally the amount of times N/A is selected. I was able to count the N/A's in Excel but need help in Adobe. For Excel, I've used "=COUNTIF(B4:C22,"N/A")" to calculate on Page 2 (not displayed above) and "=COUNTIF(B29:D35,"N/A") *1.14285714" to calculate N/A for Page 3 displayed above.
Let me try your alternate method.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You stated above that the "N/A" entry had an export value, now you are saying it doesn't? If that's the case then the original code posted by rbls would work.
Let's do a test to see what's really going on.
Follow these steps.
1) Select "N/A" on "DropDown1".
2) Type this code into the console window
this.getField("DropDown1").valueAsString
3) Run it
4) Tell us what is returned
Also, I just found an error in the original code from rlbs.
Here's the correction, run it from the console window and let us know the result.
var v = "N/A";
var total = 0;
for (var i=1; i<=7; i++) {
if (this.getField("Dropdown"+i).valueAsString==v) total++;
}
Copy link to clipboard
Copied
Thom,
Yes the problem seems to be on how the user is renaming the fields and possibly not ticking "Commit selected value immediately" under the "Options" tab of each dropdown field..
I tried recreating the problem by creating mulitple dropdown menus using: right-click on the dropdown field---> selecting "Properties" from the context menu--->>"Create Multiple Copies...".
When I use this option the wizard will assign field names as Dropdown.0, Dropdown.1, Dropdown.2, Dropdown.3, etc.
This seems to break the script. So when you check in the console for errors it throws the infamous error :
TypeError: this.getField(...) is null
An easy fix for me was to rename the fields by prefixing with a letter, or like in your example, using a full word such as "dropdown" but immediately followed by a number instead of a period (.).
See image below:
Then adjusted the custom calc script as:
var v = "N/A";
var total = 0;
for (var i=1; i<=7; i++) {
if (this.getField("m"+i).valueAsString==v) total++;
}
event.value = total;
Copy link to clipboard
Copied
I thought about the "." when using the "Create Multiple Copies" and changed the names of all those fields yesterday before I started the post. I still tried your code and removed the "0" behind Timely and still does not calculate.
Copy link to clipboard
Copied
Yes I was reading about it in a forum outside Adobe.
Looks like that with newer versions of Acrobat, also a few things have changed in javascript.
In some cases I had to create a reset button to clear the form, for example, after changing the field names, save the file, and close it, then open it again; else it wouldn't work.
Copy link to clipboard
Copied
I have tried saving and closing the file then reopening and still no value output counting N/A's. 😞
Copy link to clipboard
Copied
I tried saving, closing and reopening and there is still no output value for calculating N/A's.
Copy link to clipboard
Copied
Please do this test, it will tell us how to write the code for your loop.
Run this line of code in the console. Do it once for each item in the dropdown.
Let us know the results.
this.getField("Timely0").valueAsString
Copy link to clipboard
Copied
I forgot to ads to this conversation that in my screenshot above, I had to create a blank PDF from scratch to rule out if the issue had anything to do with calculation order interfering with all of the other calculated fields in the form.
Have you also checked tje calculating order of your fields?
Aside from that, I've also noticed that, while creating new dropdown fields using the "Create multiple copies", it keeps a numbered sequence of all other dropdowns.
For example, if the lastknown dropdown in the Acrobat PDF was "Dropdown.16" and you try to create more copies of dropdowns using Dropdown.16, it will rename this field and all new fields to something like Dropdown.16.1.1.
If you don't keep an eye on this you'll end up with a daisy-chained mess of dropdown fieldnames.
Since I really don't know why this happens, what worked for me was the long method: create a new dropdown field from scratch, then create multiple copies, and last, rename each field to what it needs to work with the script.
Copy link to clipboard
Copied
Can you share the PDF with us?
I think the issue would be reolved by deleting all those dropdown and creating from scratch.
That's the only way I was able to get it to work on my end.
Copy link to clipboard
Copied
In order to figure out what's not working, we need to nail down some information.
Please run this code in the console window and let us know the result.
Run it after selecting each of the dropdown items.
this.getField("Timely0").value;
This will tell us the exact value returned by the N/A selection, and thus the value that needs to be tested for the count.
Copy link to clipboard
Copied
HunnTee stated there were no errors reported in then console.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Is it "N/A" or "N / A"?
Can you share (part of) this form?