Custom calculation in JavaScript help needed please
- March 19, 2022
- 1 reply
- 1568 views
Hi. I am taking an Excel sheet and converting it into a PDF form, but I am having a hard time figuring it out since I have never done it before. I am not super familiar with JavaScript so I haven't been able to figure the right code for it. This is the formula from Excel: =IF(COUNTIF(F19:F23,0)=0,AVERAGE(F19:F23),"") This is the formula in cell F24. This means that you take the average of the range of F19-F23 unless it is zero or blank, then it returns a blank cell. Correct me if I am wrong. I think that I have to make an array of the fields Q4i1-Q4i5 and look for any zeros to automatically fill in the AvgQ4Score field. Is this correct? If so, how do I do that with JS? I tried a few things, but it is not working.
Here is some of the different code in the console, I am just not sure how to tie things together or if I am even going in the right direction:
var aNumFields = new Array("Q4i1", "Q4i2", "Q4i3","Q4i4","Q4i5");
myAverageFunction(aNumFields);
function myAverageFunction(aNumFields)
{
// n = number of fields that have a numerical value
var n=0, sum = 0;
for ( var i=0; i<aNumFields.length; i++) {
var v = this.getField(aNumFields[i]).value;
if ( v != "" ) {
n++;
sum += v;
}
}
if ( n == 0 ) event.value = "";
else event.value = s
//this was just to try to see if I could get anything at all to work, which it did
event.value = ( this.getField("Q4i1").value + this.getField("Q4i2").value + this.getField("Q4i3").value + this.getField("Q4i4").value + this.getField("Q4i5").value )/5;
if(this.getField("Q4i4").value = 0)
event.value="";
I appreciate any help! Thanks. I did attach a picture that has fields Q4i1-Q4i5 that average out to the AvgQ4Score field on the bottom, hopefully it is easy to read.
