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

How to script: If (field 1 through 5 ="N/A") event.value="N/A"

Explorer ,
Aug 02, 2016 Aug 02, 2016

I'm
still learning and have a script as follows:

var
totalY = 0; 

var
total = 0; 

for

var totalY = 0; 

var total = 0; 

for (var i=1; i<=5; i++) { 

    var f = this.getField("JDCRDropdown"+i); 

    if (f.valueAsString=="Y") { 

        totalY++; 

        total++; 

    } else if (f.valueAsString=="N") { 

        total++; 

    } 

if (total==0) event.value = "N/A"; 

else event.value = totalY + " of " + total;

The
problem is that and N/A is different than zero of zero. If I leave as is, then the output field defaults to N/A. If I remove: if (total==0) event.value = "N/A";  then it spits out 0 of 0 as the default. What I need is for it to default to 0 of 0, UNLESS all 5 fields are N/A. Then I need the output to be N/A.

ALL 5 fields must have N/A selected in order for it to populate N/A.

I've attempted a few different scripts to no avail.

Thanks

TOPICS
Acrobat SDK and JavaScript , Windows
562
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

correct answers 1 Correct answer

LEGEND , Aug 02, 2016 Aug 02, 2016

Add a variable to keep track of the number of "N/A" selections, and if it's 5 after the loop, set the field value to "N/A".

Translate
LEGEND ,
Aug 02, 2016 Aug 02, 2016

Why are defining the variables total and totaly twice. Once outside your loop and then again the loop. This is causing a scope issue. Define these variables once. You msy also have a rounding issue caused by Acobat's use of IEEE 16 bit floating point conversion.

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
Explorer ,
Aug 03, 2016 Aug 03, 2016

Apologies.

This was just a copy and paste error.

var
totalY = 0; 

var
total = 0; 

for

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
LEGEND ,
Aug 02, 2016 Aug 02, 2016

Add a variable to keep track of the number of "N/A" selections, and if it's 5 after the loop, set the field value to "N/A".

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
Explorer ,
Aug 03, 2016 Aug 03, 2016
LATEST

This is fun. This is what I came up with. Thanks

var totalY = 0; 

var total = 0;

var totalNA = 0;

for (var i=1; i<=5; i++) { 

    var f =
this.getField("JJCRDropdown"+i);

    if
(f.valueAsString=="Y") { 

        totalY++; 

        total++; 

    } else if
(f.valueAsString=="N") { 

        total++;

    } else if (f.valueAsString=="N/A")

        totalNA++; 

    } 

if (totalNA==5) event.value = "N/A"; 

else event.value = totalY + " of " + total;

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