Skip to main content
chrisg90226064
Participating Frequently
May 21, 2019 11:07 AM UTC
Answered

Adobe Fillable form drop down menu formula help

Hi,

I am having problem with creating a fillable form formula to work in adobe. I have created a drop down menu titled "No.ofMeals", values being 3,4 and 5. I want the form to divide the field "ProteinGrams" by whats selected from the drop down - so divided by either 3,4,5. Thanks

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
May 21, 2019 12:16 PM UTC

As the custom calculation script of the field where you want to show the result, enter this:

event.value = Number(this.getField("ProteinGrams").valueAsString) / Number(this.getField("No.ofMeals").valueAsString);

8 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 21, 2019 12:16 PM UTC

As the custom calculation script of the field where you want to show the result, enter this:

event.value = Number(this.getField("ProteinGrams").valueAsString) / Number(this.getField("No.ofMeals").valueAsString);

Translate
chrisg90226064
Participating Frequently
May 21, 2019 1:11 PM UTC

Hi try67,

thanks for your response. the formula you provided works but is not quite 100% right yet.

to give you a scenario I have created a PDF Nutrition form that when you enter your weight (lbs) into the "Weight" field it will calculate a Total Calories sum in the "Total Calories" Field (Formula is weight in lbs x 10 kcal).

Now we have the total Calories field filled in based on selection of weight e.g. 150 lbs x 10 = 1500 calories.

The form then calculates %'s of food into the following fields -

"Calories Protein" =  (1500 x 0.40) this then gives the "GramsProtein" (CaloriesProtein/4)

"Calories Carbohydrates" = (1500 x 025) this then gives us "GramsCarbs" (CaloriesCarbs/4)

"CaloriesFats" = (1500 x 0.35) this then gives us "GramsFats" (CaloriesFats/9)

The percentages of food from the total calories calculated are 40% Protein, 25% Carbs, 35% Fats.

I have managed to sort all this bit of the form perfectly myself.

The bit I am now struggling with is to divide the grams of food based on the dropdown menu "No.ofMeals" selection. - 3,4, or 5.

There are 5 individual meals boxes:

Meal 1

Meal 2

Meal 3

Meal 4

Meal 5

your suggestion divides all equally so the form does this:

So if 150 lbs x 10 kcal = 1500 kcal

Protein 150 grams

Carbs 93.75

Fats 58g.

Your calculation "works" except if I was to chose 3 meals 50 grams of protein shows in every box:

Meal 1: 50

Meal 2: 50

Meal 3: 50

Meal 4: 50

Meal 5: 50

If I now select 3 meals on the drop down menu 50grams shows in all 5 meal boxes rather than just 3.

the total grams of protein shows now 250g grams which doesn't make sense / wrong calculation as the total grams of protein is only 150.

I need the form to calculate the meals based on selection of "No.ofMeals" from the drop down menu.

So if 3 meals selected the form only divides Protein by 3 to show protein in meals 1, 2 and 3 boxes.

If the user selects 4 meals I need the form to divide  protein by 4 to show protein grams in meals 1,2,3 and 4. only

and obviously is user selects 5 meals the form shows "ProteinGrams" / 5 to show calculation in all 5 boxes.

Hope all makes sense. Attached a photos to help.

Translate
try67
Community Expert
Community Expert
May 21, 2019 1:19 PM UTC

OK, that's not quite what you asked for before...

There are various ways of doing it. Maybe the easiest is to add a condition to the 4th and 5th fields to only show the result if 4 or 5 are selected. Otherwise, keep them empty.

Something like this for field 4:

var numMeals = Number(this.getField("No.ofMeals").valueAsString);

if (numMeals==4 || numMeals==5) event.value = Number(this.getField("ProteinGrams").valueAsString) / numMeals;

else event.value = "";

And for field 5:

var numMeals = Number(this.getField("No.ofMeals").valueAsString);

if (numMeals==5) event.value = Number(this.getField("ProteinGrams").valueAsString) / numMeals;

else event.value = "";

The rest can remain the same.

Translate