Copy link to clipboard
Copied
My Adobe Acrobat was going slow due to the calculations I had. So I moved the calculations into a Document JavaScript. Below is one example of 20 calculation functions. The I go to the Total01 text Field and select run custom Validation Script. In there I have Total01(); However it does not work what am I missing?
function Total01()
{
// Get the field values, as numbers
var v1 = this.getField("Cost01").valueAsString;
var v2 = this.getField("Trips01").valueAsString;
// Multiply the values
var result = v1 * v2;
// Set the value of this field, rounding to two decimal places
Total01=result;
};
Copy link to clipboard
Copied
1. It's not safe to use "this" in a function, because the meaning of "this" changes according to where it is run. Pass it as a parameter.
2. I think you are expecting
Total01=result;
to do something other than set a variable called Total01. Like set a field? Compare with what you do in the former calculation, before you made a function.
3. Why use valueAsString when you want a number?
Copy link to clipboard
Copied
I changed the valueAsString to value as I realized my mistake on that one.
Copy link to clipboard
Copied
PS: moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations.
Copy link to clipboard
Copied
Thanks for the information. I am not a JavaScript person, so I am unsure how to pass it as a parameter. I pieced milled this together by looking around online and testing.
My problem was with all the calculations. The form was sluggish when filling it out. I read that moving it to a Document JavaScript would speed it up each time you change a field. It refreshes them all no matter what.
I only want to calculate if there is a change in the fields; otherwise, leave it at $0.00
Can you point me in a direction on how to do this? I have been Googling for 5 hours now to no avail.
Copy link to clipboard
Copied
Scripts at validation makes only sense on fields where users enter data.
Copy link to clipboard
Copied
I only want it to calculate when a user changes the field.
Attached is a screenshot of where 90% of the calculations are occurring.
For each Total and SubTotal field, I had a Calculate field with Value is the product (x) with fields from their respective row as the input.
This worked fine. The problem when you select the next field it takes upwards of 10 seconds for the form to become responsive.
Copy link to clipboard
Copied
What field does the user change?
Copy link to clipboard
Copied
The user can change any of the miles, cost, days, trips, hours. Then the Total field is read only calculated
Copy link to clipboard
Copied
When you want use validation scripts you must use scripts at all this fields.
Copy link to clipboard
Copied
Change the last line to:
event.value = result;
Also, just moving the calculation scripts from the fields themselves to a doc-level function will not change the speed in which they run.
Copy link to clipboard
Copied
Just to make sure I wasn't going down a rabbit hole.
If you start off at Airfare/Train (Tickets/Exchanges/Agent Fees) enter in a cost amount then press tab. You will see it takes a second to calculate. When more data is entered in the fields the responsivness gradually slows. Then adding in digital signatures adds 5 seconds.
I thought that if I moved the calculations into a Document JavaScript then put the function into the valdiation it would only run when there is a change. I might be misunderstanding how validation works. I basically only want to calculate with a change.event.
Copy link to clipboard
Copied
Works fine for me.
Copy link to clipboard
Copied
Ok then. For me it goes slow. I apprecaite you testing it.
Copy link to clipboard
Copied
Sorry. It is still moving slow I had 4 others test it and the report that when the press tab the see an hour glass with appears to be a keyobard behind it. It takes 4 seconds for it to go away.
Copy link to clipboard
Copied
Yes, it is slow.
Copy link to clipboard
Copied
Thank You I am currently looking at this thread as an option
https://answers.acrobatusers.com/stop-calculation-user-data-entered-textfield-q293858.aspx
Copy link to clipboard
Copied
The calculation is fast with the unsupported version XI.
Copy link to clipboard
Copied
After year and revisting this I was able to solve the problem of the constant recalcuation.
For the Total filed I went to custom calculation and added the following
// Get value of fields as a number
var n1 = +getField("Cost01").value;
var n2 = +getField("Days01").value;
var n3 = +getField("Trips01").value;
// Set this field's value
event.value = (n1 * n2) * n3;
Copy link to clipboard
Copied
"moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations."
Since each user event triggers all calculations in all calculated fields one after the other, the killer trick is to use a single trigger in only one field that does all the calculations, so that it really goes faster.
(Thanks to Thom Parker for this useful tip!)
Copy link to clipboard
Copied
Yeah I thought I had this working but it slowed back down.
I am now trying the below to test the speed. Not sure how to do a single trigger like you mentioned
var v1 = this.getField("Cost01").valueAsString;
var v2 = this.getField("Trips01").valueAsString;
if (v1=="" && v2=="") event.value = "";
else {
var Cost01 = Number(v1);
var Trips01 = Number(v2);
event.value = Cost01 * Trips01;
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more