Adding values of checkboxes with acrobat forms and dividing it by 2

Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

So, I am trying to develop a risk assessment calculator. I have almost completed the project, however there is this weird issue I am struggling to deal with. The user is presented a list of circumstances and, if they apply to the experiment they are doing, they check the box. Each box is worth one point and the total at the bottom should sum these up and then divide that sum by 2 and present the value rounded up. I am able to get the sum by just using the simple calculation feature. However, to do the divided by 2 and the rounding up, I have to use javascript calculations. Unfortunately, this doesn't seem to be working and I am not quite sure how to approach this. Any suggestions would be appreciated, thank you. 

TOPICS
How to, JavaScript, PDF forms

Views

379

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Mar 15, 2021 Mar 15, 2021
Your check box name is "probRiskFactor2a"  change a to A.

Likes

Translate

Translate
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Whats name of checkboxes and how many you have?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

There are thirteen checkboxes labelled probRiskFactor1A, probRiskFactor2A, probRiskFactor3A, all the way to probRiskFactor13A. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Remove "A" from field names and use this code :

var total = 0;
for ( var i=1; i<=13; i++){
if(this.getField("probRiskFactor"+i).valueAsString != "Off")total++;
if(total == 0) event.value = "";
else event.value = Math.ceil(total/2);}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Thank you. Is there anything preventing me from changing 

"probRiskFactor"+i

to "probRiskFactor"+i+"A". The reason I have the A is because this thing repeats itself for four different hazard classes. If not I can just keep increasing the numbers but if I could keep the naming system that would be cool. Thank you for the help though, I will try this out. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Sorry about that, yes you can use it like that.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Hm, the script doesn't seem to be working as no value is being output into the textbox. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Where did you put script?

Paste script you currently using here.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

var total = 0;
for ( var i=1; i<=13; i++){
if(this.getField("probRiskFactor"+i+"A").valueAsString != "Off")total++;
if(total == 0) event.value = "";
else event.value = Math.ceil(total/2);}
Here is the script I am using. I put this in "Custom Calculation Script" box under the properties of the textbox. My apologies if I am making some basic error, this is my first time using acrobat to generate a form and my first time with javascript. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

The final 'if' block that sets the value needs to be outside the loop, because the field value is only set once at the end of the calculation

var total = 0;
for ( var i=1; i<=13; i++)
{
     if(this.getField("probRiskFactor"+i+"A").valueAsString != "Off")
         total++;
}
if(total == 0) 
    event.value = "";
else 
    event.value = Math.ceil(total/2);

 

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

I made that change and a value still isn't being output.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

That should work. can you share your file?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

I am afraid I dont know what you mean share the file. I did not write it as an external javascript file and call it in acrobat. I directly pasted the script into the calculation box. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

I mean to upload your PDF file to something like google drive and post link here so I can download it and check it. Here is example file I made, download it and check if it works as you intended.

https://drive.google.com/uc?export=download&id=1CNRti4rlgHZ6zw6UMLOzv-boTIPVExgu 

Also try Thom advice and see if that helps.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

https://drive.google.com/file/d/1uZ6WG5vJVSMr3JKuVQA1W20rU-r4-GJS/view?usp=sharing

Here is the link. I have only done this for the probability risk factors under "chemical hazards". Thank you for taking a look. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Share the PDF file.

 

However, the first thing you need to do is look in the console window to see if there are any errors reported. It is highly likely that the field names in the code are incorrect. 

Press Control-J to display the console window.

You'll find a tutorial on it here:

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

 

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

I think there may be an error in the way I am integrating the script as the debug menu shows nothing. I will take a look through the tutorials you have shared to see if I can figure that out. Thank you for the tip. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

You need to remove restriction from google drive, go to your file in google drive,right click on it, click get link and set allow anyone with link.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Your check box name is "probRiskFactor2a"  change a to A.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

LATEST

Wow, I actually noticed that error earlier, changed it, but must have forgotten to save the change. It is working now, thank you so much for the help. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines