Copy link to clipboard
Copied
I have googled forever for a solution to the following calculation/validation problem, but couldn't find a solution. Beeing an untalented javascript greenhorn does not really help 😉 (I am using Adobe Acrobat DC for the first time).
Is there someone who could help me with the script to achieve the following?
I have 6 text fields with a numeric entry (named text1, text2, text3, text4, text5 and text6). in the seventh field (called text7) I'd like to calculate the average of those fields. I've found how to do it in the calculation tab of text7. So far so good. But now I have to add some complexitiy to it. I have a field with a number called "tolerane".
Let's say I am measuring the diameter of a rope. The outcome of the 6 measurements are entered in fields text1 to text6. The average is calculated and if they are within the tolerance than the field shows the calculated average butremains transparent. But if the value is above or under tolerance, the text7 field containing the average of the measurements turns red. As the ropes have different diameters the tolerance varies. Therefore the tolerance is calculated as follows: rope diameter * 3%.
So in numbers it would look like this (for an 8mm rope):
ropediam: 8.00
tolerance in %: 0.03
tolerance: 0.24
text1: 8.27
text2: 8.24
text3: 8.23
text4: 8.26
text5: 8.25
text6: 8.24
text7: 8.248 (calculated based on average text1 to text6)
So if text7 returns a value of 8.23 the field remains transparent but if the value returns a value of below 8.00 (the rope diameter) or above 8.24, the field turns red.
I don't know if my rambling is understandable... but hope someone out there can point me into the right direction. Thanks!
See if this works for you:
var total = 0;
var x = 0;
for (var i=1; i<=6; i++) {
if (this.getField("text"+i).valueAsString != "") total++;
x += Number(this.getField("text"+i).value);
var cal = x/total;
}
if (total != ""){
event.value = cal;
event.target.fillColor = (cal < 8.00 || cal > 8.24) ? color.red : color.transparent;}
else event.value = "";
Copy link to clipboard
Copied
See if this works for you:
var total = 0;
var x = 0;
for (var i=1; i<=6; i++) {
if (this.getField("text"+i).valueAsString != "") total++;
x += Number(this.getField("text"+i).value);
var cal = x/total;
}
if (total != ""){
event.value = cal;
event.target.fillColor = (cal < 8.00 || cal > 8.24) ? color.red : color.transparent;}
else event.value = "";
Copy link to clipboard
Copied
Thank you so much Nesa, it worked! 🙂
First I tried in Adobe Acrobat DC, but there seams to be a js problem, cause the the field did not change it's color.
Then I tried it in Nitro PDF and it worked immediately.
I really appreciate your help, thank you!
Copy link to clipboard
Copied
It would be helpful if you posted the code you're using already.