Skip to main content
March 30, 2016
Question

I need Help converting an Excel IF formula to javascript

  • March 30, 2016
  • 2 replies
  • 1024 views

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)

This topic has been closed for replies.

2 replies

Inspiring
March 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.

March 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;

 

})();

 

Inspiring
March 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;

})();

Inspiring
March 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;

}

March 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!