Lua Scripting Resources

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language.


Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. Read-only scripts will continue to function; you will just be prevented from editing. The exact date on this is to be determined; we will send notifications well ahead of time.


 Go to our Legacy Scripting Documentation.

While SurveyGizmo is one of the most flexible survey tools around, we get requests for customizations that are not available out of the box. This is where JavaScript and Custom Scripting can save the day. If you have scripting chops you can use the JavaScript action or the Custom Scripting action to achieve the survey of your dreams.


[article("bodfy")]

This function returns the value (reporting value) of an answered question (or null, if not answered). For single answer questions, a single value is returned. Multi-answer questions return an array. Grid and custom group questions return a multidimensional array.

ParametersDescriptionRequired
questionIDthe ID of the questiontrue

Examples

Single-Answer Questions

Many Alchemer questions collect and return a single answer when access via getvalue.

Questions with output that follow this pattern:

See the output for single-answer questions in an example survey.

This script prints the return to getvalue for single-select questions. The output will be the entered text for text fields or reporting value for the answer option.

print(getvalue(questionID))

This script prints the question title and the answer as a sentence.

print('The answer to question ' .. gettitle(questionID) .. ' is ' .. getvalue(questionID))

Multi-Answer Questions

There are a number of questions that collect a single vector of data. These questions all return an array. The option IDs are they keys and the answers are the values.

For example, the output from a Checkbox question looks like so:

Array ( [10005] => Bird [10006] => Other [10003] => Dog [10006-other] => iguana ) 

Questions with output that follow this pattern:

See the output for multi-answer questions in an example survey.

This script prints the entire array for getvalue for multi-answer questions

print(getvalue(questionID))

This script prints the answers to a checkbox or image select question as a sentence.

print('The answers to '..(gettitle(questionID))..(' are: ')..(implode(',',getvalue(questionID))))

This script prints the answers to a textbox list, slider list, or continuous sum as multiple sentences.

questionID = 2
optiontitles = getquestionoptions(questionID)
if isanswered(questionID) then
	for optionID,reportingvalue in pairs(getvalue(questionID)) do
        print('The answer to question: ')
 		print(gettitle(questionID) .. ' ' ..optiontitles[optionID] .. ' is ' .. reportingvalue)
	end
end

This script prints the ranks from a ranking type question

optiontitles = getquestionoptions(8)
for optionID,rank in pairs(getvalue(8)) do
 if rank then
	print('In question '..gettitle(8))
	print(', option '..optiontitles[optionID]..' is ranked '..rank..'<br>')
 end
end

You can also access individual answer options through the option ID.

question = getvalue(questionID)
option = question[optionID]

Grid Questions, Custom Group and Contact Form Questions

For grid questions, custom group and contact form questions, the return is a multidimensional array. The row IDs are the keys and the value for each row is an array of the columnID and reporting value pairs for each selected/entered answer.  

For example, the output from a Radio Button Grid looks like so:

Array ( [37] => Array ( [10128] => Never ) [39] => Array ( [10130] => Sometimes ) [38] => Array ( [10129] => Rarely ) [40] => Array ( [10131] => Often ) )

Questions with output that follow this pattern:

See the output for grid and custom questions in an example survey.

This script iterates through the return for a radio button grid, semantic differential, or dropdown menu list question and prints the answer for each row as a sentence. The first for loop iterates through each row of a grid question.--The second for loop iterates through all of the answers for each row.

for rowid,rowarray in pairs(getvalue(questionID))do 
 for optionID,reportingvalue in pairs(rowarray) do 
 print('Question ' .. gettitle(questionID))
 print(': The answer to row '.. gettitle(rowid)..' is '.. reportingvalue)
 print('<br>') 
 end
end

This script iterates through the return for a textbox grid, dropdown menu grid, or star ranking grid and prints the answer for each field as a sentence. The first for loop iterates through each row of a grid question. The second for loop iterates through all of the answers for each row.

columntitles = getquestionoptions(questionID)
for rowid,rowarray in pairs(getvalue(questionID)) do 
 for optionID,reportingvalue in pairs(rowarray) do 
 print('Question: '..gettitle(questionID))
 print(' Row: '..gettitle(rowid))
 print(' Column: '..gettitle(rowid))
 print(' Answer: '..reportingvalue..'<br>')
 end
end

This script prints the results for each row of a checkbox grid question.

for rowid,rowarray in pairs(getvalue(questionID)) do 
 print('Question '.. gettitle(questionID)..': The answers to row '..gettitle(rowid)..' are '..implode(',',rowarray)..'
')
end

You can also get the answer to an individual cell of a grid question using the below script.

question = getvalue(questionID)
field = question[rowid][optionID]

Custom Tables

Custom Tables return a multidimensional array. The return includes an array per question/column; each table row has a key and the value entered/selected is the value.

Array ( [3] => Array ( [TableRow-0] => Oil change,Tire repair [TableRow-1] => Tire repair,Brakes ) [4] => Array ( [TableRow-0] => Javier [TableRow-1] => John ) )

MaxDiff

MaxDiff questions return a multidimensional array. The below example output is for a MaxDiff question that shows 2 sets. The attributes array includes the attributes that were shown. Best and worst selections for each set are available via the best/worst keys.

Array(
	[1] => Array(
		[attributes] => Array(
			[1] => Strawberry
			[2] => Starfruit
			[3] => Apples
			[4] => Papaya
		) 
		[best] => Papaya
		[worst] => Strawberry
	) [2] => Array(
		[attributes] => Array(
			[1] => Kiwi
			[2] => Banana
			[3] => Mango
			[4] => Peach
		) 
		[best] => Banana
		[worst] => Mango
	)
)

Conjoint

Conjoint questions return a multidimensional array. The below example output is for a Conjoint question that shows 2 sets to the respondent with 4 cards each.  The attributes array shows the keys and values for each attribute that was shown on the card. The score will vary based on the conjoint type; below the score of 100 indicates that the card was selected. If the card was a none card this will be included in the card array.

Array(
	[1] => Array(
		[1] => Array(
			[attributes] => Array(
				[Price] => $300
				[Color] => Green
			)
			[score] => 100
		) [2] => Array(
			[attributes] => Array(
				[Price] => $250
				[Color] => Yellow
			)
			[score] => 0
		) [3] => Array(
			[attributes] => Array(
				[Price] => $399
				[Color] => Blue
			),
			[score] => 0
		) [4] => Array(
			[attributes] => Array() 
			[score] => 0
			[none] => 1
		)
	) [2] => Array(
		[1] => Array(
			[attributes] => Array(
				[Price] => $200
				[Color] => Green
			)
			[score] => 0
		) [2] => Array(
			[attributes] => Array() 
			[score] => 100
			[none] => 1
		) [3] => Array(
			[attributes] => Array(
				[Price] => $399
				[Color] => Yellow
			)
			[score] => 0
		) [4] => Array(
			[attributes] => Array(
				[Price] => $200
				[Color] => Blue
			) 
			[score] => 0
		)
	)
)

Other Examples

See also the following working examples for use of this function:

Net Promoter®, NPS®, NPS Prism®, and the NPS-related emoticons are registered trademarks of Bain & Company, Inc., Satmetrix Systems, Inc., and Fred Reichheld. Net Promoter Score℠ and Net Promoter System℠ are service marks of Bain & Company, Inc., Satmetrix Systems, Inc., and Fred Reichheld.