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

Need one field to average the sum of all fields [including zero values]

Community Beginner ,
Sep 26, 2023 Sep 26, 2023

Hi, scripters!

I'm new to scripting, and after trying for several hours, I give up trying. I'm working on Acrobat Pro 2023.006.20320. What I need is simple, but for someone who knows so little, this is a huge challenge. In the attached test file, the last field under "Pieces" should sum all the fields with numbers [including those with zero value], ignore the blank one and then calculate the average [sample file should show 3.25]. On the same token, the small field under "Name" should count only the fields with a name on them, ignoring blank fields; I have a working script for it but ended up adding 74 fields manually [all have the same name except for the ending number], what would be the short version of the script?

Thanks for your patience and help! [Please don't add a link to read... I have probably already read it... and did not work].

TOPICS
JavaScript , PDF , PDF forms
734
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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 26, 2023 Sep 26, 2023

There really are a lot of examples on the forum for both of yours needs.

Script for average:

var total = 0;
var count = 0;
for( var i=0; i<=4; i++){
if(typeof this.getField("Pieces"+i).value === "number"){
total += Number(this.getField("Pieces"+i).valueAsString);
count++;}}
if(count !== 0)
event.value = total/count;
else
event.value = "";

Script for count names:

var n = 0;
for(var j=0; j<=4; j++){
if(this.getField("Text1."+j).valueAsString !== "")n++;}
event.value = n == 0 ? "" : n;

In both scripts, replace 4 with the number of fields you have, for example if the last field is 'Pieces74' then change 4 to 74 like this:

for( var i=0; i<=74; i++){

Same for count names script.

 

View solution in original post

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 ,
Sep 26, 2023 Sep 26, 2023

There really are a lot of examples on the forum for both of yours needs.

Script for average:

var total = 0;
var count = 0;
for( var i=0; i<=4; i++){
if(typeof this.getField("Pieces"+i).value === "number"){
total += Number(this.getField("Pieces"+i).valueAsString);
count++;}}
if(count !== 0)
event.value = total/count;
else
event.value = "";

Script for count names:

var n = 0;
for(var j=0; j<=4; j++){
if(this.getField("Text1."+j).valueAsString !== "")n++;}
event.value = n == 0 ? "" : n;

In both scripts, replace 4 with the number of fields you have, for example if the last field is 'Pieces74' then change 4 to 74 like this:

for( var i=0; i<=74; i++){

Same for count names script.

 

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 Beginner ,
Sep 27, 2023 Sep 27, 2023

Hi Nesa,

 

I was pretty sure there were posts answering my question; however, it's better to have my own questions about my particular case. I'll put your script to the test on my forms and see how it goes. Thanks very much for sharing your knowledge. I'll update you on my success... or my succession of errors.

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 Beginner ,
Sep 27, 2023 Sep 27, 2023
LATEST

Nesa, after tweaking and trial and error, I'm happy to report that the scripts worked perfectly. Thanks again for helping all the newbies out there, and keep up the excellent work!

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