Modify Headers - Automatically Rename Column Labels

From Q
Jump to navigation Jump to search

This rule automatically renames column labels. E.g., changes NET to Total. It is based off of the labels of the current table the rule is applied to. To apply the rule to tables with different column headers, please rerun the rule. Also do not apply it to tables that you do not want relabeled as it automatically will update the current selection to the corresponding column in the current table and render bad results. 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.

includeWeb("Table JavaScript Utility Functions");


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

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
        let 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

let description = form.newLabel('Automatically rename a label whenever it appears in a column');
description.lineBreakAfter = true;
let label1 = form.newLabel('Old label:');
let combobox = form.newComboBox('old', table.columnLabels);
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 column label from \"" + current_label + "\" to \"" + new_label + "\"");
    changeColumnLabel(current_label, new_label);
} else {
    form.setSummary("Change the column label (not applied as no column selected)");

See also