Skip to content

Instantly share code, notes, and snippets.

@konahart
Created June 10, 2020 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konahart/caae45b5a734a693d3a8fd3924488400 to your computer and use it in GitHub Desktop.
Save konahart/caae45b5a734a693d3a8fd3924488400 to your computer and use it in GitHub Desktop.
Library Shelf Alphabetize Google Sheet
// adapted from https://webapps.stackexchange.com/questions/84218/sorting-alphabetically-a-z-ignoring-beginning-a-an-and-the-using-a-scri
function sortByTitle() {
var sheet = SpreadsheetApp.getActiveSheet();
// Ignore header row (A1:Z1)
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
var values = range.getValues();
values.sort(function(a,b) {return (trimmed(a[0]) < trimmed(b[0]) ? -1 : 1);});
range.setValues(values);
}
function trimmed(str) {
return str.toLowerCase().replace(/^(a|an|the) /, '');
}
function onOpen() {
SpreadsheetApp.getActiveSpreadsheet().addMenu("Sort", [{name: "by Title", functionName: "sortByTitle"}]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment