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

If a text field is zero, display nothing and check the next text field.

Community Beginner ,
Sep 07, 2023 Sep 07, 2023

I am attempting to write a script that checks the value of a field and displays fixed text + the value. If the value is zero, it should display nothing and check the next field.

 

If there are multiple fields with a value greater than zero, it should display them with a comma separating them.

 

The purpose of this document is to allow someone to choose how many awards they have and then display them in a specific format.

For example:

If someone indicates that they have an achievement award and two commendations, it should display as "UTACH-1, UTCOM-2". If they have zero achievement awards but two commendations, it'll display as "UTCOM-2".

 

The issue I'm running into is that there are 44 total awards so I have to come up with an eloquent solution rather than just do a bunch of "if else" commands.

TOPICS
Create PDFs , JavaScript , PDF forms
2.4K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Sep 07, 2023 Sep 07, 2023

Use this:

var medal = ["MOVSM-", "CGACH-", "AFAM-", "NAM-", "NAMV-", "AAM-", "JSAM-","CGCM-", "AFCM-", "NCM-", "NCCDV-", "ARCOM-V-", "ARCOM-", "JSCM-","JSCMV-", "AFAAM-", "AM-V-", "AM-", "MSM-", "DMSM-", "BSM-V-","BSM-", "ASM-", "CGM-", "NMCM-", "SM-", "DFC-", "LM-", "DSSM-","SS-", "CGDSM-", "DSM-", "DDSM-", "AFC-", "NC-", "DSC-", "MH-","UTACH-", "UTCOM-", "UTJCM-", "UTMOM-", "UTJMM-", "UTCRS-", "UTMOV-"];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var str = [];

for (var i = 1; i <= 44; i++) {
 var newScore = Number(this.getField("Text"+i).valueAsString);
  if(newScore !== 0){
   score[i-1] = newScore;}}

for(var j=0; j<score.length; j++){
 if(score[j] !== 0)
  str.push(medal[j]+score[j]);}

event.value = str.join(",");

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

After looking at this more closely, You're best strategy is to place the display text in the tooltip text. 

 

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");

 

 

Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior. 

You can add new fields and change the display text without changing the script. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

I do have this script that does work but I'd very much like to avoid the headache of writing out all of the "if else" commands I was talking about.

 

var a = this.getField("Text38").value;
var b = this.getField("Text39").value;

if (a == "0")

{

event.value = ""

} else {

event.value = "UTACH-" + a + ", " + "UTCOM-" + b

}

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

Name all of the award fields "Award.Field1", "Award.Field2", etc. 

Then this calculation script will collect all the data

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aWards.push("UTACH-" + oFld.value);});
event.value = aAwards.join(",");

 

This is nearly as elegant as you can get.

You did not provide a way to differentiation between the "UTACH" and "UTCOM". How were you planning on doing this? 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

That is a great question, sir! I am still trying to wrap my head around JavaScript. I have attached the document I am trying to create in order to help illustrate what I'm working with.

 

Thank you for the help with the array. I've been combing through the Adobe JavaScript Reference to try and get something working here.

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

You could put the Name you want displayed with the number in either the name of the field, or the tooltip for the field. 

But you need to explain more.

Is the script counting the total number of awards and comendations? or is it displaying the individual award or comendation selected? 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

After looking at this more closely, You're best strategy is to place the display text in the tooltip text. 

 

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");

 

 

Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior. 

You can add new fields and change the display text without changing the script. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Thank you so much!

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

You are welcome.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Use this:

var medal = ["MOVSM-", "CGACH-", "AFAM-", "NAM-", "NAMV-", "AAM-", "JSAM-","CGCM-", "AFCM-", "NCM-", "NCCDV-", "ARCOM-V-", "ARCOM-", "JSCM-","JSCMV-", "AFAAM-", "AM-V-", "AM-", "MSM-", "DMSM-", "BSM-V-","BSM-", "ASM-", "CGM-", "NMCM-", "SM-", "DFC-", "LM-", "DSSM-","SS-", "CGDSM-", "DSM-", "DDSM-", "AFC-", "NC-", "DSC-", "MH-","UTACH-", "UTCOM-", "UTJCM-", "UTMOM-", "UTJMM-", "UTCRS-", "UTMOV-"];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var str = [];

for (var i = 1; i <= 44; i++) {
 var newScore = Number(this.getField("Text"+i).valueAsString);
  if(newScore !== 0){
   score[i-1] = newScore;}}

for(var j=0; j<score.length; j++){
 if(score[j] !== 0)
  str.push(medal[j]+score[j]);}

event.value = str.join(",");
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 07, 2023 Sep 07, 2023

Thank you! This worked perfectly!

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

Hello ma'am! Is there a way to have it display "NO AWARDS" if all text boxes are 0?

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

Yes, there are a couple of ways to do this. And don't call me Ma'am

Use one, and only one, of the following methods. Do not use both. 

 

1.  If you don't want the actual field value to be affected by no awards being selected, then use a custom format script

  if(event.value == "")  event.value = "NO AWARDS";

 

A format script changes the text displayed to the user, but does not change the actual field value.

 

2.  Just modify the custom calculation srcript.  

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
if(aWards.length == 0)
   event.value = "NO AWARDS";
else
   event.value = aAwards.join(",");

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

I apologize! I was referring to Nesa Nurani. Thank you for the help again!

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