Skip to main content
October 15, 2020
Answered

Confused about If Then Statements

  • October 15, 2020
  • 2 replies
  • 1708 views

I'm converting a Projected Operating Costs spreadsheet from Excel to Adobe since some of my end users do not have Excel and Google takes away the security features in the document.

 

I have no experiencing with Java and am really struggling grasping how to write an if then statement for one of the fields.

 

The Text field needs to look at another field "S2HRFreeCount" and determine if it is greater than '0'.  If it is greater than '0' I want it to display the value I have entered in another field "S2HRFreeRate."  This rate changes every year so I'm wanting to use it from the field rather then enter the value in the script.  Less places to update.  If  there is no text in "S2HRFreeCount" than I want the field to either be blank or display "0."

 

My end user does not need to enter a value in field S2HRFreeCount. 

 

Any assistance would be appreciated.  I've only started creating Adobe Fillable Forms within the last 6 months so I'm learning as I go.  And this is the most complicated component so far.

This topic has been closed for replies.
Correct answer Thom Parker

The easiest way to do this is a Custom Calculation script on the text field. 

 

 

var cFreeCount = this.getField("S2HRFreeCount").valueAsString;
if((cFreeCount != "") && !isNaN(cFreeCount) && (Number(cFreeCount) > 0))
  event.value = this.getField("S2HRFreeRate").value;
else
  event.value = "";

 

 

Note that there are 3 conditions used to determine whether or not the FreeRate value is used. 

The FreeCount field is not empty, it's value is a number, and that number is greater than 0;

 

You can read more about calculation scripts here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

And about if statements in calculations here:

https://acrobatusers.com/tutorials/conditional-execution/

 

2 replies

Participant
August 17, 2021

I am stuggling to get this working.

 

( BldgPermitFee+ElectFee+PlumbingFee+SignFee+COO )= < 10,000 than *.2

( BldgPermitFee+ElectFee+PlumbingFee+SignFee+COO )= between 10,001 - 20,000 than *.12

( BldgPermitFee+ElectFee+PlumbingFee+SignFee+COO )= between 20,001 - 30,000 than *.10

( BldgPermitFee+ElectFee+PlumbingFee+SignFee+COO )= between 30,001 - 40,000 than *.09

( BldgPermitFee+ElectFee+PlumbingFee+SignFee+COO )= > 40,000 than *.08

Thom Parker
Community Expert
Community Expert
August 17, 2021

Is this a custom calculation script?  I'll just assume it is for the answer. 

First, you have to actually have an if/then structure and use Acrobat JavaScript functions.

I'd suggest reading these articles:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

https://acrobatusers.com/tutorials/conditional-execution/ 

 

Here's how you set it up.  The important bit for a calculation is that the calculated value is assigned to "event.value".

 

 

 

var nSum = this.getField("BldgPermitFee").value + this.getField("ElectFee").value + this.getField("PlumbingFee").value + this.getField("SignFee").value + this.getField("COO").value;

if(nSum <= 10000)
   event.value = 0.2;
else if (nSum <= 20000)
   event.value = 0.12;
...etc...

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
August 17, 2021

Thank you I did find one mistake I keep doing but its still not working and i have read the artcles and made changes.  It there something else I could be missing?

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
October 15, 2020

The easiest way to do this is a Custom Calculation script on the text field. 

 

 

var cFreeCount = this.getField("S2HRFreeCount").valueAsString;
if((cFreeCount != "") && !isNaN(cFreeCount) && (Number(cFreeCount) > 0))
  event.value = this.getField("S2HRFreeRate").value;
else
  event.value = "";

 

 

Note that there are 3 conditions used to determine whether or not the FreeRate value is used. 

The FreeCount field is not empty, it's value is a number, and that number is greater than 0;

 

You can read more about calculation scripts here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

And about if statements in calculations here:

https://acrobatusers.com/tutorials/conditional-execution/

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
October 16, 2020

Thank you.  I have so much more to learn about these advance features in Adobe.  But making the conversion to Adobe will make my life so much easier in the long run.