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.
Your check box name is "probRiskFactor2a" change a to A.
Copy link to clipboard
Copied
Whats name of checkboxes and how many you have?
Copy link to clipboard
Copied
There are thirteen checkboxes labelled probRiskFactor1A, probRiskFactor2A, probRiskFactor3A, all the way to probRiskFactor13A.
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);}
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.
Copy link to clipboard
Copied
Sorry about that, yes you can use it like that.
Copy link to clipboard
Copied
Hm, the script doesn't seem to be working as no value is being output into the textbox.
Copy link to clipboard
Copied
Where did you put script?
Paste script you currently using here.
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.
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);
Copy link to clipboard
Copied
I made that change and a value still isn't being output.
Copy link to clipboard
Copied
That should work. can you share your file?
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.
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.
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.
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
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.
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.
Copy link to clipboard
Copied
My bad, I forgot it defaults to that. Here you go: https://drive.google.com/file/d/1uZ6WG5vJVSMr3JKuVQA1W20rU-r4-GJS/view?usp=sharing
Copy link to clipboard
Copied
Your check box name is "probRiskFactor2a" change a to A.
Copy link to clipboard
Copied
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.