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

How to sum only visible fields?

Community Beginner ,
Nov 23, 2016 Nov 23, 2016

I'm using Acrobat Pro DC. My form has a column of 12 randomly named fields (product names) with dollar values. Each field is made visible or hidden with it's own checkbox action. I want to sum the column, but only the fields that are visible that have the boxes checked. I need a script that will determine which fields are visible, then sum them in a separate "Total" field. I don't know Javascript, but can do basic script editing make them work for me, like adding field names.

I am guessing this is pretty basic, but thanks for your any help you can provide!

TOPICS
Acrobat SDK and JavaScript
1.3K
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

LEGEND , Nov 23, 2016 Nov 23, 2016

It would be easiest if you use a custom calculation script for the Total field. The following is incomplete, but should get you started:

// Custom calculation script for Total field

// Initialize variable

var sum = 0;

// Add first field value if its corresponding check box is selected

if (getField("item1_checkbox").value !== "Off") {

    sum += +getField("product1").value;

}

// Add second field value if its corresponding check box is selected

if (getField("item2_checkbox").value !== "Off") {

    sum += +g

...
Translate
LEGEND ,
Nov 23, 2016 Nov 23, 2016

If you set the value to sum for the item to 0, zero, for the unselected state and the value when selected you can sum 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 Beginner ,
Nov 23, 2016 Nov 23, 2016

Sorry, but I have no idea what you just said . I don't know anything about scripting. I'm going to need a script i can copy & paste into the javascript field in Acrobat

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 ,
Nov 23, 2016 Nov 23, 2016

It would be easiest if you use a custom calculation script for the Total field. The following is incomplete, but should get you started:

// Custom calculation script for Total field

// Initialize variable

var sum = 0;

// Add first field value if its corresponding check box is selected

if (getField("item1_checkbox").value !== "Off") {

    sum += +getField("product1").value;

}

// Add second field value if its corresponding check box is selected

if (getField("item2_checkbox").value !== "Off") {

    sum += +getField("product2").value;

}

// Repeat for other 10 values

// Set this field value to the sum, or set to blank if zero

if (sum > 0) {

    event.value = sum;

} else {

    event.value = "";

}

You will have to replace the field names used with the getField calls with the actual field names you're using. Be sure to use the exact field names (case matters) and check for errors by displaying the JavaScript console (Ctrl + J). This script could be made more compact and efficient, but it should get you started.

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

Thanks George.

Something isn't working. I replaced in the checkbox field names in "item#_checkbox", and product value fields in the "product#" (for all fields 1-12).

It sums all the values in the the column whether the checkbox is checked or not.

It may be worth mentioning the values are static in the "product#" fields; they are a fixed number.

Can I somehow send you a sample of the form so you can see exactly what I'm doing?

Thanks. I'm heading out for Thanksgiving so may not get back until  Monday. Happy Thanksgiving!

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 28, 2016 Nov 28, 2016
LATEST

George,

I retried your script and it works perfectly. My error was I didn't clear the sum field so it had an existing value. I guess I was thinking more about turkey and stuffing than work.

Thank you for help!

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