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:
Requirements
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"
Method
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[0];
var question = data_file.getQuestionByName("Q2. Gender");
var variable = question.variables[0];
// Create new copy of variable using JavaScript
var new_variable = data_file.newJavaScriptVariable(variable.name, false, preventDuplicateVariableName(data_file, variable.name), variable.label + " - Complete");
new_variable.question.questionType = "Pick One";
data_file.moveAfter([new_variable], variable);
// Copy existing values labels into new variable
copyValueAttributesForVariable(variable, 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;
project.report.setSelectedRaw([new_table]);
4. Press the Play button.
5. The script then does the following:
- A new copy of the Gender question is created using a JavaScript variable.
- 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).
Next
How to Create a Custom QScript