Sometimes you may have an option in a questionnaire which none of the respondents select, and if you are using an Excel-style file the category will not appear when you create a table for the question. You may wish to have a table which includes the missing category, and which shows 0%.
This article describes how to use QScript to add in an empty category to a question:
This example uses Q2. Gender from the project called Cola.Q in Q's example projects. To access this:
- Select File > Open > Example Project.
- Open Cola.Q
- Create a summary table using "Q2. Gender"
1. Select your table.
2. Go to Automate > Open QScript (Macro) Editor.
3. Paste the below into the editor:
includeWeb("QScript Utility Functions");
includeWeb("QScript Value Attributes Functions");
var data_file = project.dataFiles;
var question = data_file.getQuestionByName("Q2. Gender");
var variable = question.variables;
new_variable.question.questionType = "Pick One";
// Copy existing values labels into new variable
// Add a new category by setting a label for a value that does not exist yet.
var value_attributes = new_variable.valueAttributes;
value_attributes.setLabel(3, "Prefer not to answer");
// Show the results
var new_table = project.report.appendTable();
new_table.primary = new_variable.question;
4. Press the Play button.
5. The script then does the following:
- The original labels are copied into the new question.
- A new category is added to the new question by setting a label (Prefer not to answer) for a value which does not exist (3).
How to Create a Custom QScript
Article is closed for comments.