This code:
- Creates top and bottom two box scores for questions with a five point scale.
- Creates top and bottom two and three box scores for questions with seven point scales.
- Edits the Label for each value so that it includes the Value in the code frame (e.g., replaces Strongly Agree with Strongly Agree (5).)
- Places all the modified tables in a folder in the Report tree.
This example can be run using C:\Program Files\Q\Examples\Pasta.Q (this may be located on a different place on your computer depending upon how Q was installed). This project contains multiple 7-point scales questions but none with 5-point scales.
var new_group = project.report.appendGroup();
new_group.name = "Top Boxes";
// All data files.
for (var data_i in project.dataFiles) {
var data = project.dataFiles[data_i];
// All questions in this data file.
for (var q_i in data.questions) {
var q = data.questions[q_i];
// But only non-hidden Pick One (and Multi) questions.
if ((q.questionType == "Pick One" || q.questionType == "Pick One - Multi") && !q.isHidden) {
// Don't consider missing values.
var values = q.uniqueValues;
// Search backwards through the list, because every time we remove a value,
// the list gets smaller.
for (var value_i = values.length - 1; value_i >= 0; value_i --) {
if (q.valueAttributes.getIsMissingData(values[value_i]))
values.splice(value_i, 1);
}
//alert(q.name + ": " + values.length + " values");
// Only questions with 5 or 7 non-missing values.
if (values.length == 5 || values.length == 7) {
// Prefix each value label with its value in brackets.
for (var value_i in values) {
var value = values[value_i];
q.valueAttributes.setLabel(value, "(" + value + ") " + q.valueAttributes.getLabel(value));
}
// Add bottom/top two boxes.
var bottom_label = "Bottom 2 Box ("+values[0]+","+values[1]+")";
var top_label = "Top 2 Box ("+values[values.length - 2]+","+values[values.length - 1]+")";
try {
q.dataReduction.createNET([q.valueAttributes.getLabel(values[0]),
q.valueAttributes.getLabel(values[1])],
bottom_label);
q.dataReduction.createNET([q.valueAttributes.getLabel(values[values.length - 2]),
q.valueAttributes.getLabel(values[values.length - 1])],
top_label);
} catch (error) {
log("Ignored question " + q.name + " because the top 2/bottom 2 box values could not be found in its dataReduction.");
continue; // skip this question
}
q.dataReduction.moveAfter(bottom_label, null);
q.dataReduction.moveAfter(top_label, null);
// Add bottom/top three boxes.
if (values.length == 7) {
bottom_label = "Bottom 3 Box ("+values[0]+","+values[1]+","+values[2]+")";
top_label = "Top 3 Box ("+values[values.length - 3]+","+values[values.length - 2]+","+values[values.length - 1]+")";
q.dataReduction.createNET([q.valueAttributes.getLabel(values[0]),
q.valueAttributes.getLabel(values[1]),
q.valueAttributes.getLabel(values[2])],
bottom_label);
q.dataReduction.createNET([q.valueAttributes.getLabel(values[values.length - 3]),
q.valueAttributes.getLabel(values[values.length - 2]),
q.valueAttributes.getLabel(values[values.length - 1])],
top_label);
q.dataReduction.moveAfter(bottom_label, null);
q.dataReduction.moveAfter(top_label, null);
}
// Show it in a new table.
var t = new_group.appendTable();
t.primary = q;
}
}
}
}
log("All done");log("All done");
Next