Abbreviate Month Labels for a Date/Time Question

From Q
Jump to navigation Jump to search

This example may be used to abbreviate month labels for a Date question when aggregated to monthly. Instead of showing the full month and year by default, e.g. 'January 2019', it will truncate the label to 3 letters, i.e. 'Jan'.

To use this snippet:

  1. Select your table.
  2. Select Automate > Custom Rule.
  3. Paste in the code from below.
  4. Click the 'Play' icon and close.
// Abbreviate month labels in rows and columns of table
// All labels must be months for any replacements to be made

var row_labels = table.rowLabels;
var col_labels = table.columnLabels;

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

// Check row labels and update
var split_labels = row_labels.map(function (str) { return str.split(" ")[0]; });
if (split_labels.every( function (str) { return months.indexOf(str) > -1; })) {
   split_labels = split_labels.map(function (str) { return str.substring(0, 3); });
   table.rowLabels = split_labels;
}

// Check column labels and update
var split_labels = col_labels.map(function (str) { return str.split(" ")[0]; });
if (split_labels.every( function (str) { return months.indexOf(str) > -1; })) {
   split_labels = split_labels.map(function (str) { return str.substring(0, 3); });
   table.columnLabels = split_labels;
}

See also