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

Calculation script for fields with text

Community Beginner ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

I have a form I am making that I have 3 fields that default value is N/A

I am trying to make a 4th field to add these 3 fields but the problem is if the field is not displaying a value then it the calculation script does not work. It only works if all 3 fields have a numerical value.

I have to have the field to display N/A if there is not a numerical value in it.

So my thoughts have been for the 4th field to convert the N/A value to 0 but how do I use the value if it is not N/A. I think I am over complicating it in my head and that is why I can't come up with a solution.

My original script:

var nDownPayment = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value + this.getField("DEAL_PURCHASE_CASHDOWN").value + this.getField("DEAL_PURCHASE_REBATE").value;

if( nDownPayment > 0.00) event.value = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value + this.getField("DEAL_PURCHASE_CASHDOWN").value  + this.getField("DEAL_PURCHASE_REBATE").value;

else event.value = 0.00

but this script doesn't display a value if "DEAL_PURCHASE_TRADE_TOTALNET1" and "DEAL_PURCHASE_CASHDOWN" have the default value of N/A and "DEAL_PURCHASE_REBATE" has a value in it.

How do I get it to either ignore the first 2 fields if their value is equal to "N/A" or treat "N/A" as the value of 0 only for the purpose of the calculation script.

DEAL_PURCHASE_TRADE_TOTALNET1 & DEAL_PURCHASE_CASHDOWN & DEAL_PURCHASE_REBATE will always be either a numerical value or text "N/A"

Thanks in advance for any help in solving this.

TOPICS
Acrobat SDK and JavaScript

Views

584

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Do not use "N/A" as the default value. Instead, make the default "0" and use a custom format script to display the N/A.

Custom format script:

if(event.value == 0)

    event.value = "N/A";

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

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

That worked for displaying N/A but the calculation script still doesn't calculate unless all 3 fields have a numerical value in them even though I went into each and put the value of 0.00 and it displayed N/A correctly but still doesn't calculate.

I think I have gotten close with this script. But it is only calculating the last field DEAL_PURCHASE_REBATE and not calculating the other fields even if they all have a numerical value in them.

var nTotalNet = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value;

var nCashDown = this.getField("DEAL_PURCHASE_CASHDOWN").value;

var nRebate = this.getField("DEAL_PURCHASE_REBATE").value;

if (nTotalNet == "N/A") {event.value = 0.00;}

else {event.value = nTotalNet;}

if (nCashDown == "N/A") {event.value = 0.00;}

else {event.value = nCashDown;}

if (nRebate == "N/A") {event.value = 0.00;}

else {event.value = nRebate;}

var nDownPayment = this.getField("nTotalNet").value + this.getField("nCashDown").value + this.getField("nRebate").value;

if( nDownPayment > 0.00) {event.value = nTotalNet + nCashDown + nRebate;}

else {event.value = 0.00;}

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Your logical conditions are overlapping, making only the last one have any effect on the value of the field.

You need to carefully think about how it is supposed to work and build the code based off of that.

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

After changing the default to 0, the fields will never have a value of N/A, so it can't be used as a condition.

There are also several other errors in the code. You only need one line

event.value = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value

                       + this.getField("DEAL_PURCHASE_CASHDOWN").value

                       + this.getField("DEAL_PURCHASE_REBATE").value;

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

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

The part of the code showing if ( nDownPayment > 0.00) must be in there because I can't have the field show a negative number as a result.

Ok I have set all fields "DEAL_PURCHASE_TRADE_TOTALNET1" & "DEAL_PURCHASE_CASHDOWN" & "DEAL_PURCHASE_REBATE" all have been set as default value as 0 and used your calculation script of if(event.value ==0) event.value = "N/A";

But it still doesn't calculate without a number other than 0 in each field. I have tried manually putting 0 in each field and still no luck.

I have put 0.01 and it works and I have put a negative number in and it works. But again all 3 fields must have a number for the script to work.

Another note the field "DEAL_PURCHASE_TRADE_TOTALNET1" may at times have a negative number which is fine even for the calculation script it works AS long as there are numbers in the other 2 fields.

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Fully removed the Value "N/A" altogether and my original script works. It seems to break the calculation anytime it has "N/A" in it. Seems to be no matter how I put the script in. As soon as one of the has "N/A" it breaks.

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

The simple one line addition script is all you need. Your original script is full of errors.

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

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Ok So the field with the calculation script was set to Format as a number. I used your one line of script from above. Once I removed the Number Format from this and I have

300

0 (which displays as N/A)

5

now my field calculates a result of 300N/A5

If I remove all uses of N/A the results calculate as intended.

The problem is this is a legal form and I can not have a field display 0.00 it must display a value either negative or positive otherwise it must display N/A.

Trust me this would be much easier if I didn't have to display N/A in the field

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

So the problem is that the format script I provided in my first post is being used in a calculation. That's why N/A is showing up in the result. To repeat, this script:

if(event.value == 0)

    event.value = "N/A";

Is a custom format script. Do not use it in a calculation because then it would change the value of the field. But as a format script it only changes how the field is displayed.

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

Votes

Translate

Translate

Report

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 ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

I fully understand that but I need it to be used in a calculation script. So how do I get the calculation script to either ignore the fact that it is N/A and ignore that field or to treat it as the value 0?

Votes

Translate

Translate

Report

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 ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Ideally, a field that is meant to be a number should be a number. That way it's usage is always consistent, whether it in a calculation or exported to a DB.

However, if absolutely necessary, you can test and condition the values used in a calculation.

var nTotalNet = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value;

var nCashDown = this.getField("DEAL_PURCHASE_CASHDOWN").value;

var nRebate = this.getField("DEAL_PURCHASE_REBATE").value;

var nDownPayment =(isNaN(nTotalNet)?0:nTotalNet)+ (isNaN(nCashDown)?0:nCashDown) + (isNaN(nRebate)?0:nRebate);

if( nDownPayment > 0.00)

     event.value = nDownPayment;

else

     event.value = 0.00;

Keep in mind that these numbers have to be tested in any other place they are used.

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

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 16, 2019 Feb 16, 2019

Copy link to clipboard

Copied

LATEST

You will need to code custom validation, custom formatting, and custom calculations scripts at a minimum. You could also add some custom keystroke scripting to allow only "N/A" and numbers and decimal points to be keyed in.

If you format a field as "Number" then "N/A" is not an allowed entry. If you format a field as text numbers are allowed but they mat not be usable within a calculation. Also when one enters a number into a text field, it may require some special handling within a calculation to set the type of value to a numeric value.

This is not a beginner project.

Have you looked at other PDF forms that behave this way?

I would start by getting the form to work without the "N/A" and then work on how have both the "N/A" and the numeric value entries and the calculations.

Votes

Translate

Translate

Report

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Remove all formatting from all fields involved. Just for testing.

Now, the result field will show the addition of all entries in the 3 input fields. If it doesn't then there is problem. Are there any errors displayed in the Console Window?

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

Votes

Translate

Translate

Report

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