Changing the Order of Rows or Columns for All Tables that Share the Same Code Frame

From Q
Jump to navigation Jump to search

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.

The QScript below will perform the same sorting for all questions in your project that share a common code frame. This example can be applied to any project by changing the labels that are specified at the top of the script in the JavaScript variable called label_order. To see this script in action you should use the following example:

  1. Open the example project C:\Program Files\Q\Examples\Phone Tidied.Q (this may be located on a different place on your computer depending upon how Q was installed).
  2. Select the question Q23. Attitudes (categories) in the Blue drop-down menu.
  3. Press the Save Table button.
  4. Right-click on the new table and select Duplicate Question.
  5. Follow the instructions here to run the QScript.
  6. Notice that the categories have been reordered.

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.

// 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);
	}
}

See also