Modify Headers - Abbreviate Month Labels

From Q
Jump to navigation Jump to search


This rule automatically abbreviates the names of months in crosstabs that show date questions in the rows or columns. For example, it will change January 2015 to Jan '15. This allows for more compact column headers. You can choose whether to modify the labels in the rows or the columns of the table.

Example

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

includeWeb('Table JavaScript Utility Functions');

// Abbreviate Month Names
form.setHeading("Abbreviate Month Labels");

let description = form.newLabel("Show only the first three letters of each month label");
description.lineBreakAfter = true;

let label = form.newLabel("Months in");
let row_or_column_box = form.newComboBox("rowcol", ["columns", "rows"]);
row_or_column_box.setDefault("columns");
form.setInputControls([description, label, row_or_column_box]);
let do_rows = row_or_column_box.getValue() == "rows";

form.setSummary("Abbreviate month names in " + row_or_column_box.getValue());

let invalid_message = "the table requires months to be shown in the " + row_or_column_box.getValue();

if (!do_rows) {
    excludeRTablesWithoutColumns();
}

if (!do_rows && table.columnLabels == null)
    form.ruleNotApplicable(invalid_message);

let labels = do_rows ? table.rowLabels : table.columnLabels;
labels = labels.map(function (label) {
    let label_elements = label.split(" ")
    if (label_elements.length != 2)
        form.ruleNotApplicable(invalid_message);
    let month_prefix = label_elements[0];
    if (month_prefix.indexOf("January") == 0)
        month_prefix = "Jan";
    else if (month_prefix.indexOf("February") == 0)
        month_prefix = "Feb";
    else if (month_prefix.indexOf("March") == 0)
        month_prefix = "Mar";
    else if (month_prefix.indexOf("April") == 0)
        month_prefix = "Apr";
    else if (month_prefix.indexOf("August") == 0)
        month_prefix = "Aug";
    else if (month_prefix.indexOf("September") == 0)
        month_prefix = "Sept";
    else if (month_prefix.indexOf("October") == 0)
        month_prefix = "Oct";
    else if (month_prefix.indexOf("November") == 0)
        month_prefix = "Nov";
    else if (month_prefix.indexOf("December") == 0)
        month_prefix = "Dec";

    let year = label_elements[1];
    if (year.length != 4)
        form.ruleNotApplicable(invalid_message);
    return month_prefix + " '" + year.substring(2);
});

if (do_rows)
    table.rowLabels = labels;
else
    table.columnLabels = labels;

See also