I am working with a simple spreadsheet that has name/lastnames in it. I need for the spreadsheet to automatically sort the entry alphabetically by the last name (column B).
I have used the following code:
function onEdit(event){
var sheet = event.source.getActiveSheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 1;
var tableRange = "A2:B500";
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy } );
}
}
However, when I run it, I get the following message: Cannot read property "source" from undefined. (line 2)
Could someone please help me figure out what I am doing wrong?
Best Answer
This piece of code works:
One key difference is the
.getColumnIndex
. Without it you don't receive an integer !!See example file I've created: sorting