Sorting and Reordering - Making One Column Appear After Another
This rule makes one column in a table appear after another.
Example
Technical details
- This script works for both 1 dimensional summary tables with multiple statistics and 2 dimensional tables (i.e. crosstabs).
- This rule is not compatible with tables with column spans.
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.
includeWeb("Table JavaScript Utility Functions");
var column_labels = table.columnLabels;
if (!column_labels) { // 1D table
column_labels = table.statistics;
if (column_labels.length < 2)
form.ruleNotApplicable('table has only one column');
// Set up controls for user input.
form.setHeading('Making One Column Appear After Another');
var label_1 = form.newLabel('Make');
var label_2 = form.newLabel('appear after');
var combo_box_column_1 = form.newComboBox('column1', column_labels);
form.setInputControls([label_1, combo_box_column_1, label_2]);
var column_1_label = combo_box_column_1.requireValue();
var index_1 = column_labels.indexOf(column_1_label);
var column_labels_reduced = column_labels;
column_labels_reduced.splice(index_1, 1);
var combo_box_column_2 = form.newComboBox('column2', column_labels_reduced);
form.setInputControls([label_1, combo_box_column_1, label_2, combo_box_column_2]);
var column_2_label = combo_box_column_2.requireValue();
form.setSummary('Make column "' + column_1_label + '" appear after column "' + column_2_label + '"');
// Move the columns
var index_2 = column_labels_reduced.indexOf(column_2_label);
column_labels_reduced.splice(index_2 + 1, 0, column_1_label);
table.statistics = column_labels_reduced;
} else { // 2D table
if (column_labels.length < 2)
form.ruleNotApplicable('table has only one column');
if (table.columnSpans.length > 0)
form.ruleNotApplicable('table has column spans');
// Set up controls for user input.
form.setHeading('Making One Column Appear After Another');
var label_1 = form.newLabel('Make');
var label_2 = form.newLabel('appear after');
var combo_box_column_1 = form.newComboBox('column1', column_labels);
form.setInputControls([label_1, combo_box_column_1, label_2]);
var column_1_label = combo_box_column_1.requireValue();
var index_1 = table.columnIndex(column_1_label);
var column_labels_reduced = column_labels;
column_labels_reduced.splice(index_1, 1);
var combo_box_column_2 = form.newComboBox('column2', column_labels_reduced);
form.setInputControls([label_1, combo_box_column_1, label_2, combo_box_column_2]);
var column_2_label = combo_box_column_2.requireValue();
var index_2 = table.columnIndex(column_2_label);
form.setSummary('Make column "' + column_1_label + '" appear after column "' + column_2_label + '"');
// Move the columns
moveColumnAfterComplete(index_1, index_2);
}
See also
- User Input for Rules for technical information on Rules.
- Rule Online Library for other examples of Rules.
- Table JavaScript and Plot JavaScript for the JavaScript that is used to write custom rules.
- JavaScript for information about the JavaScript programming language.
Q Technical Reference
Q Technical Reference
Q Technical Reference > Creating And Modifying Tables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript
Rule Online Library