Do you need to perform basic math within a response? You can do so using our custom scripting. Because our scripting language is based on Lua any of the operators that are available in Lua are available in our scripting language, and thus, in our surveys! Learn more about Lua arithmetic operators.
Getting Started: Get Familiar with getvalue and setvalue
getvalue and setvalue are your friends when performing math in surveys; get familiar with these functions before you jump into doing math.
getvalue* returns the value (reporting value) of an answered question (or null, if not answered). Chances are, when performing math in a survey, you'll want to pull one or more values from a question to use in your arithmetic. For single answer questions, a single value is returned. If you are looking to use the value from a more complex question this can get quite a bit trickier. Check out the getvalue function documentation to learn more.
*getvalue can be used to pull data from questions on previous pages only.
setvalue** populates the response value of the given question. Chances are, when performing math in a survey, you'll want to use the result elsewhere in the survey; using setvalue you can store this data in a hidden value or other question for later use or reporting. The setvalue portion of the script below can be used to populate a hidden value, textbox, essay, radio button, drop-down menu, slider and image choice question. If you wish to populate other questions this can get quite a bit trickier. Check out the setvalue function documentation to learn more.
**setvalue can be used to set data in questions on any page.
Let's say, for example, you want to multiply the number of shirts the customer orders (entered in a Textbox question in a survey) by the price so you can get a total amount due.
Check it out in an example survey!
Add a survey with this script to your account.
In the below script we pull the value of the number of shirts Textbox multiply it by the price and set the value of the Total Amount Due Textbox question.
numberofshirts = getvalue(5) --Get value of first textbox shirtprice = 15 --Replace 15 with amount of shirt totalamountdue = (numberofshirts * shirtprice) --[[Multiply the first textbox times the shirt amount--]] setvalue(6,totalamountdue) --Change 6 to ID of Hidden Value or Textbox