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

Actual script for Sum of Fields

Community Beginner ,
Apr 12, 2021 Apr 12, 2021

I feel like this should be easy to find, but for some reason it isn't.

 

If I have fields named Strata.1, Strata.2, Strata.3, Strata.4 etc... and I want to return the sum of all the Strata fields in a field named Strata_Total.

I know I can use the pull down menu and just choose Sum and select the checkbox for Strata.

 

But what is the actual script that is happening behind the curtain?

 

TOPICS
How to , JavaScript , PDF forms
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
Community Expert ,
Apr 12, 2021 Apr 12, 2021

You can read about basic field scripting here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

And specically about calculations here:

https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations/

 

But since you've done such a great job of naming your fields, here's an advanced calculation script:

 

var sum = 0;
this.getField("Strata").getArray().forEach(a=> sum+=Number(a.value));
event.value = sum;
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Apr 13, 2021 Apr 13, 2021

I would advise against using this type of notation. It's not compatible with earlier versions of Acrobat and Reader and will not work there. Instead, you should stick to plain "vanilla" JS syntax, like this:

 

var sum = 0;
var fields = this.getField("Strata").getArray();
for (var i in fields) sum+=Number(fields[i].value);
event.value = 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
New Here ,
Jun 15, 2023 Jun 15, 2023

Hi There!

 

I am completely new to Java and working through building a PDF menu that will be sent through email and also accessed online for ordering purposes. I have a ton of fields (imagine foods on a restaraunt menu) and a variety of pricing for each item. I have created editable fields for the quantities of each item, which would be filled out by the person placing the order, and I have the pricing visible separately, not listed in a field.

 

Is there a way to create script to calculate each field by it's own price and then total at the bottom next to my "submit order button" that I've already connected?

 

This is the text I've entered into my Product_Total calculation script:

 

event.value = Number(this.getField("Product.1").valueAsString) * 12;

event.value = Number(this.getField("Product.2").valueAsString) * 10.5; event.value = Number(this.getField("Product.3").valueAsString) * 12; event.value = Number(this.getField("Product.4").valueAsString) * 9.5; event.value = Number(this.getField("Product.5").valueAsString) * 12; event.value = Number(this.getField("Product.6").valueAsString) * 9.5;

 

However, this doesn't create a summed total in the Product_Total box, just the last number in the script (9.5)

 

What should I be doing differently? Again, I know almost nothing at this stage, just learning by reading online and trial and error with the calculations, so any pointers would be helpful! Thank you in advance!

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 ,
Jun 15, 2023 Jun 15, 2023

You doesn't create a sum of the values.

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
New Here ,
Jun 15, 2023 Jun 15, 2023

Okay, so it sounds like a sum of the values isn't possible? Or the way I'm creating the fields won't allow me to create a sum of

values?

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 ,
Jun 15, 2023 Jun 15, 2023

To create a sum you must add the values.

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 ,
Jun 15, 2023 Jun 15, 2023

Put the prices in an array and use this:

var price = [12,10.5,12,9.5,12,9.5];
var fields = this.getField("Product").getArray();
var total = 0;
for( var i=0; i<fields.length; i++){
if(fields[i].valueAsString !== "")
total += Number(fields[i].valueAsString)*price[i];}
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
New Here ,
Jun 16, 2023 Jun 16, 2023

Thank you so much for sending this -- I tried entering this array as my script, and it seems to be totalling everything slightly off-kilter. If I enter values of 3 for each of these values on the editable form, I should get a sum of 198, but the form generates a total of 196.5. I've reviewed my fields in order to make sure they are listed correctly and that the values in the array correspond with the Field label and actual order on the menu; I've also tried plugging numbers in, but it looks like this script is also assigning the values to the fields in order from smallest to largest without regard to the typed order in the script, and not according to the order that they are input it. So, in other words, even though the script is entered as this:

var price = [12,10.5,12,9.5,12,9.5];

What is actually being applied to my form is:

var price = [9.5,9.5,10.5,12,12,12];

 

Any thoughts or other things that I could try to get this tightened up?

 

 

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 ,
Jun 16, 2023 Jun 16, 2023

@Macaeli305345004brc It works correctly for me, do you have any other calculations?

Can you share your file?

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
LEGEND ,
Jun 16, 2023 Jun 16, 2023

You've written

event.value = Number(this.getField("Product.3").valueAsString) * 12; event.value = Number(this.getField("Product.4").valueAsString) * 9.5; event.value = Number(this.getField("Product.5").valueAsString) * 12; event.value = Number(this.getField("Product.6").valueAsString) * 9.5;

Let's tidy that up, it's very hard to see mistakes if it's untidy.

event.value = Number(this.getField("Product.3").valueAsString) * 12;

event.value = Number(this.getField("Product.4").valueAsString) * 9.5;

event.value = Number(this.getField("Product.5").valueAsString) * 12;

event.value = Number(this.getField("Product.6").valueAsString) * 9.5;

Do you see a problem yet? Ok, think about this

event.value=1;

event.value=2;

event.value=3;

What this does is set event.value first to 1; then it throws away the 1 and sets it to 2. Then it throws away the 2 and sets it to 3. So the problem is that you aren't doing anything to add the numbers together, you're just throwing all of them away except the last.

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
New Here ,
Jun 16, 2023 Jun 16, 2023

Gotcha! I really appreciate all the responses!

 

So what magic can I use to instruct these event numbers to update into the total field as the order quantities are updated by the person typing into the form? 


 

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
LEGEND ,
Jun 17, 2023 Jun 17, 2023
LATEST

You just have to add the numbers together

event.value = first thing + second thing + third thing + ... 

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 ,
Jun 16, 2023 Jun 16, 2023

You don't need JavaScript for such a simple calculation.
In the attached PDF you can see the calculation in the text field and the export values in the checkboxes.

 

Capture_2306161441.png


Acrobate du PDF, InDesigner et Photoshoptographe
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
Enthusiast ,
Jun 16, 2023 Jun 16, 2023

@JR Boulaythat is old post.

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 ,
Jun 16, 2023 Jun 16, 2023

Yes, I realised that too late.

😉


Acrobate du PDF, InDesigner et Photoshoptographe
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