Modify Headers - Automatically Rename Column Labels

From Q
Jump to: navigation, search

This rule automatically renames column 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).



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.


  • 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).


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

if (table.columnLabels == null)
    form.ruleNotApplicable('this table does not have columns');

// Change the label of a given column
function changeColumnLabel(current_label,new_label) {
    if(table.columnIndex(current_label) != -1) { // If there is a column with the specified label
        var col_labs = table.columnLabels; // Get the column labels
        col_labs[table.columnIndex(current_label)] = new_label; // Change the one of interest
        table.columnLabels = col_labs; // Set the column labels
    else alert("There is no column called \"" + current_label + "\""); // Otherwise, present a warning message

form.setHeading('Automatically Rename Column Labels');
var label1 = form.newLabel('Old label:');
var combobox = form.newComboBox('old', table.columnLabels);
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 column label from \"" + current_label + "\" to \"" + new_label + "\"");
changeColumnLabel(current_label, new_label);

See also