Modify Cell Content - Color Selected Rows or Columns

From Q
Jump to: navigation, search

This rule allows you to specify the colors for rows and columns in the table.


Color selected rows.PNG

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


// Color selected rows or columns

includeWeb('JavaScript Array Functions');

form.setSummary("Color Selected Rows or Columns");

var no_selection_string = '(none)';

var controls =[];

// Color rows or columns?
var color_options = ["Color rows", "Color columns"];
var color_rows_or_columns_box = form.newComboBox('columnOrRow', color_options);
color_rows_or_columns_box.lineBreakAfter = true;
var color_columns = color_rows_or_columns_box.getValue() == color_options[1];

// If coloring columns, table must be two-dimensional.
if (color_columns && table.columnLabels == null)
    form.ruleNotApplicable('the columns in this table are statistics');

// Trim column/row labels and change column/row labels that collide with no_selection_string
var col_row_combobox_alternatives = (color_columns ? table.columnLabels : table.rowLabels).map(function (label) {
    return label.trim();
}).map(function (label) {
    if (label == no_selection_string)
        return label + (color_columns ? ' (column)' : ' (row)');
        return label;
col_row_combobox_alternatives = [no_selection_string].concat(enumerateDuplicatesInStringArray(col_row_combobox_alternatives, '(', ')'));

var selected_cols_rows = [];
var selected_colors = [];
var default_color = "AliceBlue";
var last_index = 0;
var current_index = 0;

var col_row_label = color_columns ? 'Column:' : 'Row:';

while (true) {
    var col_row_combobox = form.newComboBox((color_columns ? 'column' : 'row') + current_index, col_row_combobox_alternatives);
    var selected_col_row = col_row_combobox.getValue();
    if (selected_col_row == no_selection_string) // stop adding when the combobox selection is null
    else { // only add comboboxes with a column/row selected
        var color_picker = form.newColorPicker('color' + current_index)
        color_picker.lineBreakAfter = true;
        var selected_color = color_picker.getValue();
        last_index = current_index + 1;


// Do the coloring
var cell_colors = table.cellColors;
var indices_to_color = (label) { return col_row_combobox_alternatives.indexOf(label) - 1; });
indices_to_color.forEach(function (x, ind) {
    var current_color = selected_colors[ind];
    if (color_columns) {
        for (var row = 0; row < table.numberRows; row++)
            cell_colors[row][x] = current_color;       
    } else {
        for (var col = 0; col < table.numberColumns; col++)
            cell_colors[x][col] = current_color;

table.cellColors = cell_colors;

See also