Copy link to clipboard
Copied
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].
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!