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

I need Help converting an Excel IF formula to javascript

Guest
Mar 30, 2016 Mar 30, 2016

I have a simple excel IF formula below that I need help converting to java script. Any help would be greatly appreciated. Thank you!

=IF(G37>G39,G39,G37)

TOPICS
Acrobat SDK and JavaScript , Windows
908
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
LEGEND ,
Mar 30, 2016 Mar 30, 2016

Have you looked a the MDN JavaScript Reference?

Do you have any experience with Object Programing?

There are two ways to program an If statement.

(logical statement) ? {block of code for true} : {block of code for false};

or

if(logical statement) {

// block of code for true;

} else {

// block of code for false;

}

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
Guest
Mar 30, 2016 Mar 30, 2016

I looked into the MDN JavaScript Reference but I had a hard time finding what I needed. I have no experience with object programming.  I appreciate the help! Thank you!

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
LEGEND ,
Mar 30, 2016 Mar 30, 2016

The custom calculation script for the field could be:

(function () {

    // Get the field values, as numbers

    var G37 = +getField("G37").value;

    var G39 = +getField("G39").value;

    // Set this field's value

    event.value = G37 > G39 ? G39 : G37;

})();

but replace "G37" and "G39" in the getField statements with the actual field 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
Guest
Mar 30, 2016 Mar 30, 2016

Below is what I have entered into the JavaScript editor. I'm still getting a Syntax Error. Can help me find out what I'm doing wrong?

 

 

(function () {

 

    // Get the field values, as numbers

 

    var Eligible for ReimbursementRow33.value = +getField("Eligible for ReimbursementRow33").value;

 

    var Allowed.value = +getField("Allowed").value;

 

    // Set this field's value

 

    event.value = +getField("Eligible for ReimbursementRow33").value > +getField("Allowed").value? +getField("Allowed").value: +getField("Eligible for ReimbursementRow33").value;

 

})();

 

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
LEGEND ,
Mar 30, 2016 Mar 30, 2016

Variable names cannot have spaces in them. There are other problems too, but this should work:

(function () {

    // Get the field values, as numbers

    var Eligible_for_Reimbursement = +getField("Eligible for ReimbursementRow33").value;

    var Allowed = +getField("Allowed").value;

    // Set this field's value

    event.value = Eligible_for_Reimbursement > Allowed ? Allowed : Eligible_for_Reimbursement;

})();

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
Guest
Mar 30, 2016 Mar 30, 2016

I have no errors pop up when I enter that java script in the custom calculation script. The only problem I do have is when I preview the report, the cell where I have the formula shows no value calculated. Is there something I am forgetting to do? Thank you for the help so far, I really appreciate it!

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
LEGEND ,
Mar 30, 2016 Mar 30, 2016

Try saving the document, closing Acrobat, and then open the document again. Confirm that the script is still there, and after changing some of the input fields, check the JavaScript console (Ctrl+J) for any errors.

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
Guest
Mar 30, 2016 Mar 30, 2016

This is what I get when I press Ctrl+J:

Acrobat EScript Built-in Functions Version 11.0
Acrobat SOAP 11.0

TypeError: getField("Eligible for ReimbursementRow33") is null
7:Field:Calculate
TypeError: getField("Eligible for ReimbursementRow33") is null
7:Field:Calculate
TypeError: getField("Eligible for ReimbursementRow33") is null
7:Field:Calculate
TypeError: getField("Eligible for ReimbursementRow33") is null
7:Field:Calculate

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 ,
Mar 30, 2016 Mar 30, 2016

That means there's no such field with that name. You need to make sure you

enter it EXACTLY as it appears in the field's Properties window, including

upper/lower-case letters, spaces, etc.

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
Guest
Mar 30, 2016 Mar 30, 2016
LATEST

Got it! Thank you for everything!

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