Skip to main content
Pricoky
Known Participant
October 8, 2016
Answered

How to calculate sum of filled fields?

  • October 8, 2016
  • 3 replies
  • 6080 views

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.

This topic has been closed for replies.
Correct answer try67

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;

3 replies

Pricoky
PricokyAuthor
Known Participant
November 23, 2016

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?

try67
Community Expert
Community Expert
November 23, 2016

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();

Pricoky
PricokyAuthor
Known Participant
November 23, 2016

MAN YOU ARE AWSOME !!!

Pricoky
PricokyAuthor
Known Participant
November 22, 2016

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!

try67
Community Expert
Community Expert
November 22, 2016

event.value = Number(this.getField("Other field name goes here").value) * 2;

Pricoky
PricokyAuthor
Known Participant
November 22, 2016

OK! This works but... It is somehow messing up my TOTAL field. Don't know why. Here it is a explanation .jpej file and Here is the actual .pdf file if you could please take a look 'cause I just can't figure it out!

try67
Community Expert
Community Expert
October 8, 2016

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;

Pricoky
PricokyAuthor
Known Participant
October 8, 2016

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?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 8, 2016

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;