Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Thank you! That worked.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you for pointing that out. How would I have to write this out so that an A value of zero does not cause a miss cacluation?
Copy link to clipboard
Copied
Depends. What should be the outcome if either one of the fields is empty? Should the calculated field show zero? Nothing at all? Something else?
Copy link to clipboard
Copied
It should show zero. Such that is value for A was zero, and value for B was 5 the result would be 5.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
OK. I see what I did wrong. Thank you again for the help.