Modify Headers - Automatically Rename Row Labels

From Q
Jump to navigation Jump to search


This rule automatically renames row labels. E.g., changes NET to Total. Often it may be better to chance such things using Find/Replace (see Rules, QScript Macros and When To Use Them).

Example

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

You can find a simpler version of this code, which does not contain the controls, here.

form.setHeading('Automatically Rename Row Labels');

// Change the label of a given row
function changeRowLabel(current_label,new_label) {
    if(table.rowIndex(current_label) != -1) { // If there is a row with the specified label
        let row_labs = table.rowLabels; // Get the row labels
        row_labs[table.rowIndex(current_label)] = new_label; // Change the one of interest
        table.rowLabels = row_labs; // Set the row labels
    }
    else alert("There is no row called \"" + current_label + "\""); // Otherwise, present a warning message
}

let description = form.newLabel('Automatically rename a label whenever it appears in a row');
description.lineBreakAfter = true;

let label1 = form.newLabel('Old label:');
let combobox = form.newComboBox('old', table.rowLabels);
let label2 = form.newLabel('New label:');
let textbox = form.newTextBox('new');
form.setInputControls([description, label1, combobox, label2, textbox]);
let current_label = combobox.getValue();
let new_label = textbox.requireValue();

if (current_label !== null) {
    form.setSummary("Change the row label from \"" + current_label + "\" to \"" + new_label + "\"");
    changeRowLabel(current_label, new_label);
} else {
    form.setSummary("Change the row label (not applied as no row selected");
}

See also