These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Check box calculations...script help

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Answered

Hi, this is what I've got so far.
 
The formula I have puts a value into a box, if a check box is ticked. What I need to do is have it add up multiple values, if several boxes are ticked.
 
if (getField("1stBox").value=="Yes")
event.value = getField("M").value;
else event.value = "0"
 
So I need to say
Also check
"PBox" (a check box)
and add "PValue" (a pre-determined number)
Also check
"JLBox" (a check box)
and add
"JLValue" (a pre-determined number)
and last of all, always add anything typed in the "CCBox" (a variable number)
 
I just don't know to make the multiple if statements add together, and also work so they can run independantly, i.e. it doesn't matter if one box is ticked or not for it to check the others. Hope that makes sense?

My Product Information:
Acrobat Pro 10.1, Windows
maxwyss
Registered: Jul 25 2006
Posts: 255
If the effect of a clicked checkbox always has the same effedt (meaning you add a fixed value, for example), make that value the return value of the checkbox. To embed such a checkbox into a calculation, you would use something like this code snippet:

var mySum ; // define a variable to sum up; not needed, but for clarity of this code sample required
mySum += this.getfield("myCheckBox").value.replace(/Off/gi, "0") *1 ;

and that should do it. We need to do the replace() because the value of an unchecked checkbox is "Off", and to make sure that we have a number, we multiply the value with 1.

Hopte this can help.

Max Wyss.

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Well I wanted to keep it so that the numbers that were added came from seperate boxes, because I may wish to change these numbers from time to time, and I don't want to have to edit the formulas to change the numbers.

If I have a box with a number in it, and the formula is already in place to always grab that number when the box is ticked, I won't need to worry about amending formulae (and for that matter, nor will anyone else), if/when those numbers need to be changed.

I also need the numbers to be displayed, even if the check boxes aren't ticked.

That's why my plan was to create something that only added those numbers in when the check boxes were ticked. My java knowledge is extremely limited though and I don't know much about its syntax... just picking up what I can from reading these boards.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Why not just put each value into variable and check the variable for a value of "Off". When "Off" then change the value to 0.

var 1stBox = this.getField("1stBox").value;
if (1stBox = "Off") 1stBox = 0;
var PBox = this.getField("PBox").value;
if (PBox = "Off") PBox = 0;
var JLBox = this.getField("JLBox").value;
if (JLBox = "Off") JLBox = 0;
var JLvalue = this.getField("JLValue").value;
if (JLValue = "Off") JLValue = 0;
event.value = Number(1stBox) + Number(PBox) + Number(JLBox) + Number(JLValue);

George Kaiser

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Okay that sounds like a great way to do things, I've never used variables before but I like how it looks.

I my understanding of how variables is working is right, I've given the variables different names to the boxes so as not to confuse myself - so I've written "var one" & "var two"... this is more along the lines of what I'm trying to achieve, but I'm getting syntax issues....var one = this.getField("MLSCAnnualCost").value
if (1stMLSCBox="Off") one = 0;
var two = this.getField("JLAnnualCost").value
if (JLBox="Off") two = 0;
event.value = Number(one) + Number(two);

Note I re-named some fields. Here's what is meant to be happening:

var one
there's a box called 1stMLSCBox. When it's checked, I want to copy the value from the field called MLSCAnnualCost.

var two
there's a box called JLBox. When it's checked, I want to copy the value from the field called JLAnnualCost.

I then want all of these values added together, and if the boxes aren't checked, nothing to be added.

It's essential that the fields containing the values (MLSCAnnualCost, JLAnnualCost) are looked up rather than me putting the values into the checkboxes themselves - because I want people to be able to change the value of these boxes without having to edit formulas.

Am I close? :)
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Note that there's an error in the code George provided. To compare two values you must use this operator: "==". If you use a single equals-sign it means "assign the value on the right to the variable or property on the left.

So this line of code:
if (1stBox = "Off") 1stBox = 0;
Should actually be:
if (1stBox == "Off") 1stBox = 0;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Ah okay... this still gives me a syntax error on my proposed new solution:
var one = this.getField("MLSCAnnualCost").value
if (1stMLSCBox == "Off") one = 0;
var two = this.getField("JLAnnualCost").value
if (JLBox == "Off") two = 0;
event.value = Number(one) + Number(two);

try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Which syntax error?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
missing ) after condition 2: at line 3
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Post your entire code, please.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
That's it above, based upon an earlier suggestion -

var one = this.getField("MLSCAnnualCost").value
if (1stMLSCBox == "Off") one = 0;
var two = this.getField("JLAnnualCost").value
if (JLBox == "Off") two = 0;
event.value = Number(one) + Number(two);

If I try to hit OK I get the Syntax Error: missing ) after condition 2: at line 3 .
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Where are you declaring "1stMLSCBox" and "JLBox"? Also, if you want to assign a value to a field, don't use a variable. You need to access the field's value property directly.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Okay I'm just following the suggestions I've had so far.

I'll start again! Perhaps it'll be easier to pickup from scratch.

Here's the critera:

"1MLSCBox" is a checkbox
"JLBox" is a checkbox
"MLSCAnnualCost" is a textbox
"JLAnnualCost" is a textbox

I'm assigning the formula to a box called "Additional"

When the check boxes are checked, I want the associated text box values to be added together and the value returned to the "Additional" box.
So if I tick "1MLSCBox", the value of "MLSCAnnualCost" will be added to "Additional"
If I tick "JLBox", the value of "JLAnnualCost" will be added to "Additional"

In each case if they're not ticked I don't want anything adding in.

It's important that the text boxes remain seperate (rather than the values they contain being exported from the checkboxes) because a) I want to display the values and b) I want any users to be able to edit them without playing wiht the formulas.



try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Sorry, I don't quite follow. So when the user ticks "1MLSCBox" (and "JLBox" is not ticked) the value in "additional" should be equal to the value in "MLSCAnnualCost"? And when the user ticks just "JLBox", the value in "additional" should be equal to the value in "JLAnnualCost"? And when both are ticked the value of "additional" is the sum of both text fields? Is that it?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
That's right.
I have some others boxes to add in but once I have the principle of how this is put together I'll be able to add to it

try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
OK, then use this as the custom calculation code of the "additional" field:

  1. var newValue = 0;
  2. if (this.getField("1MLSCBox").value!="Off")
  3. newValue += Number(this.getField("MLSCAnnualCost").value);
  4. if (this.getField("JLBox").value!="Off")
  5. newValue += Number(this.getField("JLAnnualCost").value);
  6. event.value = newValue;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Chris Lunt
Registered: Aug 22 2011
Posts: 17
Magic, works a treat, many thanks!