Hello,
I am trying to write a script for a fillable PDF in Adobe Acrobat, but I don't have much experience with JavaScript and I need some help!
Basically I have three fields as follows:
A1 = a field with a number value in it
B1 = a field with a number value in it
C1 = a field where the sum calculation will be.
I want to write a script that will enter the lesser of the values in A1 and B1 into C1. If no value has been entered into B1, then it enters the value of A1 into C1.
This is what I have come up with so far:
// Get the field values, number fields as numbers
var d1 = getField("A1").value;
var d2 = +getField("B1").value;
// Set the field value to either A1 or B1, whichever is lesser
event.value = Math.min(d1,d2);
Some of the problems with the script so far are:
Can anyone help??
Thanks!
The second issue has to do with the field calculation order. You should set C1 to calculate after both A1 and B1.
Use this code instead of what you currently have:
var d1 = getField("A1").value;
var d2 = getField("B1").value;
if (d2=="") event.value = d1;
else event.value = Math.min(+d1,+d2);
Adjusted the field calculation order fixed question 2.
The script given as answer worked great except for one area.
B1 values can range from 0 and up. If a 0 is entered in B1, then the Math.min ignores it since d2=="".
I can get around that problem by having a default value set in B1 of -1 (outside of the range). I'd prefer that there not be a default value set.
Any suggestions? I appreciate the help.
That script can be changed to the following to fix the problem:
var d1 = getField("A1").valueAsString; var d2 = getField("B1").valueAsString; if (d2 === "") event.value = d1; else event.value = Math.min(+d1,+d2);