Skip to main content
stevene30137170
Participating Frequently
November 30, 2017
Answered

I'm trying to add some calculations that take two variables to determine a designated output into a form.

  • November 30, 2017
  • 1 reply
  • 2746 views

Can I set up a formula or script that runs off of a lookup table to complete these? For example. From the first drop-down you select the color blue, the second drop-down you select 25, then in the third box it auto fills "apple". Like the table below, the result is based on checking the first and second columns to determine the output in the third.

ColorNumberWord
Blue25Apple
Red16Orange
Blue16Peach
Red25Pineapple

I'm thinking that when I do the third field I'd use a loop script for the calculation.

(I don't know any javascript yet)

if (Box Color = Blue)

        if (Box Number =25)

           then Box result = Word

        else if (Box Number =16)

           then Box result = Peach

else if (Box Color = Red)

        if (Box Number = 25)

           then Box result = Pineapple

        else if (Box Number = 16)

           then Box result = Orange

else Box result = " "

Is this along the right lines? Or is there a better way? Considering the data tables I'm using are more extensive than the example above.

This topic has been closed for replies.
Correct answer Thom Parker

Since this is not a calculation, you are correct that the values need to be hard coded.  You could create an "if" coding structure such as one you have above, but my preferred solution is to organize the data into a structure where the two choices automatically select for the correct value.

For Example:

var valueList = {"Blue":{"25":"Apple","16":"Peach"},

                         ""Red":{"25":"Pineapple","16":"Orange"}

}

Then value selection is just a matter of selecting from the list.

var email = valueList[ this.getField("DropDown1") ][ this.getField("DropDown2") ];

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
November 30, 2017

Since this is not a calculation, you are correct that the values need to be hard coded.  You could create an "if" coding structure such as one you have above, but my preferred solution is to organize the data into a structure where the two choices automatically select for the correct value.

For Example:

var valueList = {"Blue":{"25":"Apple","16":"Peach"},

                         ""Red":{"25":"Pineapple","16":"Orange"}

}

Then value selection is just a matter of selecting from the list.

var email = valueList[ this.getField("DropDown1") ][ this.getField("DropDown2") ];

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
stevene30137170
Participating Frequently
December 1, 2017

That definitely looks much better than what I had in mind. I was just informed that I need to account for another variable that I wasn't expecting.

Blue - 25 - Rectangle - Apple

Blue - 25 - Square - Grape

For the primary or first value there are only two options, for the second there are 6 but the third has about 25. So could I write it the same?

var valueList = {"Blue":{"25":{"Rectangle":"Apple","Square":"Grape","Circle":"Starfruit"},"16":{...

then selecting from the list

var email = valueList[this.getField("DropDown1")][this.getField("DropDown2")][this.getField("DropDown3")];

Would that work? I'm going to give it a shot in the morning. Hopefully my understanding of programming and how lists work isn't failing me.

Thom Parker
Community Expert
Community Expert
December 1, 2017

As long as the brackets all line up correctly you can nest it as much as you'd like. If you format it well, it makes it easy to add, remove, and modify entries.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often