10 Replies Latest reply on Feb 9, 2017 3:05 PM by neophyte1978

# need help changing excel formula to javascript for medication order form

Hi, I'm trying to convert this excel formula to javascript:

=ROUND((0.007184*(E6)^0.725*(G6)^0.425),3-LEN(INT(0.007184*(E6)^0.725*(G6)^0.425)))

where E6 is a pdf field named Ht

G6 is a pdf field named Wt

any help would be very much appreciated.

• ###### 1. Re: need help changing excel formula to javascript for medication order form

All of the methods you need to use are described here:

General tutorial about calculations in Acrobat: https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations

• ###### 2. Re: need help changing excel formula to javascript for medication order form

The problem with translating such an Excel formula is that the round function is different in JavaScript. I would solve the problem like this (the following assumes that there are never any negative results):

```var E6 = this.getField("Ht").value;
var G6 = this.getField("Wt").value;
var v = Math.pow(0.007184 * E6, 0.725) * Math.pow( G6, 0.425);
if (v < 10.0) {
// round to two decimal places
event.value = Math.round(v * 100) / 100;
}
else if (v < 100.0) {
// round to one decimal
event.value = Math.round(v * 10) / 10;
}
else {
// round to the nearest integer
event.value = Math.round(v);
}
```

You would use this as your custom calculation script in your result field.

• ###### 4. Re: need help changing excel formula to javascript for medication order form

Yes the rounding was where I had trouble. This worked perfectly! Thank you!

• ###### 5. Re: need help changing excel formula to javascript for medication order form

I'm trying to take this a step further. I have a field named Dose1 with the following code:

var v = this.getField("BSA");

event.value = Math.round(v.value * 125);

I then have a field named AdjDose1 that defaults to 100. I want to make it so that when any number below 100 is entered, it takes that percentage of the result of Dose1 and displays the new result in Dose1 field. For example:

Dose1 displays 250.

Dose1 now displays 200.

Is it possible to do this?

Thank you very much indeed in advance.

• ###### 6. Re: need help changing excel formula to javascript for medication order form

If this is for human medications, you really need do better. At minimum you need to validate the input values and the calculated results.

• ###### 7. Re: need help changing excel formula to javascript for medication order form

Hi, yes I should clarify. I'm a pharmacist with 15 years of experience. I know I titled the thread Medication Order Form, however this form will be used as a tool to "double-check" separate, handwritten medication order forms. Nonetheless, the input values and custom calculations will be validated by two pharmacists other than myself.

• ###### 8. Re: need help changing excel formula to javascript for medication order form

OK. You can change the calculation script to:

var v = this.getField("BSA");

event.value = Math.round(125 * v.value * AdjDose1 / 100);

} else {

event.value = Math.round(125 * v.value);

}

• ###### 9. Re: need help changing excel formula to javascript for medication order form

works perfectly. Thank You!!

• ###### 10. Re: need help changing excel formula to javascript for medication order form

I have just ONE last calculation based on the above I'm hoping you could help with.

I need Dose1 to be result of 1.4 * BSA. If the result is >2, I need it to display 2. If less than 2, I need the result to be rounded to two decimal places but no hanging zeros (ex: 1.9 but not 1.90). Then as above, if any value less than 100 is entered into AdjDose1, that percentage of Dose1 is displayed in Dose1. For example:

when BSA = 1.35

Dose1 = 1.4 * 1.35

Dose1 = 1.89

Dose1 now displays 0.95

when BSA = 1.7

Dose1 = 1.4 * 1.7

Dose1 = 2