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

Converting IF Then statement from excel to PDF form

Community Beginner ,
Jun 26, 2023 Jun 26, 2023

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!

TOPICS
How to , JavaScript , PDF forms
2.0K
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 ,
Jun 26, 2023 Jun 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;

 

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 ,
Jun 26, 2023 Jun 26, 2023

Thank you! That worked.

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 ,
Jun 26, 2023 Jun 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.

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 ,
Jun 26, 2023 Jun 26, 2023

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? 

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 ,
Jun 26, 2023 Jun 26, 2023

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?

 

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 ,
Jun 26, 2023 Jun 26, 2023

It should show zero.  Such that is value for A was zero, and value for B was 5 the result would be 5.

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 ,
Jun 26, 2023 Jun 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;
}
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 ,
Jun 26, 2023 Jun 26, 2023

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. 

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 ,
Jun 26, 2023 Jun 26, 2023

Works fine for me... See attached.

If your A and/or B fields have a calculated value, check the fields' calculation order.

Also, make sure remove all previous actions you added.

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 ,
Jun 26, 2023 Jun 26, 2023

I really appreciate your help with this. I think now I am getting mixed up when I try to apply it to cells with different names. 

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 ,
Jun 26, 2023 Jun 26, 2023
LATEST

OK. I see what I did wrong. Thank you again for the help.

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