19 Replies Latest reply on Aug 30, 2013 7:54 AM by M.Su

# Calculation Script

I have a field where a percentage needs to be calculated. Ideally, when the user enters a value, the entered number is divided by 30 and multiplied by 20. This is the code I've put into the custom calculation script:

event.value = (event.value/30)*20;

event.value = util.printf("%.2f", event.value).replace(/\.?[0]+\$/, "");

Is there any way to:

- Set an upper limit on the value that can be entered by the user? (Max of 30)

- Append a percentage sign (%) to the calculated value? (The calculated value is a percent)

- Leave the field blank instead of a zero if the user has not entered a value?

Also, a problem I have with this script is that the value is being repeatedly calculated. I have multiple fields that all need to be calculated this way, thus the same script is in each of the fields. When a value is entered into the first field, it is calculated correctly. Upon entering a value into the next field however, the calculated value of the first field has been recalculated. Any ideas as to why this is happening and how it can be fixed?

My sincere thanks for any help.

• ###### 1. Re: Calculation Script

Let's start from the last issue, as that's the most serious one. There is a basic problem with the way you're doing it. The Calculation script of a field executes each time any field is edited. This means that when you're editing Field2, the value of Field1 is (re-)calculated. Because of the way you wrote the code, it means that each time that happens you're multiplying that value by 2/3... It's not a very good approach to edit the value entered by the user (or the current value) directly in the field. A much better apporach is to have one field for data entry, and another field to show the calculated value (the second field can be read-only, so the user can't edit it directly).

Then you will not encounter this issue as the result of each calculation will be the same.

• ###### 2. Re: Calculation Script

Is there another script altogether or any way to edit the script so that the user's value can be directly edited without recalculating? I would prefer it, because I have space limitations and would rather not have two different fields for data entry and calculation.

• ###### 3. Re: Calculation Script

I think it should be possible, but you need to use the event.source property to check which field originated the calculation command, and then only apply it if it was the same field.

• ###### 4. Re: Calculation Script

I'm rather new with Acrobat/Javascript. Would you mind explaining exactly how that would be done? Thank you so much.

• ###### 5. Re: Calculation Script

I haven't tested it, but you can try something like this:

if (event.source!=null && event.source.name==event.target.name) {

// put the rest of your code here

}

• ###### 6. Re: Calculation Script

That solved it, thank you! Do you know of any ways to fix the other issues?

• ###### 7. Re: Calculation Script

Do you want an upper limit on the value the user enters, or on the final value?

For the other issues, use this:

if (event.value==0) {event.value = "";}

else {

event.value = (event.value/30)*20;

event.value = util.printf("%.2f", event.value).replace(/\.?[0]+\$/, "");

event.value = "\$" + event.value;

}

• ###### 8. Re: Calculation Script

An upper limit on the value the user enters.

When clicking into the field, there is automatically a zero that needs to be replaced when entering the value. Are there any means of arranging it so that the user can simply input the desired value without having to

first delete the zero?

• ###### 9. Re: Calculation Script

For the upper limit use the custom Validation script:

event.rc = true;

if (Number(event.value)>100) {

app.alert("The value must not exceed 100.");

event.rc = false;

}

To remove the default value from the field use this as the OnFocus script:

if (event.target.value==event.target.defaultValue) event.target.value = "";

• ###### 10. Re: Calculation Script

Thanks again!

This is more of a separate matter, but I'm wondering if you would be able to help me out. I have a multistate object, where upon clicking on it, the display text cycles between "blank", "M", and "F".

This is the document level script:

// Array for Button Text and Text Colour

var aButtonText = [];

aButtonText.push(["", color.black]);

aButtonText.push(["M", color.black]);

aButtonText.push(["F", color.black]);

var setNextButtonVal = function (f_name, ap_array) {

// Retrieve value of text box associated with multistate button

var f = getField(f_name);

var val = f.value;

// Increment the value by one

val += 1;

// Return to beginning if value cannot be located

if (typeof ap_array[val] === "undefined") {

val = 0;

}

// Update text field value to trigger validation script

f.value = val;

}

var setNewButtonText = function (b_name, caption_array, val) {

// Set button text as indicated in array

var f = getField(b_name);

f.buttonSetCaption(caption_array[val][0]);

f.textColor = caption_array[val][1];

}

There are two components to it: a button and a hidden text field. The way I was taught to use this required having both. Do you happen to know of any way to achieve the same effect but without having to use a text field?

The Button Script:

setNextButtonVal("text3", aButtonText);

The Text Field Script:

setNewButtonText("button3", aButtonText, event.value);

• ###### 11. Re: Calculation Script

This might be more suitable for a new thread... But I don't really see what's the problem. Where and for what are you using the text field?

• ###### 12. Re: Calculation Script

This technique is used to fake a multistate check box type field. The button provides the appearance and user interface and the text field provides the persistent value that can be exported/imported and you can get/set using the usual means. If you don't need the value to persist when the document is saved, you can instead store it in a variable (or just use the button text) that gets initiated when the document is opened. The code above relies on the text field's Validate event triggering the code that sets the button text, so you'll have to do something else.

• ###### 13. Re: Calculation Script

There isn't any problem with it. It does work fine, but because I have several hundred of them, I'm curious as to whether there is an alternative method to accomplish the same thing. The value does need to be persist though (the document needs to be saved/printed), so if it's not impossible, do you have any suggestions?

• ###### 14. Re: Calculation Script

There are several possible solutions.

1. Simplify the code. Is the formatting needed? Can you use the standard formatting?

2. Create a document level function to perform the calculation.

3. Turn off the auto calculation for the first field and turn it back on in the last calculated field.

4. Create on calculation to calculate all the fields on the form.

You can set validation requirements using the "Validation" tab.

You can use the "Percentage" formatting to add the percentage sign and retain the numeric formatting of the field with a value that can be used in a calculation without any adjustments.

Suppressing zero becomes more trickier because of the asynchronous nature of Acrobat JavaScript

Formatting Numbers: Problems, Bugs, Thoughts, and Solutions

• ###### 15. Re: Calculation Script

Thanks GKaiseril. I should really be starting a new thread, but I am looking for a way to simplify the multistate, not the calculation script.

• ###### 16. Re: Calculation Script

I've inputted various versions of this script for different calculations in my form.

if (event.source!=null && event.source.name==event.target.name) {

if (event.value==0) {event.value = "";}

else {

event.value = (event.value/30)*20;

event.value = util.printf("%.2f", event.value).replace(/\.?[0]+\$/, "");

event.value = event.value + "%";}

}

Individually, they evaluate correctly. However, I have a sum field that is meant to find the total sum of the calculated values and it seems to be having difficulty evaluating.

By difficulty, I mean that the sum it generates is inaccurate, as though it is adding values that are different from the final calculated values.

I currently have a simplified field notation calculation to determine the sum and I've a feeling that I should be using some manner of custom calculation.

Any thoughts?

• ###### 17. Re: Calculation Script

Sounds like a problem with the field calculation order.

• ###### 18. Re: Calculation Script

if you wish, I may have a look at your form

send it over here: queenscaesar@gmail.com

• ###### 19. Re: Calculation Script

Thanks for the offer. It was the field calculation order