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

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

New Here ,
Jun 23, 2023 Jun 23, 2023

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"]);

2.4K
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 Expert ,
Jun 23, 2023 Jun 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

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
New Here ,
Jun 23, 2023 Jun 23, 2023

I am using Adobe Acrobat Pro. 

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 Expert ,
Jun 23, 2023 Jun 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>

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 Expert ,
Jun 23, 2023 Jun 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);

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
New Here ,
Jun 24, 2023 Jun 24, 2023

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


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

 

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 Expert ,
Jun 25, 2023 Jun 25, 2023

Add the function in the document.

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 Expert ,
Jun 25, 2023 Jun 25, 2023
LATEST

You didn't add the word "function" before the function name. Also, as mentioned, this function should be located at the doc-level, so you could call it from different fields without having to duplicate the code.

See attached.

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 Expert ,
Jun 23, 2023 Jun 23, 2023

The function doesn't return a value.

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