PDF Editing Issues

How to Calculate Leave Hours from Multiple PDF Form Drop-Downs

Steve KSteve K Oct 28, 2026 869 views

Question details

The user wants to total specific leave hours, such as vacation, personal, and holiday time, by extracting values from multiple drop-down fields in a PDF form.

How to Calculate Leave Hours from Multiple PDF Form Drop-Downs
Product
PDF Forms
Device & OS
not provided
Scenario
Automating timesheets or leave request forms to automatically tally total hours based on the user's selected drop-down options.
Observed behavior
A script is needed to capture selected options from multiple drop-downs, map them to correct hour values, and output a sum while avoiding null or undefined errors.
Before you start

Ensure all your PDF form drop-down fields have exact, unique names and note the specific export values assigned to each leave type before writing your calculation script.

Solution 1Recommended

Use Custom JavaScript to Calculate Drop-Down Totals

Write a custom calculation script that reads the value of each drop-down field, determines the appropriate hours, and calculates the total sum.

To sum up leave hours, you need to read either the exact export value or the displayed string of each drop-down field. If your fields output text like 'Vacation' or 'Holiday', your script must translate these into numerical hours before adding them together.

1
Identify Form Field Names

Enter form editing mode in your PDF editor and accurately write down the exact names of all drop-down fields you want to include in the calculation (e.g., 'LeaveType1', 'LeaveType2').

2
Configure the Total Field

Create a Text Field where the total hours will be displayed. Right-click this field, select 'Properties', and navigate to the 'Calculate' tab.

3
Insert Custom Script

Select the 'Custom calculation script' option and click 'Edit'. This will open the JavaScript editor.

4
Write the Calculation Logic

Use code to retrieve field values using getField('FieldName').valueAsString. Create variables to map specific text (like 'Holiday') to numerical hour values (like 8), and assign the final sum to event.value.

5
Verify and Test

Save the script and test the drop-downs. If the total does not update, open the JavaScript console to check for errors like 'getField(...) is null', which indicates a typo in your field names.

Use Custom JavaScript to Calculate Drop-Down Totals
Script Validation: Successfully executing this script will automatically update the total hours instantly whenever a user changes a drop-down selection.

Create and Manage Interactive PDF Forms in WPS Office

Easily design interactive PDF forms with drop-downs, text fields, and advanced customization using WPS Office. Create professional timesheets and manage leave tracking efficiently without complex setup.

  1. 1. Open Your PDF: Launch WPS Office and open your existing PDF document or create a new one from a template.
  2. 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to view all interactive form field options.
  3. 3. Add Drop-Down Fields: Click on the 'Combo Box' or 'Drop-down' icon, then click on the document where you want the field to appear.
  4. 4. Configure Properties: Right-click the new drop-down field, select 'Properties', and add your list items and export values.
  5. 5. Save and Share: Once your form is complete, save the interactive PDF to share it with your team.
Easily add and format drop-down and combo box form fieldsHigh compatibility with interactive PDF form standardsLightweight and fast PDF viewer and editorIntuitive interface for form creation and management
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'getField(...) is null' error in my PDF script?

This error occurs when the field name referenced in your JavaScript does not exactly match the actual field name in the PDF form. Check for typos, extra spaces, or incorrect capitalization.

What does 'selectedOption is not defined' mean?

This JavaScript error means you are trying to use a variable named 'selectedOption' before declaring it. Ensure you initialize all variables using 'var' or 'let' before using them in your calculation logic.

What is the difference between value and valueAsString in PDF JavaScript?

Using '.value' retrieves the typed data (which could be a number, string, or boolean), whereas '.valueAsString' forces the returned value to be treated as plain text. This is highly useful when comparing string names like 'Holiday' or 'Vacation'.

Can I prioritize one drop-down field over another if both are filled?

Yes. You can use an IF/ELSE statement in your script to check if the priority field (e.g., SUNDAYLEAVE2) has a value. If it does, use its value; if not, fall back to using the value of the secondary field.