QScript Functions for Unhiding Codes
Jump to navigation
Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!
// Unhide rows or columns in the selected table.
// Set rows = true to unhide rows, rows = false to unhide columns.
// Does not work for Banners, Date/Time, or Text data.
function unhideRowsOrColumns(rows = true) {
includeWeb("JavaScript Array Functions");
includeWeb("QScript Selection Functions");
const check_missing_types = ["Pick One", "Pick One - Multi", "Pick Any - Compact"];
const two_d_questions = ["Pick One - Multi", "Pick Any - Grid", "Number - Grid"];
// Organize information about missing values as an object
// Only applies for Pick One, Pick One - Multi, Pick Any - Compact
function getMissingCodes(question) {
let value_attributes = question.valueAttributes;
let data_reduction = question.dataReduction;
let unique_values = question.uniqueValues;
let question_type = question.questionType;
let missing_values = unique_values.filter(x => value_attributes.getIsMissingData(x))
let missing_obj = missing_values.map(function (x) {
return { code: x,
axis: (question_type == "Pick One - Multi" && data_reduction.transposed) ? "Columns" : "Rows",
label: value_attributes.getLabel(x),
question: question,
type: "missing"}
})
return missing_obj;
}
//Organize info about hidden codes as an object
function getHiddenCodes(question, rows) {
let is_2d = two_d_questions.indexOf(question.questionType) > -1;
let default_axis = rows ? "Rows" : "Columns";
let data_reduction = question.dataReduction;
return data_reduction.hiddenValues().map(function (x) { return { code: x,
axis: (is_2d ? x.axis : default_axis),
label: x.originalLabel,
question: question,
type: "hidden"};
});
}
// Prevent unhide on questions we can't process
function checkCanUnhide(question, rows) {
let question_type = question.questionType;
let axis_text = rows ? "rows" : "columns"
if (question.isBanner) {
log("To unhide " + axis_text
+ " from a Banner, select the Banner under Data Sets and choose DATA VALUES > Reset on the right.");
return false;
}
if (question_type.indexOf("Text") > -1) {
log("You cannot hide or unhide " + axis_text + " in Text data.");
return false;
}
if (question_type == "Date") {
log("You cannot hide or unhide " + axis_text
+ " from Date/Time data. To modify which dates are included, select the variable under Data Sets and then use GENERAL > Date/Time on the right.");
return false;
}
return true;
}
const user_selections = getAllUserSelections();
if (user_selections.selected_tables.length == 0 && user_selections.selected_questions.length == 0) {
log("Select a table or variable set to unhide from.");
return;
}
let table = user_selections.selected_tables.length > 0; // selected_questions and selected_tables can't both have elements
let hidden_codes = [];
if (table) {
let selected_tables = user_selections.selected_tables;
// Require exactly one table selected
if (!exactlyNSelected(user_selections, "Table", 1)) {
log("Select a single table before running this option.");
return;
}
let selected_table = selected_tables[0];
let primary = selected_table.primary;
let secondary = selected_table.secondary;
if (primary == null || secondary == null)
return;
let primary_type = primary.questionType;
let secondary_type = secondary.questionType;
if (rows && !checkCanUnhide(primary, rows))
return;
// Collect an object of hidden codes
let primary_dr = primary.dataReduction;
if (primary_dr != null) {
hidden_codes = getHiddenCodes(primary, true);
if (check_missing_types.indexOf(primary_type) > -1)
hidden_codes = hidden_codes.concat(getMissingCodes(primary));
}
if (secondary != "SUMMARY" && secondary != "RAW DATA") {
if (!rows && !checkCanUnhide(secondary, rows))
return;
let secondary_dr = secondary.dataReduction;
if (secondary_dr != null) {
hidden_codes = hidden_codes.concat(getHiddenCodes(secondary, false));
if (check_missing_types.indexOf(secondary_type) > - 1)
hidden_codes = hidden_codes.concat(getMissingCodes(secondary));
}
}
} else { // Variable set rather than table
let selected_questions = user_selections.selected_questions;
if (!exactlyNSelected(user_selections, "Question", 1)) {
log("Select a single Variable Set before running this option.");
return false;
}
let selected_question = selected_questions[0];
if (!checkCanUnhide(selected_question))
return;
let question_is_2d = two_d_questions.indexOf(selected_question.questionType) > -1;
let data_reduction = selected_question.dataReduction;
hidden_codes = getHiddenCodes(selected_question, true);
if (check_missing_types.indexOf(selected_question.questionType) > -1)
hidden_codes = hidden_codes.concat(getMissingCodes(selected_question));
}
// Filter out what is hidden to show only rows or columns
let relevant_axis = rows ? "Rows" : "Columns";
hidden_codes = hidden_codes.filter(function (obj) {
return obj.axis == relevant_axis;
});
if (hidden_codes.length == 0) {
log("There are no " + relevant_axis.toLowerCase() + " to unhide in this " + (table ? "table" : "variable set") + ".");
return;
}
let codes_to_unihide = selectMany("Select the " + (rows ? "rows" : "columns") + " to unhide:", hidden_codes.map(function (obj) { return obj.label; }));
codes_to_unihide = getElementsOfArrayBySelectedIndices(hidden_codes, codes_to_unihide);
codes_to_unihide.forEach(function (obj) {
let data_reduction = obj.question.dataReduction;
if (obj.type == "hidden")
data_reduction.unhide(obj.code);
else if (obj.type == "missing") {
let value_attributes = obj.question.valueAttributes;
try {
value_attributes.setIsMissingData(obj.code, false);
} catch (e) {
log("Could not unhide " + obj.code + ": " + e);
}
}
});
}