This script will count the number of rows selected in a Grid question, for specified columns only. The final count will be stored in a numeric question, which can then be used in a conditional sequence to ask follow up questions if necessary.
This script will work for the following question types:
- Single Choice Grids (standard and visual)
- Multi Choice Grids (standard and visual)
- Scale Slider Grids
- Single Choice Card Sorts: rows=cards, columns=categories
- Single Choice Magnetic Boards: rows=magnets, columns=boards
- Multi Choice Magnetic Boards: rows=magnets, columns=boards
The script will NOT work for these grid questions:
- Allocation Grids
- Allocation Sliders
Example
You ask a participant to select all the brands of devices they own at home. You have a Multi Choice Grid question where the brands are set up as the rows (e.g Apple, Sony, Microsoft etc.) and the device types are set up as the columns (e.g. computer, television, mobile phone, tablet etc).
You then want to ask a follow up question to all participants who have more than one brand of television (i.e. more than one row selected for the television column).
Detailed Steps
- Create your Grid question as required.
- Create a Numeric question that will be used to store the final count of the number of rows selected.
- Hide the question so respondents do not see it.
- 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 Q_NAME_COUNT to the name of your Numeric question.
- Change {LIST} to the list of column indexes. The script will count the number of rows selected for these column indexes only.
Each number in the list must be separated by a comma and is 1-based e.g. if you want to specify columns 2,3 and 7, the list will become {2,3,7}. Ensure there are curly brackets on either side of the list of numbers. - Copy the completed script from Notepad or whichever text editor you are using.
- Insert the completed script into the Script Window in the On Exit tab section of your Grid question.
- Click Save.
Note: If you need to set up a follow-up condition, you can simply reference the Numeric question in the conditional sequence and create the necessary condition. - Validate and test your survey.
Script
/* Script Name: How do I record the number of rows selected for specific columns in a grid? 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; VerbatimQuestion vq = Q_NAME_COUNT; int [] col_arr = {LIST}; /******* DO NOT MODIFY BELOW *******/ bool [] row_arr = new bool [gs.WorkflowSteps.Count]; for (int i=0; i<(row_arr.Length); i++) { row_arr[i]=false; } foreach (ShowQuestionAction sqa in gs.WorkflowSteps){ ChoiceQuestion cq_row = (ChoiceQuestion) sqa.ChoiceQuestion; int rowPos = sqa.OrderInParent; if (sqa.Visible){ foreach (Answer ans in cq_row.Answers){ int ansPos = ans.OrderInParent+1; if ( ans.Selected && Array.IndexOf(col_arr, ansPos)>=0 ) { row_arr[rowPos]=true; break; } } } } int count=0; for (int j=0; j<(row_arr.Length); j++) { if ( row_arr[j] ) count++; } Set (vq,count);
More Information
For more information, see our webhelp topics:
Comments
0 comments
Article is closed for comments.