Skip to main content
Participant
May 27, 2016
Answered

Help converting a basic Excel-style formula into JavaScript

  • May 27, 2016
  • 1 reply
  • 582 views

I have a self populating table in an Acrobat document; figured out most of the simple equations but I have one slightly more complicated one that needs JavaScript and I know nada.

The reader inputs two values (a), (b)

and if I were to write the fomula in Excel I would write:

=a-(IF(b<20.5,b,20.5))+41.5

Any thoughts on how to translate this?  Thanks!

This topic has been closed for replies.
Correct answer George_Johnson

If you want this to be a custom calculation script for a field, it could be:

// Custom calculation script for text field

(function () {

    // Get the field values, as numbers

    var a = +getField("A").value;

    var b = +getField("B").value;

    // Set this field's value

    event.value = a - (b < 20.5 ? b : 20.5) + 41.5;

})();

but replace "A" and "B" in the getField statements with the actual field names you're using.

1 reply

George_JohnsonCorrect answer
Inspiring
May 27, 2016

If you want this to be a custom calculation script for a field, it could be:

// Custom calculation script for text field

(function () {

    // Get the field values, as numbers

    var a = +getField("A").value;

    var b = +getField("B").value;

    // Set this field's value

    event.value = a - (b < 20.5 ? b : 20.5) + 41.5;

})();

but replace "A" and "B" in the getField statements with the actual field names you're using.

Participant
May 27, 2016

Yes!  That did it!  I was close a couple of times earlier today, but didn't include the " ; "

Thank you!