Modify Cell Content - Add Text to Cells with Large Numbers

From Q
Jump to navigation Jump to search

This rule appends user-specified text (default is an exclamation mark "!") to each cell whose value exceeds a user-defined threshold. If there is more than one statistic in the table, the user may select the statistic to use, which is assigned to be the primary statistic.

Example

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

See here for a simpler version of the JavaScript which does not contain the controls.

includeWeb('Table JavaScript Utility Functions');

excludeRTables();
table.requireNumericTable();

// Set up controls for user input.
form.setHeading('Adding Text to Cells with Large Numbers:');
let description = form.newLabel("Add symbols or text to cells with large values");
description.lineBreakAfter = true;
let label_statistic = form.newLabel('Statistic to use:');
let translated_statistics = translateStats(table.statistics);

let combo_box = form.newComboBox('statistic', translated_statistics);
combo_box.setDefault(translated_statistics[0]);
combo_box.lineBreakAfter = true;
let label_threshold = form.newLabel('Threshold:');
let numeric_up_down  = form.newNumericUpDown('threshold');
numeric_up_down.setDefault(20);
numeric_up_down.setIncrement(1);
numeric_up_down.setMaximum(999999999);
numeric_up_down.lineBreakAfter = true;
let label_text = form.newLabel('Text to append:');
let text_box = form.newTextBox('text');
text_box.setDefault('!');
form.setInputControls([description, label_statistic, combo_box, label_threshold, numeric_up_down, label_text, text_box]);

let statistic_translated = combo_box.getValue();
let statistic = table.statistics[translated_statistics.indexOf(statistic_translated)];
let threshold = numeric_up_down.getValue();
let text = text_box.getValue();
form.setSummary('Append "' + text + '" to cells with ' + statistic_translated + ' larger than ' + threshold);

// Move chosen statistic to top
let stats = table.statistics;
stats.splice(stats.indexOf(statistic), 1);
stats.splice(0, 0, statistic);
table.statistics = stats;

// Get the values for statistic
let values = table.get(statistic);
 
// Get the default text in each cell in the table.
let cell_texts = table.cellText;
 
// Loop through each cell...
for (let row = 0; row < table.numberRows; row++)
    for (let column = 0; column < table.numberColumns; column++) {
        let value = values[row][column];
        if (value > threshold) {
            // Append text next to the first statistic.
            cell_texts[row][column] = [text];
        }
    }
 
// Now store the new cell texts.
table.cellText = cell_texts;

See also