Skip to content

Instantly share code, notes, and snippets.

@AriLFrankel
Created January 29, 2018 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AriLFrankel/6eacb213fcd38e9f4229e3f0e5b5c06c to your computer and use it in GitHub Desktop.
Save AriLFrankel/6eacb213fcd38e9f4229e3f0e5b5c06c to your computer and use it in GitHub Desktop.
an auto sort function for Google Sheets
/*
* This is a custom sheet that can be used to autosort
* a specified range in Google Sheets.
* To add this script to your Google Sheet, simply:
*
* Click Tools > Script Editor
*
* Copy this file in to the script file.
*
* Configure the script with the options below.
*
* Click save and return to the Google sheet.
*
* Profit :)
*
*
*
* Configure the script here:
*
*
* Define a sort pattern.
* Indicate which columns to sort by, in order,
* and whether to sort ascending (true) or descending (false)
* for each column
*
* e.g.
* [{ column: "H", ascending: true },
* { column: "I", ascending: false}]
*
*
* Select a range to sort, e.g. "H50:I200"
*/
var SORT_CONFIG = [];
// Select the range you will be sorting
var RANGE_TO_SORT = "";
/*
---------Edit below this line at your own risk-------
*/
// helper functions
/**
* Translates a column index to its letter value
*
* @param {Int} column A column index: A = 1, B = 2 ...
*/
function columnToLetter(column) {
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
/**
* Translates a column index to its letter value
*
* @param {String} letter A column: A, B, ... AA, etc.
*/
function letterToColumn(letter)
{
var column = 0, length = letter.length;
for (var i = 0; i < length; i++)
{
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
/**
* returns a BOOLEAN that a cell is in a range
*
* @param {range} range
* @param {cell} cell
*/
function isInRange(range, cell){
// get the coordinates of the range
var rangeRowStart = range.getRow();
var rangeRowEnd = rangeRowStart + range.getHeight() - 1;
var rangeColStart = range.getColumn();
var rangeColEnd = rangeColStart + range.getWidth() - 1;
// Get the coordinates of cell that was just modified.
var [row, col] = [cell.getRow(), cell.getColumn()];
// only re-sort if the edited cell is in the target range
return col >= rangeColStart && col <= rangeColEnd && row >= rangeRowStart && row <= rangeRowEnd;
}
/* One Time Setup
*
* Get active sheet
* get range to sort
* translate config object from user-friendly column names to column indeces
*/
var activeSheet = SpreadsheetApp.getActiveSheet();
var RANGE = activeSheet.getRange(RANGE_TO_SORT);
SORT_CONFIG = SORT_CONFIG.map(function(c){
return { column: letterToColumn(c.column), ascending: c.ascending };
});
/**
* Sorts on the target column.
*
* @param {Sheet} sheet The sheet to sort.
*/
function sort(sheet) {
RANGE.sort(SORT_CONFIG);
}
/**
* Triggers a fresh sort when a cell in the target range is edited
*/
function onEdit() {
// Update the active sheet in case it changed.
activeSheet = SpreadsheetApp.getActiveSheet();
// Get the coordinates of cell that was just modified.
var editedCell = activeSheet.getActiveCell();
if(isInRange(RANGE, editedCell)){
sort(activeSheet);
}
}
@nycaur
Copy link

nycaur commented Apr 20, 2018

Hi – wanted to seek your help on this- I'm new to apps script but got smthg to work that Autosorts – butunfortunately it only works on first column. Here's the script
/**

  • Automatically sorts the 1st column (not the header row) Descending.
    */
    function onEdit(){
    var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var editedCell = sh.getActiveRange().getColumnIndex();

if(editedCell == 2) {
var range = sh.getRange("A2:B100");
range.sort({column: 2, ascending: false });
}
}
Run code snippetExpand snippet

I've linked my test goog sheet here...its editable.
https://docs.google.com/spreadsheets/d/1cfgYJ91h6g6bLqrI5uuUoUYgqcyM0WoktirxBjxVbN0/edit?usp=sharing

I’ve list (sheets) of 4 team games we organize and list of participants. Need something that can do a simple AutoSort on all 8 columns (4 datasets of 2 cols each) as follows:
These are 4 separate teams (and to separate them, I’ve colored them as such- they have nothing related to each other- so only Col A & B are related, then Col C & D are related, likewise) so Col A & B is 1st game team; Col C & D is 2nd game team (totally separate from Game 1) and Col E & F is 3rd game team (totally separate from Game1 & 2)

All that sheet needs to Autosort is: Anytime an edit is made it shd. first sort Descending on Col A, then Ascending Col B; likewise Descending on Col C, then Ascending Col D; likewise Descending on Col E, then Ascending Col F; likewise Descending on Col G, then Ascending Col H And it shd. ignore any blank cells...

All 4 teams are placed together only for formatting and printing purposes but need to be sorted separately.

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