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

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.

table.requireNumericTable();

// Set up controls for user input.
form.setHeading('Adding Text to Cells with Large Numbers:');
var label_statistic = form.newLabel('Statistic to use:');
var translated_statistics = table.statistics.map(function(s) {
    try {
	return table.getTranslation(s);
    }catch(e) {
	return s;
    }
});

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

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

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

// Get the values for statistic
var values = table.get(statistic);
 
// Get the default text in each cell in the table.
var cell_texts = table.cellText;
 
// Loop through each cell...
for (var row = 0; row < table.numberRows; row++)
    for (var column = 0; column < table.numberColumns; column++) {
        var 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