Skip to main content
Participating Frequently
June 3, 2024
Answered

Splitting the total sum based on dropdown options selected

  • June 3, 2024
  • 3 replies
  • 2015 views

I've created a form that adds the total amount of trip and vacation days. The dropdown options are (Departure (Trip), Return (Trip), Trip (Half Day), Trip, Vacation (Half Day), and Vacation). The value to each of these options is 1. So whenever you select a option from the dropdown list, there is a
"Total days" column that collects this info and counts each option as 1. At the bottom of the grid, I have a "Total Trip Days" and "Total Vacation Days" row. Based on the option selected from the dropdown list, I would like for all "Trip" options selected to be added to the sum of the "Total Trip Days" row and for all the "Vacation" options selected to be added to the total sum of the "Total Vacation Days"  row. Right now, each option selected is added to the total sum of "Total Trip Days." Is there a custom script I can insert to perform this task? (Please see the attached form as an example)

This topic has been closed for replies.
Correct answer PDF Automation Station

Add another export value:  TripHalf, and then change the script as follows:

var trip=0;
for(var i=1;i<15;i++)
{
if(this.getField("Dropdown"+i).value=="Trip")
{trip+=1}
if(this.getField("Dropdown"+i).value=="TripHalf")
{trip+=0.5}
}
event.value=trip;

3 replies

Inspiring
June 5, 2024

Yes, you can achieve this functionality with a custom script. You'll need to write JavaScript to differentiate between "Trip" and "Vacation" options and then update the respective totals accordingly. You can attach event listeners to the dropdown options and update the totals dynamically based on the selected option.

PDF Automation Station
Community Expert
Community Expert
June 3, 2024

Agreeing with Thom that Total Days columns isn't helpful.  Change the export values to either "Trip", or "Vacation".  Enter the following script as a custom calculation script in the Total Trip Days field:

var trip=0;
for(var i=1;i<15;i++)
{
if(this.getField("Dropdown"+i).value=="Trip")
{trip+=1}
}
event.value=trip;

Enter the same script in the Total Vacation Days field but change "trip" to "vacation" and "Trip" to "Vacation".

Participating Frequently
June 4, 2024

Hello, this actually worked perfectly! Thank you so much.  I am also wondering if there is a way to count the "half days" as .5 instead of 1?

 

- Trip (Half Day) 

- Vacation (Half Day)

PDF Automation Station
Community Expert
Community Expert
June 4, 2024

Add another export value:  TripHalf, and then change the script as follows:

var trip=0;
for(var i=1;i<15;i++)
{
if(this.getField("Dropdown"+i).value=="Trip")
{trip+=1}
if(this.getField("Dropdown"+i).value=="TripHalf")
{trip+=0.5}
}
event.value=trip;
Thom Parker
Community Expert
Community Expert
June 3, 2024

Separating trip days from vacation days requires analyzing the dropdown values.  To do this it is helpful to name the fields so that the field set of interest can be easily acquired. For example, if the fields in the "Description" column were named "Description.Row1",  "Description.Row2", etc. then the selected values could be easily acquired. 

Also, the total days field is not helpful, since it will always be 1 day.  It would also be better for the caculation if there was no export value. 

And the calculation scripts could be written like this.

 

// Custom calculation for Trip days total

var nSum = 0;

this.getField("Description").getArray().forEach(function(oFld){if(oFld.getItemAt(oFld.currentValueIndices,false).indexOf("Trip")>-1)nSum++;});

event.value = nSum;

 

Haven't debugged this so open the console window before testing it to see if any errors are reported when it was run. 

The code could be simplified if the export values were removed from the description dropdown, or if they were changed to numbers that represent the Trip/Vacation split. 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
June 3, 2024

Hi Thom, thank you so much for the prompt feedback. I work for Mayo Clinic and we are developing this form for our doctors to use and I want it be as userfriendly as possible. I know this may be a stretch, but would it be possible for us to potentially connect on a teams call? I would like to know how this script should be implemented as I have several of other scripts running on this form already.

Thom Parker
Community Expert
Community Expert
June 3, 2024

@Kevin37813114stf9 , I'd be happy to help you with your required form functionality.  For private consulting/development, Contact me through www.pdfscripting.com.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often