Modify Whole Table or Plot - Hide Empty Rows and Columns

From Q
Jump to: navigation, search

This rule hides rows or 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 rows or columns have been removed.



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.


  • 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).


You can find a simpler version of this code, which does not contain the controls, here.

includeWeb("Table JavaScript Utility Functions");
form.setSummary("Hide empty rows and columns");
// Returns true if all elements of the array are zero
function arrayElementsAllZero(array) {
    var non_zero = array.filter(function(x) { return x > 0; } );
    return non_zero.length == 0;
// Returns true if all elements of the array are NaN
function arrayElementsAllNaN(array) {
    var not_nan = array.filter(function(x) { return !isNaN(x); } );
    return not_nan.length == 0;
function arrayElementsNotSpecifiedStrings(array, strings) {
    var not_string = array.filter(function (x) { return strings.indexOf(x) == -1; });
    return not_string.length == 0;
// Obtain the primary statistics to check
var primary_statistic = table.availableStatistics[0];
var values = table.get(primary_statistic);
// Check to see if the primary statistic is a % statistic.
var is_percentage = primary_statistic.indexOf('%') != -1;
// Check to see if the table is showing a Text question
var is_text = primary_statistic == "Text";
var empty_strings = [""];
// Check for empty rows
var empty_rows = [];
for (var j = 0; j < table.numberRows; j++) {
    var current_row_values = values[j];
    if ( (is_percentage && arrayElementsAllZero(current_row_values)) || (arrayElementsAllNaN(current_row_values) && !is_text) || (is_text && arrayElementsNotSpecifiedStrings(current_row_values, empty_strings)))
// Check for empty columns
var empty_columns = [];
for (var j = 0; j < table.numberColumns; j++) {
    var current_column_values = [];
    for (var k = 0; k < table.numberRows; k++) 
    if ((is_percentage && arrayElementsAllZero(current_column_values)) || (arrayElementsAllNaN(current_column_values) && !is_text) || (is_text && arrayElementsNotSpecifiedStrings(current_column_values, empty_strings)))
// Remove empty rows
if (empty_rows.length > 0)
    for (var j = empty_rows.length - 1; j > -1; j--) {
// Remove empty columns
if (empty_columns.length > 0)
    for (var j = empty_columns.length - 1; j > -1; j--) {
// Add a note to the footer
if (empty_rows.length > 0 || empty_columns.length > 0) {
    var current_footers = table.extraFooters;
    current_footers.push("Some empty rows or columns have been removed");
    table.extraFooters = current_footers;

See also