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

Basic Calculation & Display Text (Custom Calculation script)

Explorer ,
Sep 19, 2017 Sep 19, 2017

Ok, I've been searching for 3 days on this answer, but everytime I change something I break my form again.

I have 4 text boxes:

Two with Currency variables.

In the 3rd box, they need to subtract in.

In the 4th box, it needs to display Over/Short/Balanced

I've got the form working with just that information. My problem is that if the first two boxes are blank, the other two need to be blank. Or in otherwords, if a calculation hasn't taken place, Box 3 & 4 need to remain blank till Box 1 & 2 are populated.

In Box 3 for the Calculation Script, I've got this:

event.value = ( this.getField("Actual_Cash_in_TCR").value - this.getField("TCR_Reported_Totals").value );

In Box 4 for the Calculation Script, I've got this:

var n = this.getField("Amount").value;

if( n > 0) event.value = "Over";

else if ( n < 0) event.value = "Short";

else event.value = "Balanced";

Where am I messing up, what needs to be added and where?

Adobe Acrobat Pro DC 2015.006.30355 for Windows.

TOPICS
Acrobat SDK and JavaScript , Windows
2.1K
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 ,
Sep 19, 2017 Sep 19, 2017

You can use an if/else construct to find out if the source fields are filled in. Something like this should work:

var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;

var val2 = this.getField("TCR_Reported_Totals").valueAsString;

if (val1 == "" || val2 == "") {

    event.value = "";

}

else {

    event.value = ( Number(val1) - Number(val2) );

}

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

I get SyntaxError: syntax error 3 for the "else" argument

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 ,
Sep 19, 2017 Sep 19, 2017

The code from above works for me. Can you please post the exact code you are using.

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

Ok, I just copied your code exactly, now there's no error, but theres nothing populating in Box 3 (in the picture, it's "Amount Difference", technically Box 4)

Adobe.JPG

var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;

var val1 = this.getField("TCR_Reported_Totals").valueAsString;

if (val1 == "" || val2 == "") {

event.value = "";

}

else {

event.value = ( Number(val1) - Number(val2) );

}

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 ,
Sep 19, 2017 Sep 19, 2017

If you are using my script for the forth field, then it should work. Are you getting any errors on the console?

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

Nope, it allows me to enter it and save it.

Just doesn't populate the Amount Difference Field, Like something is missing, or it's taking the Blank command to heart. Adobe2.JPG

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

Adobe4.JPG

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 ,
Sep 19, 2017 Sep 19, 2017

You defined two variables called "val1", instead of the second one being "val2"...

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

Wow, ok! It's been a string of Mondays. Thank you!

Ok, now how about the other?

Over/Short (box 3)... I've got it to be blank to start, but it's "reading" it incorrectly or I'm inputting incorrectly

if I input 15 & 15, the OVER/SHORT should read "Balanced". It's showing blank.

for 16 & 15, the OVER/SHORT should read "Short" but it's still blank,

for 14 & 15, the OVER/SHORT should read "Over" but this time it displays "Short"

This is what I have for Box 3:

var val3 = this.getField("Amount").valueAsString;

if (val3 == "" ) {

event.value = "";

}

else if ( val3 > 0 ) event.value = "Over";

else if ( val3 < 0 ) event.value = "Short";

else if ( val3 = 0 ) event.value = "Balanced";

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 ,
Sep 19, 2017 Sep 19, 2017

A value can't be a string and a number at the same time... Also, you have a syntax error in the last if-condition. Use this instead:

if (val3 == "" ) event.value = "";

else if ( Number(val3) > 0 ) event.value = "Over";

else if ( Number(val3) < 0 ) event.value = "Short";

else if ( Number(val3) == 0 ) event.value = "Balanced";

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

Unsuccessful...

doesn't val3 need to be defined?

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 ,
Sep 19, 2017 Sep 19, 2017

You still need this line from your script:

var val3 = this.getField("Amount").valueAsString;

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
Explorer ,
Sep 19, 2017 Sep 19, 2017

var val3 = this.getField("Amount").valueAsString;

if (val3 == "" ) event.value = "";

