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

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.

// 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
        var 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
}

form.setHeading('Automatically Rename Row Labels');
var label1 = form.newLabel('Old label:');
var combobox = form.newComboBox('old', table.rowLabels);
combobox.setDefault(table.rowLabels[0]);
var label2 = form.newLabel('New label:');
var textbox = form.newTextBox('new');
form.setInputControls([label1, combobox, label2, textbox]);
var current_label = combobox.requireValue();
var new_label = textbox.requireValue();

form.setSummary("Change the row label from \"" + current_label + "\" to \"" + new_label + "\"");
changeRowLabel(current_label, new_label);

See also