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

Help changing a excel formula to javascript

New Here ,
Jan 08, 2021 Jan 08, 2021

My boss gave me an invoice to create a workable PDF form and the final formula had this calculation...

=+C27+C29+(IF(((E14+E25)-C27)>0,((E14+E25)-C27),"0"))

 

I tried to change it to work in my form but there appears to be an issue (see below).  I feel that I am pretty close but not sure what I am doing wrong (syntaxerror: syntax error 3: at line 4).  can anyone help me out?

 

event.value = (this.getfield ("text15").value + this.getfield ("text16.0").value)

+ if (this.getfield (this.getfield ("text11").value + this.getfield ("text14").value) - this.getfield ("text15").value > "0",

else if (this.getfield (this.getfield ("text11").value + this.getfield ("text14").value) - this.getfield ("Text15").value, 0))

TOPICS
How to , JavaScript , PDF forms
1.3K
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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 09, 2021 Jan 09, 2021

++ Adding to Try67's guidance,

 

FROM THE EXCEL FORMULA: =+C27+C29+(IF(((E14+E25)-C27)>0,((E14+E25)-C27),"0"))

 

The total value of this formula is based on the SUM for this calculation: C27+C29+((E14+E25)-C27) as long as it meets the criteria for two conditions. And since the formula do have a nested "IF" function that tests for two conditions, you must consider how to phrase this properly with an Acrobat JavaScript.script.

 

If the first condition "((E14+E25)-C27)>0" is greater than the value of "0",  then the total value for this equation is true and "C27+C29+((E14+E25)-C27)" would become the desired total value.

 

Else, if the second test for that same condition above "((E14+E25)-C27),"0")"  equals "0", then the test for first condition fails and therefore and instructs the formula to return a total value for  "C27+C29" in the target field (or the cell that contains the formula in the case of MS Excel where this opration is performed).

 

If we want to express the equivalent syntax of a  MS Excel VBA Macro script with Acrobat JavaScript, refer to my example below. 

 

In this example, instead of using different text fieldnames I will declare all of my variables with the same cell names that you were using in the MS Excel invoice that your boss gave you. This will make it all easier to follow.

 

 

/* IN ACROBAT JAVASCRIPT YOU NEED TO DECLARE YOUR VARIABLES FIRST. 
IN THIS CASE, GET THE FIELD VALUES AND ESTABLISH THE DESIRED MATHEMATICAL FORMULA */

//Get all of the field NUMBER values that will be used in the equation NOT TEXT STRINGS

var c27 = Number(this.getField("C27").value);
var c29 = Number(this.getField("C29").value);
var e14 = Number(this.getField("E14").value);
var e25 = Number(this.getField("E25").value);


/* 
Now express an IF/ELSE IF condition with a custom calculation script to evaluate the totals of both conditions which will result in the total value that will display in the event target field
*/

if ( ((e14 + e25) - c27) > 0) { event.value = c27 + c29 + ( (e14 + e25) - c27 ) }

else if ( ((e14 + e25) - c27) <= 0) event.value = c27 + c29 ;

 

 

  • Compare the script used above with slide below that contains the original MS Excel VBA Macro:

 

FROMEXCEL2.pngexpand image

 

 

  •  And below, notice the total value which is the same result from the MS Excel spreadsheet using Acrobat JavaScript in my PDF document:

 

  • FROMeXCEL.pngexpand image

View solution in original post

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 ,
Jan 08, 2021 Jan 08, 2021

You're not that close. You can't just copy and paste the syntax from one language to another. Then have quite different ways of doing things. Also, JS is case sensitive, so there's no such thing as getfield, it's getField.

 

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 ,
Jan 09, 2021 Jan 09, 2021

++ Adding to Try67's guidance,

 

FROM THE EXCEL FORMULA: =+C27+C29+(IF(((E14+E25)-C27)>0,((E14+E25)-C27),"0"))

 

The total value of this formula is based on the SUM for this calculation: C27+C29+((E14+E25)-C27) as long as it meets the criteria for two conditions. And since the formula do have a nested "IF" function that tests for two conditions, you must consider how to phrase this properly with an Acrobat JavaScript.script.

 

If the first condition "((E14+E25)-C27)>0" is greater than the value of "0",  then the total value for this equation is true and "C27+C29+((E14+E25)-C27)" would become the desired total value.

 

Else, if the second test for that same condition above "((E14+E25)-C27),"0")"  equals "0", then the test for first condition fails and therefore and instructs the formula to return a total value for  "C27+C29" in the target field (or the cell that contains the formula in the case of MS Excel where this opration is performed).

 

If we want to express the equivalent syntax of a  MS Excel VBA Macro script with Acrobat JavaScript, refer to my example below. 

 

In this example, instead of using different text fieldnames I will declare all of my variables with the same cell names that you were using in the MS Excel invoice that your boss gave you. This will make it all easier to follow.

 

 

/* IN ACROBAT JAVASCRIPT YOU NEED TO DECLARE YOUR VARIABLES FIRST. 
IN THIS CASE, GET THE FIELD VALUES AND ESTABLISH THE DESIRED MATHEMATICAL FORMULA */

//Get all of the field NUMBER values that will be used in the equation NOT TEXT STRINGS

var c27 = Number(this.getField("C27").value);
var c29 = Number(this.getField("C29").value);
var e14 = Number(this.getField("E14").value);
var e25 = Number(this.getField("E25").value);


/* 
Now express an IF/ELSE IF condition with a custom calculation script to evaluate the totals of both conditions which will result in the total value that will display in the event target field
*/

if ( ((e14 + e25) - c27) > 0) { event.value = c27 + c29 + ( (e14 + e25) - c27 ) }

else if ( ((e14 + e25) - c27) <= 0) event.value = c27 + c29 ;

 

 

  • Compare the script used above with slide below that contains the original MS Excel VBA Macro:

 

FROMEXCEL2.pngexpand image

 

 

  •  And below, notice the total value which is the same result from the MS Excel spreadsheet using Acrobat JavaScript in my PDF document:

 

  • FROMeXCEL.pngexpand image
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
New Here ,
Jan 11, 2021 Jan 11, 2021

Thank you so much.  That worked out perfectly.

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 ,
Jan 11, 2021 Jan 11, 2021
LATEST

You're welcome.

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