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

need help changing excel formula to javascript for medication order form

Community Beginner ,
Jul 21, 2016 Jul 21, 2016

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.

TOPICS
Acrobat SDK and JavaScript
614
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 ,
Jul 21, 2016 Jul 21, 2016

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

JavaScript Math Reference

JavaScript String Reference

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

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 Beginner ,
Jul 22, 2016 Jul 22, 2016

Very helpful indeed! 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
Community Expert ,
Jul 21, 2016 Jul 21, 2016

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.

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 Beginner ,
Jul 22, 2016 Jul 22, 2016

Yes the rounding was where I had trouble. This worked perfectly! 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
Community Beginner ,
Sep 22, 2016 Sep 22, 2016

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.

80 is entered into AdjDose1.

Dose1 now displays 200.

Is it possible to do this?

Thank you very much indeed in advance.

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 ,
Sep 22, 2016 Sep 22, 2016

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

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 Beginner ,
Sep 23, 2016 Sep 23, 2016

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.

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 ,
Sep 23, 2016 Sep 23, 2016

OK. You can change the calculation script to:

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

var AdjDose1 = getField("AdjDose1").value;

if (AdjDose1 < 100) {

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

} else {

    event.value = Math.round(125 * v.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
Community Beginner ,
Sep 28, 2016 Sep 28, 2016

works perfectly. 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
Community Beginner ,
Feb 09, 2017 Feb 09, 2017
LATEST

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

AdjDose1 = 50

Dose1 now displays 0.95

when BSA = 1.7

Dose1 = 1.4 * 1.7

Dose1 = 2

AdjDose1 = 50

Dose1 now displays 1

Many thanks in advance!

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