Thursday, February 28, 2019

Calculations with Adobe Forms radio buttons

 Calculating Adobe Forms


Its been a long while since I've blogged.  I had an interesting small project land on my desk that required calculating values based on radio buttons in an Adobe form.  Initially, I figured this was a simple task that any office worker with intermediate knowledge could do.  After digging into it farther I discovered Javascript had to be used for anything more than a simple sum, product, average, minimum or maximum. 

The thumbnail picture at the beginning of this blog is just a small part of the form I had to work on.  In this blog post, I will create a 5x4 table with radio buttons.  At the bottom, I will sum the choices based on their value. Then I will find the sum of all the columns and find the mean of the choices.

I started out by creating a basic document in Microsoft Word.
Test PDF form image

After creating the PDF, I added the form fields and radio buttons.  The group names were Question1, Question2, etc.  Each button had the Radio Button Choice set to a unique value for each button group. For instance Question 1, and Question 2, both had 4,3,2,1, and 0.

Test PDF From with form buttons and boxes imageTest PDF Form Fields sidebar image


That value was set by either right-clicking the button and selecting Properties or double-clicking the option under the question and entering the value.
Test PDF Form radio button options image
 After the radio buttons were set, I added simple text boxes for each column. Lastly, I added a Sum box and a Mean box.

Now that the form fields exist on the form, it is time to start adding the calculations.

I started with the first column, the 4s column.  I went into the properties of the Total4s text box. I selected the Calculate tab, chose the "Custom calculation script", and clicked Edit.

This is the way I choose to calculate the values. I am certain there are other ways to accomplish this but this method seemed the most straight forward and scalable option for less tech-savvy users.



The first variable total4s is how we will track the number of buttons selected in the 4 column.  I set it to 0 to make sure we are starting from the same place every time.  Next, I use this.getField("Question1").value to get the value of the button selected.  If the radio button in column 4 on Question 1 is selected, the value will be 4 based on the "Radio Button Choice" field referred to earlier.  In my initial testing, I set the value to a variable. I then had the if statement evaluate based on that variable.  After working to simplify the script, I realized I was able to evaluate the getField value directly.

Next, I entered the same code in for each Total3s, Total2s, Total1s, and Total0s.  I modified the variable total4s to match the text box for each column (Total3s = total3s, Total2s = total2s, etc.).  I also changed the condition value in the if statement to match the column number.

After entering the similar javascript in each text box I can test if I receive the expected value.
If the values aren't as expected, you may need to check the Field Calculation Order. The order is top down.  For instance if a calculation depends on a previous calculation, the dependent calculation should be higher in the list.  In this example, the Total*s must calculate before the sum and lastly the mean.


Now I will choose Preview so I can verify I am getting the expected.


At this point, the value only shows a 1 for each selection.  For my needs, I want each selected column to have the value of that column in the total.  So I would like any buttons in the 4 column to return 4 for their total and add up as buttons are selected.  The way I did this was simply by taking the final line of the code event.value = total4s and multiplying it by the column value. Below is what I changed for the last line in each text box.  For the 0 column text box, I don't have any calculations and set the default value to 0.



The next part is adding up the values. Fortunately, we are able to do this with built-in calculations. From the Properties > Calculate tab, I chose "Value is the" and left the default as "sum (+)". When choosing that field, the "Pick" button becomes active.  I chose Total4s, Total3s, Total2s, and Total1s.  Since Total0s will always be 0, I don't need to include it.


Next, I will find the mean.  For that, I'm able to use another option in the Calculate tap under properties for a text box.  "Simplified field notation" is the perfect solution to this.  I just entered TotalSum/4.


After all that, the final product


No comments:

Post a Comment