In some cases, your data file will contain a number of questions that share the same code frame, for example a five-point rating. Often you can combine these questions into a Pick One - Multi question so that you can show all of the data on the same table, but in some cases you may wish to show each question separately. If the rows of the tables need to be re-ordered then this would require you to drag-and drop the rows of each table according to the desired order.
This article describes how to use QScript to perform the same sorting for all questions in your project that share a common code frame...
... such as reversing the order of the column categories:
Requirements
This example uses Q23. Attitudes (categories) from the project called "Phone Tidied.Q" in Q's example projects. To access this:
- Select File > Open > Example Project.
- Open Phone Tidied.Q
To ensure you have 2 versions of your question as tables to run the script on, please begin by doing the following:
- Create a table of Q23 Attitudes (categories).
- Press the + Duplicate button.
- Right-click on the new table and select Duplicate Question > [rename] > OK.
Method
1. Go to Automate > Open QScript (Macro) Editor.
2. Paste the below into the editor:
// Specify the ordering of the labels.
// The first label will appear at the top of the table or in the leftmost column.
var label_order = ['Strongly disagree', 'Disagree a little', 'Neither','Agree a little','Strongly agree'];
var label_codes = [100,200,300,400,500];
// Get all of the questions in the project
var questions = project.dataFiles[0].getQuestionsByName('');
// This function checks to see if a question contains all of
// the specified labels, and returns false if one of the
// labels in 'label_array' does not exist in the Values for
// this question.
function questionContainsAllLabels(question,label_array) {
var has_all_labels = true;
for (var j = 0; j < label_array.length; j++) {
if (question.dataReduction.contains(label_array[j]) === null) {
has_all_labels = false;
break;
}
}
return(has_all_labels);
}
// This function sorts the labels of a question according to
// a given order
function sortLabels(question, label_array) {
question.dataReduction.moveAfter(label_array[0],null);
for (var j = 1; j < label_array.length; j++) {
question.dataReduction.moveAfter(label_array[j],label_array[j-1]);
}
}
// Recode the question according to the arrays of input labels and values
function recodeQuestionByLabelValueArray(question, label_array, value_array) {
var current_value_attributes = question.valueAttributes;
var cur_val;
for (var j = 0; j < label_array.length; j++) {
cur_val = current_value_attributes.getSourceValueByLabel(label_array[j]);
if (cur_val != null) {
current_value_attributes.setValue(cur_val, value_array[j]);
}
}
}
// Loop over all of the questions in the project.
// If the question contains the required labels
// then sort the rows/columns for this question.
for (var j = 0; j < questions.length; j++) {
if (questionContainsAllLabels(questions[j], label_order)) {
sortLabels(questions[j],label_order);
recodeQuestionByLabelValueArray(questions[j], label_order, label_codes);
}
}
3. Update the the JavaScript variable called label_order
at the top of the script with the name of the labels you want to reorder.
4. OPTIONAL: This script also recodes the question according to an input array called label_codes
. To prevent the recoding, remove the line recodeQuestionByLabelValueArray(questions[j], label_order, label_codes);
in the main loop.
5. Press the Play button and both tables will now have their categories reordered.
Next
How to Create a Custom QScript
How to Work with Variables via QScript