onOpen: add a custom menu that sorts the active sheet

This is original example code for a Google Sheet. It adds a menu when the spreadsheet opens and sorts the active sheet by the first column, keeping a header row. Official docs for simple triggers: search “Apps Script simple triggers” on developers.google.com/apps-script/guides/triggers.

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Sheet tools')
    .addItem('Sort by column A (keep header)', 'sortActiveKeepingHeader')
    .addToUi();
}

function sortActiveKeepingHeader() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var lastCol = sheet.getLastColumn();
  if (lastRow < 2 || lastCol < 1) {
    SpreadsheetApp.getUi().alert('Nothing to sort under the header.');
    return;
  }
  sheet.getRange(2, 1, lastRow - 1, lastCol).sort({column: 1, ascending: true});
}

Paste both functions into a new script file in the spreadsheet, save, and reload the sheet. The first time you run the menu item, Google will ask you to authorize the script. Read the permission screen. This example only uses the current spreadsheet.