Copy link to clipboard
Copied
I am creating a form (using Acrobat Pro DC) that has multiple checkboxes set up along an X and Y access, mapping to a matrix of values. The person MUST select one checkbox in Row 1, Row 2, Row 3, Row 4. They then MUST select one check box in Column 1, Column 2 or Column 3. I want to set up a custom calculation script that "sees" the selection combination (e.g. Row X with Column X) and auto-fills with value in that intersection location in the matrix. The values in the matrix are fixed. I really don't know Javascript, but I've had some success using it by adapting other Community post suggestions to create custom calculations...but for this particular situation I have not been able to find anything close enough to successfully adapt. I have attached a simple mockup for easier visualization of the situation. Any help would be greatly appreciated!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
There can be different valid ways to get this to work with Acrobat JavaScript
But in my case, I would run a conditional custom calculation script from each of the fields that you want to populate with a pre-defined value, not from the checkboxes.
For example, format these fields as "Number" with currency $ symbol using the field properties "Format" tab--->>>"Number". Then add a conditional script on each fields that will populate a value only if the checked state of both of the corresponding checkboxes are not in the "Off" state. Using this as a rule of thumb for your form is a lot easier than other methods in my opinion.
See how I renamed the fields in my slide using your PDF template as example:
Following up with the field names used in my example then you can use something like the script below for the corresponding fields under each of the Insurance columns :
//custom calulcation script for the fields under column Insurance 1
var single = this.getField("Single").value;
var person = this.getField("Two Person").value;
var family = this.getField("Family").value;
var ins1 = this.getField("Insurance 1").value;
var ins2 = this.getField("Insurance 2").value;
var ins3 = this.getField("Insurance 3").value;
// for field "value1"
if (single && ins1 !=="Off") event.value = 10;
else event.value = "";
//for field value2
if (person && ins1 !=="Off") event.value = 10;
else event.value = "";
//for field value3
if (family && ins1 !=="Off") event.value = 10;
else event.value = "";
//custom calulcation script for the fields under column Insurance 2
var single = this.getField("Single").value;
var person = this.getField("Two Person").value;
var family = this.getField("Family").value;
var ins1 = this.getField("Insurance 1").value;
var ins2 = this.getField("Insurance 2").value;
var ins3 = this.getField("Insurance 3").value;
// for field "value4"
if (single && ins2 !=="Off") event.value = 10;
else event.value = "";
//for field value5
if (person && ins2 !=="Off") event.value = 10;
else event.value = "";
//for field value6
if (family && ins2 !=="Off") event.value = 10;
else event.value = "";
//custom calulcation script for the fields under column Insurance 3
var single = this.getField("Single").value;
var person = this.getField("Two Person").value;
var family = this.getField("Family").value;
var ins1 = this.getField("Insurance 1").value;
var ins2 = this.getField("Insurance 2").value;
var ins3 = this.getField("Insurance 3").value;
// for field "value7"
if (single && ins3 !=="Off") event.value = 10;
else event.value = "";
//for field value8
if (person && ins3 !=="Off") event.value = 10;
else event.value = "";
//for field value9
if (family && ins3 !=="Off") event.value = 10;
else event.value = "";
Copy link to clipboard
Copied
A side-note: It doesn't make sense to have multiple, independent of each other if-else statements that apply a value to the same location. Since each one covers the entire range of possible "truth conditions" it means that only the last one matters. The rest of them will have no effect on the result whatsoever. Think about it like this:
Let's calculate X.
If the sky is blue X is 5, otherwise 10.
If the ocean is dry X is 6, otherwise 10.
If the desert is full of sand X is 2, otherwise 10.
At the end of this process, what's the value of X? And does it matter if the sky is blue or the ocean is dry?
No, because the last condition overwrites all previous conditions and so will determine the value of X, no matter what, so it can only be 2 or 10. I hope that makes sense...
To prevent this from happening you should combine them to a single if-else if-else if-...-else statement (If the sky is blue X is 5, else if the ocean is dry X is 6, etc., else X is 10), OR set a default value up front and then only use if statements later on, without an else clause (X is 10. If the sky is blue X is 5. If the ocean is dry X is 6, etc.).
Copy link to clipboard
Copied
[Damn, I see now that each condition is for a different field... You can disregard all of the above, it doesn't apply. My apologies.]
Copy link to clipboard
Copied
No biggie, on the contrary your guidance is always very valuable.
Yes, I declared the same variables on each field but I used a simple condition just to check for the "Off" value (unchecked state) of the corresponding checkboxes that intersect with each field.
If this was a large PDF, like the ones I am currently working with, then I would've probably done it entirely different, but this is a very small table.
Copy link to clipboard
Copied
I also didn't organize my variables appropriately, there was no need to put them all together on each calulating field and I also did massive mistakes by copying and pasting the script many times. I didn't fix the fieldnames in the script.
I should've posted the code like this without decalring variables:
//custom calulcation script for the fields under column Insurance 1
// for field "value1"
if this.getField("Single").value && this.getField("Insurance 1").value !=="Off") event.value = 10;
else event.value = "";
//for field value2
if ( this.getField("Two Person").value && this.getField("Insurance 1").value !=="Off") event.value = 10;
else event.value = "";
//for field value3
if (this.getField("Family").value && this.getField("Insurance 1").value !=="Off") event.value = 10;
else event.value = "";
//custom calulcation script for the fields under column Insurance 2
// for field "value4"
if (this.getField("Single").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
else event.value = "";
//for field value5
if ( this.getField("Two Person").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
else event.value = "";
//for field value6
if (this.getField("Family").value && this.getField("Insurance 2").value !=="Off") event.value = 10;
else event.value = "";
//custom calulcation script for the fields under column Insurance 3
// for field "value7"
if ( this.getField("Single").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
else event.value = "";
//for field value8
if ( this.getField("Two Person").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
else event.value = "";
//for field value9
if (this.getField("Family").value && this.getField("Insurance 3").value !=="Off") event.value = 10;
else event.value = "";
Copy link to clipboard
Copied
Thank you both -- I am very, very appreciative of your guidance. I have a snag:
I used the above code to create a field at the bottom of each insurance column which would show the value of the checkbox combination selected. A problem has cropped up: no matter what combination of checkboxes I use (for example in Column 1: Single + Insurance 1 OR 2 Person + Insurance 1 OR Family + Insurance 1), the only value that returns is the last value in the string (i.e. Family + Insurance 1). This happens for a three columns. Further, I noticed that if I only check the box underneath the column, it returns the last value....in other words, it doesn't seem to even be paying attention to the Single, 2 Person or Family checkboxes). It only looks at whether the box underneath the column is checked. I did not retype -- I simply copied and pasted the code suggestion. Any ideas?
Copy link to clipboard
Copied
I should also add that I put all three of the column code scripts together into a single calculation field. For example, in the Column 1 field, this is what is in the custom calculation box (resulting in my above issue) - the values have been updated to reflect the numbers I'm actually working with:
if (this.getField("Single").value && this.getField("Insurance 1").value !=="Off") event.value = 23.09;
else event.value = "";
if (this.getField("Two Person").value && this.getField("Insurance 1").value !=="Off") event.value = 124.70;
else event.value = "";
if (this.getField("Family").value && this.getField("Insurance 1").value !=="Off") event.value = 199.17;
else event.value = "";
Copy link to clipboard
Copied
Well, at least the long rumbling text I wrote before was not for nothing, as you're doing now exactly what I explained you shouldn't do... Drop these lines from the code:
else event.value = "";
And instead add this to the top of it:
event.value = "";
Copy link to clipboard
Copied
Thanks for the quick reply. I am still only getting the last value (199.17). Here is the modified text based on what I believe you meant:
event.value="";
if (this.getField("Single").value && this.getField("Insurance 1").value !=="Off") event.value = 23.09;
if (this.getField("Two Person").value && this.getField("Insurance 1").value !=="Off") event.value = 124.70;
if (this.getField("Family").value && this.getField("Insurance 1").value !=="Off") event.value = 199.17;
Copy link to clipboard
Copied
What values are selected in one of the fields?
Copy link to clipboard
Copied
We might have a nomenclature issue here, but I believe what you are asking me is what combination of checkboxes is selected to return that value of 199.17? If so, the only combination that *should* return that value is Family + Insurance 1. However, it doesn't matter what combination is selected -- so long as there is a checkbox under Column 1 (indicating a selection of Insurance 1), the field with the custom calculation shows $199.17. (The code doesn't appear to recognize any of the row checkboxes - Single, Two Person, Family)
Copy link to clipboard
Copied
Can you share the actual file with us?
Copy link to clipboard
Copied
Based on the form that was sent to me by NesaNurani below I do not believe I did a very good job of conveying what I was trying to do. I have attached a stripped down version of my form for your reference. I want the field labeled "Medical/Dental/Vision Per Pay contribution" to show the value indicated by the checkbox selections. I do not need the values in the matrix to be calculated or filled in. Those are fixed.
This is the actual excerpt from the form, using the actual amounts and labeling.
https://drive.google.com/file/d/1bK9DBOFSJ6y4V8WY4S8LqZduMO209Rmb/view?usp=sharing
Copy link to clipboard
Copied
Thank you for all your help with this. I am very, very grateful!
Copy link to clipboard
Copied
Hi, I made example file can you see if thats what you looking for? Code is in first text field custom calculation script.
https://drive.google.com/uc?export=download&id=11m-Q-ZsctzUovS192lvYsg3NEFYWo8Oq
Copy link to clipboard
Copied
Thank for preparing this...I have pasted a sample in the thread of try67.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Yayyyyyyyyy! That did the trick. Thank you immensely.

