Custom Sort in Google Apps Spreadsheet

I use a Google Docs spreadsheet to track my personal projects. Only, I’m not aware of a way to save sort options in a Google Docs spreadsheet. The solution is to create a script. The following script will add a menu option that remembers sort parameters and makes use of a header row on the data. Detailed instructions on adding the script are here: http://productforums.google.com/forum/#!topic/docs/Kl9WEj6Kha0 and I just added skipping of the header row.

/**
* sort spreadsheet assuming first row is a header row
*/
function customSort() {
var sheet = SpreadsheetApp.getActiveSheet();
var allData = sheet.getDataRange();
var range = sheet.getRange(2,1, allData.getNumRows()-1, allData.getNumColumns());
range.sort( [1,2,4]); // see https://developers.google.com/apps-script/class_range#sort
};

/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Custom Sort",
functionName : "customSort"
}];
sheet.addMenu(“Custom Scripts”, entries);
};

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>