Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Last active May 30, 2016 13:50
Show Gist options
  • Save alexanderjeurissen/877d479143218c135113 to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/877d479143218c135113 to your computer and use it in GitHub Desktop.
function generateAlphaNumericLabels(amount) {
var labels = [];
for (var i = 0; i <= amount; i++) {
var appendNumber = 0;
var offset = 0;
if (i > 25 && i <= 34) {
appendNumber = i - 25;
labels.push(String.fromCharCode(65 + offset) + appendNumber);
} else if (i > 33) {
offset = Math.floor((i - 26) / 10);
appendNumber = i - (25 + (offset * 10));
labels.push(String.fromCharCode(65 + offset) + appendNumber);
} else {
labels.push(String.fromCharCode(65 + i));
}
}
return labels;
}
@alexanderjeurissen
Copy link
Author

returns an array of strings / labels that can be used for charts. The format of the returned list is:

["A",..."Z",...,"A1","A2",...,"A10",..."Z10"]

the amount parameter determines how many elements will be generated. there is no check on the amount so in case the amount exceeds "Z10" you'll get strange labels with symbols in it.

@aledustet
Copy link

aledustet commented May 30, 2016

If you wish to change it to the headers on excel documents for example, like continuous headers ["A",..."Z","A1",..."Z1","A2" ...] is really simple the change

function generateAlphaNumericLabels(amount) {
  var labels = [];
  for (var i = 0; i <= amount; i++) {     
      if (i > 25) {
        labels.push(String.fromCharCode(65 + (i % 26)) + Math.floor(i/26));
      } else {
        labels.push(String.fromCharCode(65 + i));
      }
  }
  return labels;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment