Created
June 10, 2020 05:30
-
-
Save konahart/caae45b5a734a693d3a8fd3924488400 to your computer and use it in GitHub Desktop.
Library Shelf Alphabetize Google Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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