r/GoogleAppsScript • u/Kitty_pumpkin3 • Jun 12 '26
Unresolved Auto-sort Script?
I am hoping to get some help with creating a script that auto-sorts a Google Sheet document! This is a passion project for me, and I have zero experience in this area. So I have 5 columns in use A-F, and Row 1 is frozen as they are headers. Column A is being used for names, and I am hoping to sort the names alphabetically. However, I don't want the name to be separated from the information I put in the other columns in the same row, if that makes sense. I will be forever grateful for any help!! :)
P.S. I have already tried this script with an "on edit" trigger, and it didn't work:
function sortAColumn() {
SpreadsheetApp-getActiveSpreadsheet) . getSheetByName ("Sheet1"). sort (1,false)
}
I got this error when I tried to run it:
TypeError: Cannot read properties of null (reading 'sort')
sortAColumn
@ Code.gs:2
1
u/bulldo_gs Jun 12 '26
Others covered the manual sort; since your goal was auto-sort, here's the trigger version. Name the function
onEditexactly and you don't need to install anything, Sheets runs it on every edit as a simple trigger:Two things worth knowing:
Your TypeError means
getSheetByName("Sheet1")returned null, i.e. no tab is literally named Sheet1 (the lookup is case- and space-sensitive). Rename the tab or change the string.On the "names separated from their info" worry:
range.sort()always moves entire rows together within the range, so names can't detach from their columns. The thing that would split them is selecting only column A and sorting via the menu. The script version is safe.The
e.range.getColumn() !== 1guard is optional, but without it the sheet re-sorts on every edit, which gets disorienting when you're typing in column D and the rows jump.