PDF Editing Issues

How to Calculate Average Ignoring Blanks and Zeros in PDF Forms

Camila MilosovichCamila Milosovich Nov 3, 2026 869 views

Question details

The user needs to calculate the average of specific numeric fields in a fillable PDF form without including blank fields or zero values in the calculation.

How to Calculate an Average While Ignoring Blank and Zero PDF Fields
Product
PDF Forms
Device & OS
not provided
Scenario
Setting up advanced field calculations in a fillable PDF form.
Observed behavior
Standard average calculation functions include zero values and sometimes blanks, which inappropriately lowers the calculated average. A custom script is required to exclude them.
Before you start

Ensure you know the exact field names you wish to calculate (e.g., Y_01 through Y_04) and verify that your PDF editor supports adding custom JavaScript to form fields.

Solution 1Recommended

Use a Custom JavaScript Calculation

Apply a custom JavaScript loop to dynamically count and average only the non-zero and non-blank fields.

By default, built-in PDF average functions will count zero values, which can skew the result. Writing a custom script allows you to selectively ignore fields that are blank or exactly zero.

1
Open Field Properties

Open your fillable PDF form in edit mode, right-click the target field where you want the average to appear, and select Properties.

2
Select Custom Script

Navigate to the Calculate tab, choose the option for 'Custom calculation script', and click the Edit button.

3
Input the Code

Paste the following script: var names=["Y_01","Y_02","Y_03","Y_04"]; var total=0, count=0; for (var i=0;i<names.length;i++){ var v=this.getField(names[i]).valueAsString; if (v!=="" && !isNaN(Number(v)) && Number(v)!==0){ total+=Number(v); count++; } } event.value=count ? total/count : "";

4
Test the Calculation

Click OK to save the script, close the properties window, and test your form by entering numbers, zeros, and leaving some fields blank to ensure the average calculates correctly.

Use a Custom JavaScript Calculation
Check Form Calculation Order: If the source fields (Y_01 to Y_04) contain calculated values themselves, ensure they are calculated before the average field in the PDF form's calculation order settings.
Advanced PDF Form Editing

Create and Edit Advanced PDF Forms with WPS Office

WPS Office provides a comprehensive suite of PDF tools, allowing you to easily create fillable forms, manage field properties, and configure advanced data calculations.

  1. 1. Open PDF Form: Launch WPS Office and open your PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab to insert or edit interactive text fields and buttons.
  3. 3. Edit Properties: Double-click the target field to open the Properties menu and configure your calculation settings easily.
Easily insert and manage text fields, checkboxes, and calculation fields.Fully compatible with standard PDF formats and Adobe Acrobat forms.Intuitive interface for editing PDF text, images, pages, and interactive elements.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the standard PDF average function include zeros?

Standard built-in average functions in PDF software count all targeted fields that contain a numerical value, including zero. To bypass this, you must use a custom JavaScript loop that explicitly excludes zero values from the division count.

How do I change the calculation order in a PDF form?

In most advanced PDF editors, you can adjust the calculation order through the form editing tools, often under a menu like 'More Form Options', 'Tools', or 'Calculation Order'. This ensures that prerequisite totals are calculated before final averages.

Can I apply this JavaScript snippet to more than four fields?

Yes. You can simply add the exact names of your additional fields to the 'names' array in the first line of the script. For example: var names=["Y_01","Y_02","Y_03","Y_04","Y_05"];