Skip to main content
Participant
June 23, 2023
Question

Get the Average of the rows with value. Ignore the 0 and empty field

  • June 23, 2023
  • 4 replies
  • 2689 views

I use this code I saw in the discussions but it didn't work.

I need to get the average of each field with data and ignore the empty or 0 fields.

 

myAverageFunction(aNames)
{
// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 21; i++)
{

v = +getField("AvgCohRow1_."+ i).value;

if (v!== 0) {

// increment the non-blank/zero field counter

num++;

// add the field value to the running total

sum += v;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}
}


event.value = myAverageFunction(["AvgCohRow1_1", "AvgCohRow1_2", "AvgCohRow1_3", "AvgCohRow1_4", "AvgCohRow1_5", "AvgCohRow1_6", "AvgCohRow1_7", "AvgCohRow1_8", "AvgCohRow1_9", "AvgCohRow1_10", "AvgCohRow1_11", "AvgCohRow1_12", "AvgCohRow1_13", "AvgCohRow1_14", "AvgCohRow1_15", "AvgCohRow1_16", "AvgCohRow1_17", "AvgCohRow1_18", "AvgCohRow1_19", "AvgCohRow1_20", "AvgCohRow1_21"]);

    This topic has been closed for replies.

    4 replies

    Bernd Alheit
    Community Expert
    Community Expert
    June 24, 2023

    The function doesn't return a value.

    try67
    Community Expert
    Community Expert
    June 23, 2023

    The code you posted should do just that. If it doesn't, check the JS Console for errors, and/or share the file in question. The screenshot you posted is not very helpful...

     

    However, you should adjust this line of the code:

    for (i = 1; i <= 21; i++)

    To:

    for (i = 0; i<aNames.length; i++)

     

    And this:

    v = +getField("AvgCohRow1_."+ i).value;

    To:

    v = Number(getField(aNames[i]).valueAsString);

    Participant
    June 25, 2023

    @try67 I changed it and is the error in the console.


    ReferenceError: myAverageFunction is not defined
    1:Field:Calculate.

     

    Bernd Alheit
    Community Expert
    Community Expert
    June 25, 2023

    Add the function in the document.

    kglad
    Community Expert
    Community Expert
    June 23, 2023

    i assume this is an acrobat question.

     

    in the future, to find the best place to post your message, use the list here, https://community.adobe.com/

     

    p.s. i don't think the adobe website, and forums in particular, are easy to navigate, so don't spend a lot of time searching that forum list. do your best and we'll move the post if it helps you get responses.

     

    <moved from using the community>

    John T Smith
    Community Expert
    Community Expert
    June 23, 2023

    When you post a question you always need to tell the Adobe program you are using
    There are MANY programs in a full subscription, plus other non-subscription programs
    Please post the exact name of the Adobe program you use so a Moderator may move this message to that forum

    Participant
    June 24, 2023

    I am using Adobe Acrobat Pro.