Copy link to clipboard
Copied
hi, in my form i want to calculate average in 40 text fields. fields names are AB-1, AB-2, AB-3, etc up to AB-40. i have found bellow code that really works but only issue is that code not ignoring zero values. so can u pls help me edit bellow code to ignore zero. thanks..
// Initialize variables
var num = 0;
var sum = 0;
// Loop through the input fields
for (var i = 1; i < 5; i++) {
var f = getField("AB-" + i);
if (f.valueAsString) {
// increment the non-blank field counter
num++;
// add the field value to the running total
sum += +f.value;
}
}
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}
Use this:
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 40; i++) {
v = +getField("AB-" + 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 = "";
}
Copy link to clipboard
Copied
You could change it to something like the following:
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 40; i++) {
v = +getField("AB-" + i).value;
if (v !== 0) {
// increment the non-blank/zero field counter
num++;
// add the field value to the running total
sum += +f.value;
}
}
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}
Copy link to clipboard
Copied
hi, thanks a lot for your code but answer giving zero & not calculating correctly..
Copy link to clipboard
Copied
Use this:
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 40; i++) {
v = +getField("AB-" + 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 = "";
}
Copy link to clipboard
Copied
Hi, I'm sure someone can help me.
I have 15 fields all with different names.
I would like another field (box16) to calculate the average of all, but only of the fields which have a number inserted.
So I would like "box16" to not include empty fields when calculating the average.
Thank you.
I'm not very good at this, so I would appreciate a very simple explanation.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Here is one way to do it, Put this code in the Custom Calculation for the Average field, "box16" :
var aFieldNameList = ["Text1","Text2","Text3","Text4"];
var nSum = 0, nCnt = 0, oDoc=this;
aFieldNameList.forEach(function(a){if(((v=oDoc.getField(a).valueAsString) !="") && !isNaN(v)){nSum+=v;nCnt++;}});
event.value = nCnt?nSum/nCnt:"";
Copy link to clipboard
Copied
Hi, can you please help me with this....since your previous script worked perfectly for me.
This time the fields are not AB-1, AB-2, AB-3 like in the previous example of your script.
Instead they are A1, B1, C1, D1 ( practically all the letters from A to Z followed by the number 1 )
Thank you
Copy link to clipboard
Copied
what script are you talking about?
Copy link to clipboard
Copied
This one:
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
for (i = 1; i <= 40; i++) {
v = +getField("AB-" + 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 = "";
}
Copy link to clipboard
Copied
Incrementing letters is different from incrementing numbers. Each letter has a character code, so you need to increment the character code, then convert it to a letter.
The character code for "A" is 65 or 0x41 in Hexidecimal.
Character codes are converted into actual characters using
String.fromCharCode();
So here's a modification of the loop portion of your script:
var nCharA = 65;
for (i = 0; i < 40; i++) {
var strName = String.fromCharCode(nCharA + i) + "1";
v = +getField(strName).value;
Copy link to clipboard
Copied
Thanks. But that is very difficult to understand.
Is this loop portion including all the letters from A to Z ?
Copy link to clipboard
Copied
Yes, but also other characters... Not sure why it's going up to 40 instead of 26
To get only the letters change 40 to 26 in the code above.
To verify it you can run this code from the console:
var nCharA = 65;
for (i = 0; i < 26; i++) {
console.println(String.fromCharCode(nCharA + i));
}
Copy link to clipboard
Copied
I really appreciate your time. I understand that incrementing letters is not the same as numbers.
Maybe I'm asking too much but could someone please post the full Custom calculation script I need ?
I have about 1000 text fields in my pdf , so it's getting a bit confusing.
Thanks.
Copy link to clipboard
Copied
Here's the loop for calculating the sum and number of non-empty entries. If you are writing code for a large form, then you need to understand the scripting, or hire a professional. For example, knowing that the loop should be set to a limit of 26 should be a given. It's your form, you designed the fields.
// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
var nCharA = 65;
for (i = 0; i < 26; i++) {
var strName = String.fromCharCode(nCharA + i) + "1";
v = Number(getField(strName).value);
if (v !== 0) {
// increment the non-blank/zero field counter
num++;
// add the field value to the running total
sum += v;
}
}
Copy link to clipboard
Copied
Copied the script, but it doesn't do anything.
Copy link to clipboard
Copied
This is only part of the script. The rest you already have, i.e. the part the calculates the average and assigns it to the field value.
Here's the bottom part of the script.
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}
Copy link to clipboard
Copied
That's exactly what I did, but it does nothing.
Copy link to clipboard
Copied
What's reported in the console window?
Copy link to clipboard
Copied
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: getField(strName) is null
7:Field:Calculate
Copy link to clipboard
Copied
There is no "f" in the script, so you've got an error in some other script.
This is the error in the script,
TypeError: getField(strName) is null
7:Field:Calculate
and you are correct that one of the field names that has the problem. If you've already changed the problem name to match the pattern, then all is good.
Copy link to clipboard
Copied
Changed the end fields name and it seems to be working now. I'll let you know.
Copy link to clipboard
Copied
Worked perfectly. Thank you.
Copy link to clipboard
Copied
Thank you so much from the United States @Bernd Alheit, I'm completely unfamiliar with coding but got this to work on a very important document. I really appreciate your help!