Modify Cell Content - Color Selected Rows or Columns

From Q
Jump to navigation Jump to search


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

Example

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.

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

// Color selected rows or columns

includeWeb('JavaScript Array Functions');
includeWeb('Table JavaScript Utility Functions');

excludeRTables();

form.setHeading('Color Selected Rows or Columns');

let description = form.newLabel('Choose colors for rows or columns in the table');
description.lineBreakAfter = true;

const no_selection_string = '(none)';

// Color rows or columns?
let color_label = form.newLabel("Color:")
let color_options = ["Rows", "Columns"];
let color_rows_or_columns_box = form.newComboBox('columnOrRow', color_options);
color_rows_or_columns_box.setDefault(color_options[0]);
color_rows_or_columns_box.lineBreakAfter = true;
let color_columns = color_rows_or_columns_box.getValue() == color_options[1];

let controls = [description, color_label, color_rows_or_columns_box];
form.setSummary('Color selected ' + color_rows_or_columns_box.getValue().toLowerCase());

// 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
let 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)');
    else
        return label;
});
col_row_combobox_alternatives = [no_selection_string].concat(enumerateDuplicatesInStringArray(col_row_combobox_alternatives, '(', ')'));


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

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

while (true) {
    let col_row_combobox = form.newComboBox((color_columns ? 'column' : 'row') + current_index, col_row_combobox_alternatives);
    col_row_combobox.setDefault(no_selection_string);
    let selected_col_row = col_row_combobox.getValue();
    controls.push(form.newLabel(col_row_label));
    controls.push(col_row_combobox);
    if (selected_col_row == no_selection_string) // stop adding when the combobox selection is null
        break;
    else { // only add comboboxes with a column/row selected
        
        controls.push(form.newLabel('Color:'));
        let color_picker = form.newColorPicker('color' + current_index)
        color_picker.setDefault(default_color);
        color_picker.lineBreakAfter = true;
        let selected_color = color_picker.getValue();
        controls.push(color_picker);
        
        selected_cols_rows.push(selected_col_row);
        selected_colors.push(selected_color);      
        last_index = current_index + 1;
    }
    current_index++;
}

form.setInputControls(controls);

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

table.cellColors = cell_colors;

See also