This script is intended to limit the number of rows participants can select for each column, in a Single Choice or Multi Choice Grid question. When participants exceed the maximum number of rows and attempt to proceed in the survey, they receive an error message. The script lets you specify a range for the number of options allowed for each column, or you can specify an exact number by setting the minimum and maximum values to be the same number. This script works on both standard and visual Single and Multi Choice Grid questions.
Note: If you are using this script on a Single Choice Grid (standard or visual), participants will not be able to deselect erroneously selected answers.
Examples
- You have a Single Choice Grid and want to allow only between 1 and 3 options per column.
- You have a Multi Choice Grid and want to allow exactly 4 options per column.
Detailed Steps
- Create your Grid question as required.
- In the Properties section of your Grid question, ensure Required Row Count is set to your desired minimum.
- Copy the script below into Notepad or any other text editor (e.g. Notepad++, Textpad).
- Change Q_NAME to the name of your Grid question.
- Change min to the minimum number of options you want for each column.
- Using example 1 above, this line should look like "int MIN_COUNT = 1;".
- Using example 2 above, this line should look like "int MIN_COUNT = 4;".
- Change max to the maximum number of options you want for each column.
- Using example 1 above, this line should look like "int MAX_COUNT = 3;".
- Using example 2 above, this line should look like "int MAX_COUNT = 4;".
- Insert the completed script into the Scripting section of your grid question by doing the following:
- Select your grid question in the Questionnaire Tree.
- In the Scripting section of the grid, click Edit Scripts.
- Paste the script into the On Validate tab.
- Click Save.
- Validate and test your survey.
Note: If you use a Single Choice question type, you need to have an amount of rows that is divisible by the number selected. Otherwise you will get an error message due to “range_err” found in the ERR_MSG question.
Script
/* Script Name: How do I ensure only a certain number of rows per column are selected? (Power Surveys) To ensure your script works as expected, always copy the original script from the script library found in the support portal. Failure to use the most up to date script from the script library may cause unexpected results. */GridSequence gs = Q_NAME; int MIN_COUNT = min; int MAX_COUNT = max; if(!IsATR) { ShowQuestionAction row = ((ShowQuestionAction) gs.WorkflowSteps[0]); ArrayList shown = new ArrayList(); foreach(Answer ans in row.GetVisibleAnswers()) shown.Add(ans.OrderInParent); int [] col_count = new int[((ChoiceQuestion)row.ChoiceQuestion).Answers.Count]; foreach(ShowQuestionAction sqa in gs.WorkflowSteps){ if(sqa.Visible){ ChoiceQuestion cq = (ChoiceQuestion) sqa.ChoiceQuestion; foreach (Answer a in cq.GetSelectedAnswers()) col_count[a.OrderInParent]++; } } for (int i=0; i < col_count.Length; i++){ if(shown.Contains(i)){ int count = col_count[i]; if (MIN_COUNT == MAX_COUNT && count != MIN_COUNT){ NewError(String.Format("Please select {0} options per column.", MIN_COUNT)); return; } else if (!Between(MIN_COUNT,MAX_COUNT,count)){ NewError(String.Format("Please select between {0} and {1} options per column.", MIN_COUNT, MAX_COUNT)); return; } } } }
More Information
For more information, see our webhelp topics:
Comments
0 comments
Article is closed for comments.