• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

1.8K

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

Community Expert , Mar 15, 2021 Mar 15, 2021

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

Votes

Translate

Translate
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Whats name of checkboxes and how many you have?

Votes

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. 

Votes

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
Community Expert ,
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);}

Votes

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. 

Votes

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
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

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

Votes

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. 

Votes

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
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Where did you put script?

Paste script you currently using here.

Votes

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. 

Votes

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
Community Expert ,
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

Votes

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.

Votes

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
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

That should work. can you share your file?

Votes

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. 

Votes

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
Community Expert ,
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.

Votes

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. 

Votes

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
Community Expert ,
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

Votes

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. 

Votes

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
Community Expert ,
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.

Votes

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

Votes

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
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

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

Votes

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. 

Votes

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