Copy link to clipboard
Copied
Hi there!
My question to you is how can I calculate the sum of certain filled fields?
For example in a PDF document I have some fields that need to be filled with name and surname and at the end of the document another field that represents the total of the "Name and surname" fields which shows how many persons are in this document. The operation shouldn't take in account blank/unfilled fields.
Now I know I could do this with the simple "sum(+)" calculation but I'd have to enter a "1" for each of this field and I'd like to avoid that.
Yeah, that's the first option I described. In that case you can use this code:
var total = 0;
for (var i=1; i<=79; i++) {
if (this.getField("Nume si prenume "+i).valueAsString!="") total++;
}
event.value = total;
Copy link to clipboard
Copied
That's not really a sum, but more of a total. To do it would require using a custom-made script.
Are the names of the fields consistent (like Name1, Name2, Name3, etc.), or is it just a list of unrelated fields?
If the latter then you can use this script as the custom calculation script of the text field where you want the total to appear:
var fields = ["First Name", "Last Name"]; // etc.
var total = 0;
for (var i in fields) {
if (this.getField(fields).valueAsString!="") total++;
}event.value = total;
Copy link to clipboard
Copied
Thank you try67, the code you provided works.
The names of the fields look like this: "Nume si prenume 1" till "Nume si prenume 79". Is there a way to "tell" the code to take in account all the other fields without having to enter each one manually?
Copy link to clipboard
Copied
Yeah, that's the first option I described. In that case you can use this code:
var total = 0;
for (var i=1; i<=79; i++) {
if (this.getField("Nume si prenume "+i).valueAsString!="") total++;
}
event.value = total;
Copy link to clipboard
Copied
Sir you are AWSOME! THANK YOU SO MUCH!
Copy link to clipboard
Copied
Hi there guys! A simple question for you. Which is the proper script to automatically fill a field with the double amount of a value entered in another field? I've already tried a few scripts but it seems that they're messing up the "total" field!
Thank you!
Copy link to clipboard
Copied
event.value = Number(this.getField("Other field name goes here").value) * 2;
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You need to adjust the fields calculation order. Total must be calculated after the Valoare fields, for example.
Copy link to clipboard
Copied
Hmm.. from where can I change this order?
Copy link to clipboard
Copied
In Form Edit mode, under Tasks - Other Tasks.
Copy link to clipboard
Copied
Found it, change it! But still when changing the values of the other fields after the total was calculated does not imediatly reflect in the "total" field nor the other to fields where the values should change according to the modified values. Wouldn't be better to use a custom calculation java script for the "total" instead of the standard calculation "sum "
Copy link to clipboard
Copied
Yes, that might be a good idea... Also check the JS console for error
messages.
If it still doesn't work, share the file again.
Copy link to clipboard
Copied
Ok, it took me a while to check fo errors but couldn't find one. I think the problem is with field "B" as shown in the .jpeg file I've posted earlier. The "B" field gets it's value from field "A". This value is automatically multiplied by two. So to get a value in "B" first I'll have to enter a value in "A". Now in "Field calculation order" field "A" does not appear instead field "B" does.
To be more speficic this form will be used for eyglasses orders. A client can/could make two pairs of glasses that's why I have to similar "areas" separated by the thin red line. The forms should calculate frames price, lens(fields "A" and "B") price and montage price if it is required and get the total shown in "TOTAL lei)" field. Then there are the other two lower fields, in first I enter the advance sum that the client pays and the other field calculates the rest of money the client has to pay. THIS is the file as it is at this moment. Don't know what else to say!
Copy link to clipboard
Copied
You set the calculation order in reverse order. The lower a field is in the list, the later it will be calculated. Total should be the last field in your list, not the first...
Copy link to clipboard
Copied
Thank you man! I've reversed the calculation order and now it seems to work just fine!
Oh.. just another tiny little question. Is it possible to add a button which when pressed to recalculate/refresh all the fields just to be sure there is no misscalculation mistake before printing?
Copy link to clipboard
Copied
It's possible, but it should not be necessary if you set it up correctly.
If you really want to do it then use this code as the button's MouseUp event:
this.calculateNow();
Copy link to clipboard
Copied
MAN YOU ARE AWSOME !!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now