• Re: Javascript maths calculation (SUM, COUNTIF)

# Javascript maths calculation (SUM, COUNTIF)

Contributor ,
Mar 11, 2022 Mar 11, 2022

Copied

Hi, I'm hoping someone can help me urgently on this one...

I have the following formula in excel, but not sure how to change it so it works as Java in the custom calculation field in Acrobat (I've used some online tools but none seem to work).

• =SUM(B2:Q2)/(13*(COUNTIF(B2:Q2,"<>")))*100

JS wise, this where I've got to....not very far:

• event.value = ( this.getField("FINALscoreS1").value + this.getField("FINALscoreS2").value + this.getField("FINALscoreS3").value );

There'll be a number of 'FINALscoreS#' fields (yet to be decided). and what I'm after is:

The sum of all results in the 'FINALscoreS#' fields, divided by the sum of 13*however many 'FINALscoreS#' have a value (even if that value is zero...so ignoring any blanks).

(And then multiplied by 100 to give a percentage - the excel formula works perfectly if that's any use)

Does that makes sense? Can anyone help?? Pretty sure there'll be an array involved somewhere...

Huge thanks in advance...I expect for a JS guru this is as simple as 2+2, apologies!!

🙂

TOPICS
Acrobat SDK and JavaScript , Mac , Windows

Views

171

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Enthusiast , Mar 11, 2022 Mar 11, 2022

Hi,

Try that and let me know if I didn't understand someting.

``````var nb=numberOfFields;
var theSum=0;
var noBlank=0;
for (var i=1; i<=nb; i++) {
theSum+=Number(this.getField("FINALscoreS"+i).value);
if (this.getField("FINALscoreS"+i).valueAsString!="") noBlank++;
}
event.value=theSum*100/(13*noBlank);``````

@+

Likes

7 Replies 7
Enthusiast ,
Mar 11, 2022 Mar 11, 2022

Copied

Hi,

Try that and let me know if I didn't understand someting.

``````var nb=numberOfFields;
var theSum=0;
var noBlank=0;
for (var i=1; i<=nb; i++) {
theSum+=Number(this.getField("FINALscoreS"+i).value);
if (this.getField("FINALscoreS"+i).valueAsString!="") noBlank++;
}
event.value=theSum*100/(13*noBlank);``````

@+

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Contributor ,
Mar 11, 2022 Mar 11, 2022

Copied

Hi Bebarth,

Unfortunately I'm only getting a result of 0, no matter what value is in the 'FINALscoreS#' fields.

Reading the code (from my limited understanding) it looks like you've understood what I'm after, it's just not working as it should...Thanks again for trying to help, it's hugely appreciated 🙂

P.S. The fields are on separate pages...that didn't used to make any difference as far as a I remember, but thought I'd mention it just in case.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Contributor ,
Mar 11, 2022 Mar 11, 2022

Copied

ooooh...the "FINALscoreS" fields are calculated fields. They work fine as it's simple x+x...would that matter?

Thanks again

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Mar 11, 2022 Mar 11, 2022

Copied

Look at the attched file (I don't know the values which should be entered).

@+

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Contributor ,
Mar 11, 2022 Mar 11, 2022

Copied

aaah!!! FANTASTIC! I understand now, I needed to add the variable number. My mistake, i didn't get that.

This is perfect!! Thanks so much for your help. You have no idea how much I appreciate it 🙂

Thanks so again! 🙂 🙂 🙂 🙂 🙂 🙂 🙂

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 01, 2022 Jun 01, 2022

Copied

Dear Adobe, our company starts blocking java scripts in Adobe (Reader and Pro) how you use calculation fields after that? What is the alternative to javascript? How can these fields be used afterwards?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 02, 2022 Jun 02, 2022

Copied

LATEST

There is no alternative. If JS is disabled calculations will not work.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more