Modify Whole Table or Plot - Hide Empty Columns
Jump to navigation
Jump to search
This rule hides columns for tables and charts where the primary statistic for the table is always 0% or NaN. The primary statistic is the first statistic on the Statistics - Cells list for the table. A note is added to the footer when any columns have been removed.
Example
How to apply this rule
For the first time in a project
- Select the table(s)/chart(s) that you wish to apply the rule to.
- Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
- Click on the Rule when it appears in the QScripts and Rules section of the search results.
OR
- Select Automate > Browse Online Library.
- Choose this rule from the list.
Additional applications of the rule
- Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
- Click on the Rules tab (bottom-left of the table/chart).
- Select the rule that you wish to apply.
- Click on the Apply drop-down and choose your desired option.
- Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.
Removing the rule
- Select the table(s)/chart(s) that you wish to remove the rule from.
- Press the Rules tab (bottom-right corner).
- Press Apply next to the rule you wish to remove and choose the appropriate option.
How to modify the rule
- Click on the Rules tab (bottom-left of the table/chart).
- Select the rule that you wish to modify.
- Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).
JavaScript
You can find a simpler version of this code, which does not contain the controls, here.
includeWeb("QScript Utility Functions");
includeWeb("Table JavaScript Utility Functions");
includeWeb("JavaScript Array Functions");
// This rule does not work with R tables where columns are not recognised,
// so we disable the rule where there is more than one statistic (column).
if (table.statistics.length > 1) {
excludeRTablesWithoutColumns();
}
form.setSummary("Hide empty columns");
form.setHeading("Hide Empty Columns");
const description = form.newLabel("Hide any columns where all of the values are zero, NaN, or blank.");
form.setInputControls([description]);
excludeLargeTables();
// Obtain the primary statistics to check
const primary_statistic = table.availableStatistics[0];
const values = table.get(primary_statistic);
// Check to see if the primary statistic is a % statistic.
const is_percentage = primary_statistic.indexOf('%') != -1;
// Check to see if the table is showing a Text question
const is_text = primary_statistic == "Text";
const empty_strings = [""];
const empty_columns = [];
for (let j = 0; j < table.numberColumns; j++) {
const current_column_values = [];
for (let k = 0; k < table.numberRows; k++) {
current_column_values.push(values[k][j]);
}
if ((is_percentage && arrayElementsAllZero(current_column_values)) || (arrayElementsAllNaN(current_column_values) && !is_text) || (is_text && arrayElementsNotSpecifiedStrings(current_column_values, empty_strings))) {
empty_columns.push(j);
}
}
// Remove empty columns
if (empty_columns.length > 0) {
if (table.deleteColumns !== undefined) {
table.deleteColumns(empty_columns);
}
else {
for (let j = empty_columns.length - 1; j > -1; j--) {
deleteColumnComplete(empty_columns[j]);
}
}
// Add a note to the footer
const current_footers = table.extraFooters;
current_footers.push("Some empty columns have been removed");
table.extraFooters = current_footers;
}
See also
- User Input for Rules for technical information on Rules.
- Rule Online Library for other examples of Rules.
- Table JavaScript and Plot JavaScript for the JavaScript that is used to write custom rules.
- JavaScript for information about the JavaScript programming language.