Copy link to clipboard
Copied
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"]);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I am using Adobe Acrobat Pro.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
@try67 I changed it and is the error in the console.
ReferenceError: myAverageFunction is not defined
1:Field:Calculate.
Copy link to clipboard
Copied
Add the function in the document.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The function doesn't return a value.