else if ( Number(val3) > 0 ) event.value = "Over";

else if ( Number(val3) < 0 ) event.value = "Short";

else if ( Number(val3) == 0 ) event.value = "Balanced";

Still getting some random displays.... 15/15 shows blank "OVER/SHORT", 15/14 is showing Balanced, 15/16 is showing Short, 15/17 is showing Over.

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 ,
Sep 19, 2017 Sep 19, 2017

Is the value of your field literally "15/15"? That's not a number, although it can be converted to one...

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
Explorer ,
Sep 20, 2017 Sep 20, 2017

Based on your message, I assume you mean something like this... It didn't produce errors, but still not calculating the correct output.

Just noticed something odd as well.

var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;

var val2 = this.getField("TCR_Reported_Totals").valueAsString;

var val3 = this.getField("Amount").valueAsString;

if (val1 == "" || val2 == "") {

event.value = "";

}

else if ( val3 > 0 ) {

event.value = "Over" ;

}

else if ( val3 < 0 ) {

event.value = "Short";

}

else {

event.value = "Balanced";

}

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 ,
Sep 20, 2017 Sep 20, 2017

Change:

var val3 = this.getField("Amount").valueAsString;

To:

var val3 = Number(this.getField("Amount").valueAsString);

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
Explorer ,
Sep 20, 2017 Sep 20, 2017

Everything is showing as "Balanced" now regardless of number

var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;

var val2 = this.getField("TCR_Reported_Totals").valueAsString;

var val3 = Number(this.getField("Amount").valueAsString);

if (val1 == "" || val2 == "") {

event.value = "";

}

else if ( val3 > 0 ) {

event.value = "Over" ;

}

else if ( val3 < 0 ) {

event.value = "Short";

}

else {

event.value = "Balanced";

}

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 ,
Sep 20, 2017 Sep 20, 2017

To see what goes into your calculation, add the following just before the if statement:

console.println("val3 = " + val3);

Then check the console to see what val3 is being set to.

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 ,
Sep 20, 2017 Sep 20, 2017

I can't help you further without seeing the actual file.

On Wed, Sep 20, 2017 at 5:16 PM, Throwzone2001 <forums_noreply@adobe.com>

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
Explorer ,
Sep 20, 2017 Sep 20, 2017

Maybe this will help?

Sample.pdf - Google Drive

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 ,
Sep 20, 2017 Sep 20, 2017

- You're not using the code we gave you. You still have the same error I mentioned in my first reply!

This is incorrect:

else if ( val3 = 0 ){

Either you follow our advice or don't, but don't say you do and then don't and say it's not working... That's very frustrating.

- Also, your field calculation order is incorrect. "Over-Short" needs to be calculated after "Amount".

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
Explorer ,
Sep 20, 2017 Sep 20, 2017

This is what I have in now (unless I missed something)...

What do you mean about Field Calculation? I'm not trying to not follow advice, just getting it in different formats.

try67 looks like what I was starting with, and Karl's looks like SQL, getting confused. (why I didn't go into programming).

var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;

var val2 = this.getField("TCR_Reported_Totals").valueAsString;

var val3 = Number(this.getField("Amount").valueAsString);

console.println("val3 = " + val3);

if (val1 == "" || val2 == "") {

event.value = "";

}

else if ( val3 > 0 ) {

event.value = "Over" ;

}

else if ( val3 < 0 ) {

event.value = "Short";

}

else if ( Number(val3) == 0 ) {

event.value = "Balanced";

}

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 ,
Sep 20, 2017 Sep 20, 2017

Fields are calculated in a certain order. When you calculate a field that depends on another field value before that second field gets calculated, then the results are of course wrong - you are basing your calculation on old values.

Acrobat allows you to set the field calculation order. This is also done when you are in the form editor, but it's selected via a menu:

2017-09-20_12-32-15.png

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 ,
Sep 20, 2017 Sep 20, 2017

BTW: You don't need to test for zero: That's already implied - when a value is not greater than zero and not less than zero, it must be zero, so you can just use the else branch as you did before:

else {

   event.value = "Balanced";

}

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