Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How to calculate sum of filled fields?

Community Beginner ,
Oct 08, 2016 Oct 08, 2016

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.

TOPICS
Acrobat SDK and JavaScript , Windows
5.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 08, 2016 Oct 08, 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;

Translate
Community Expert ,
Oct 08, 2016 Oct 08, 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 08, 2016 Oct 08, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2016 Oct 08, 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 08, 2016 Oct 08, 2016

Sir you are AWSOME! THANK YOU SO MUCH!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 22, 2016 Nov 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2016 Nov 22, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 22, 2016 Nov 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2016 Nov 22, 2016

You need to adjust the fields calculation order. Total must be calculated after the Valoare fields, for example.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 22, 2016 Nov 22, 2016

Hmm.. from where can I change this order?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2016 Nov 22, 2016

In Form Edit mode, under Tasks - Other Tasks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 22, 2016 Nov 22, 2016

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 "

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2016 Nov 22, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 22, 2016 Nov 22, 2016

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2016 Nov 22, 2016

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 23, 2016 Nov 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 23, 2016 Nov 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 23, 2016 Nov 23, 2016
LATEST

MAN YOU ARE AWSOME !!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines