I have the following function running in Apps Script, however the onEdit function is only called via manual edits. It does not work when I edit the sheet using the API. Here is the function:

 * Automatically sorts the 8th column (not the header row) Ascending.
function onEdit(event){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 8;
  var startRow = 2;
  var startColumn = 1;
  var range = sheet.getRange(startRow,startColumn,sheet.getLastRow()-startRow+1,sheet.getLastColumn()-startColumn+1);
  range.sort( { column : columnToSortBy, ascending: true } );

Edit: A commenter suggested that this question may be similar to another question, see comment below. However that question is discussing the difference between two functions, neither of which solve my issue, this question is asking about a functionality that neither of those functions provide.

  • 2
    Does this answer your question? Diference between onEdit or onChange trigger? – Kos Jun 24 at 20:44
  • 1
    Thanks for the response, a quote from there states "Script executions and API requests do not cause triggers to run.", so this would not work. – programmer1 Jun 24 at 21:34
  • Script executions and API requests do not cause triggers to run That's true, but it would not affect a time-driven trigger. – Tedinoz Jun 25 at 11:01
  • @Tedinoz would you be able to show me how to do that or link me to some documentation? Would you also know what the minimum time is for the trigger and if there is any downside to having it running frequently? – programmer1 Jun 25 at 19:12
  • I figured it out, you just go into the Triggers settings in the App Scripting. I've set it to the minimum time of 1 minute, not exactly what I wanted. If anyone has the automatic solution, I am still interested. – programmer1 Jun 25 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.