

From Q
Jump to: navigation, search

This rule allows you to select the number of rows to show on a table or chart. The user is able to specify the number of rows (i.e., k), and also to specify particular rows that must always appear. Colors can be assigned to the rows that must always appear. If used after first applying Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), it allows you to show, for example, the top 3 brands automatically.


This example applies two rules. First, Sorting and Reordering - Sort Rows (Automatically Updates when Data Changes), and then this rule. It has been set so that it shows the top two brands and, if Pepsi appears, it highlights it in green. In this instance, Pepsi was not in the top two rows, so it is appended to the table as a third row.


How to apply this rule

For the first time in a project



  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select the rule that you wish to apply.

Removing the rule

  • Press the Rules tab (bottom-right corner).

How to modify the rule

  • Select the rule that you wish to modify.



// Heading for the form
form.setHeading('Selecting the number of rows to show on a table and specifying required rows');
includeWeb("Table JavaScript Utility Functions");
// Creating input for user to specify number of rows
numeric_up_down.lineBreakAfter = true;
numeric_up_down.setMinimum(0); // preventing the user from having tables with less than 0 rows (not 1, as a user may wish to only specify the brands)
var control_array = [number_label, numeric_up_down];
// Textboxes for selecting rows to always retain
var label_row = form.newLabel('Rows to always retain:');
var text_box = form.newTextBox('textBox');
// Adding addition text boxes, if required
var last_selection = text_box.getValue();
var required_row_labels = [];
while (last_selection != "") {
    var new_name = "textBox" + (control_array.length - 1);
    var new_menu = form.newTextBox(new_name);
    last_selection = new_menu.getValue();
// control for the output color
var label_color = form.newLabel('Color for required rows:');
var color_picker = form.newColorPicker('color');
form.setOutputControls([label_color, color_picker]);
// control for the output color
var color = color_picker.getValue();
var maximum_number_rows = numeric_up_down.getValue();
// Creating a summary
var summary = 'Top ' + maximum_number_rows + ' rows';
if (required_row_labels.length != 0)
    summary += '; always show "' + required_row_labels.join('", "') + '"';
// Deleting the rows more than the specified number (unless they are required)
var row_labels = table.rowLabels;
var n_extra_required_rows = 0;
while (table.numberRows > maximum_number_rows + n_extra_required_rows) {
    var last_row = table.numberRows - 1 - n_extra_required_rows;
    var last_label = row_labels[last_row];
    if (required_row_labels.indexOf(last_label) != -1) {
    } else {
// Coloring rows
row_labels = table.rowLabels; //getting again as may have changed
var colors = table.cellColors;
row_labels.forEach(function (label, index) {
    if (required_row_labels.indexOf(row_labels[index]) != -1) { // check if row label contains text
        for (var i = 0; i < table.numberColumns; i++) // set color for each element in row
            colors[index][i] = color;
if (table.numberRows != 0)
    table.cellColors = colors;
// that right_table is included properly before the rule code is
// executed.
function rightTableExists() {
    var exists = true;
    try { 
    } catch (e) {
        exists = false;
    return exists;

See also