Skip to main content
Participating Frequently
June 26, 2023
Question

Converting IF Then statement from excel to PDF form

  • June 26, 2023
  • 1 reply
  • 2403 views

I have the statement IF(A>0, A-B, B) in a excel document that I need to use in a PDF form and do not know how to write the script. Any help would be greatly appreciated, thanks!

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
June 26, 2023

Try something like this:

var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);

if(a&&b){
if(a>0)
event.value = a-b;
else
event.value = b;}
else
event.value = 0;

 

try67
Community Expert
June 26, 2023

This will not work correctly, as (a&&b) will always return false if either one of them is zero. So if A is 0 and B is 5 the result will be 0, not 5 (as expected).
If you want to check that neither of the fields are empty then you need to check their values as strings, and only then convert them to numbers.

Participating Frequently
June 26, 2023

Then use this:

 

var a = this.getField("A").valueAsString;
var b = this.getField("B").valueAsString;

if (a!="" && b!="") {
	a = Number(a);
	b = Number(b);
    if (a > 0)
        event.value = a - b;
    else
        event.value = b;
} else {
    event.value = 0;
}

Thank you. That seems to have fixed the issue when A=0 but when I try that and I imput a value A=10 and B=2 the calculation comes out as 0 now instead of 8.