Google-sheets – Google spreadsheet multiple auto-sorting

google sheetsgoogle-apps-scriptsorting

In a Google Spreadsheet I have different group of cells to auto-sort ascending i.e.

  • table range a3:c16 columntosortby = 2
  • table range d18:f31
    columntosortby = 5
  • table range g33:i46 columntosortby = 8

etc.

By using the following code I'm able to auto-sort the first group:

/**
* Automaticamente ordina arrivi gruppo A 
*/

function onEdit(event){
  var sheet = event.source.getActiveSheet();
  var editedCell = sheet.getActiveCell();

  var columnToSortBy = 2;
  var tableRange = "a3:c16"; // Celle da ordinare.

  if(editedCell.getColumn() == columnToSortBy){   
    var range = sheet.getRange(tableRange);
    range.sort( { column : columnToSortBy, ascending: true } );
  }
}

If I replicate one script for each of others groups only the last script remains active. How can I solve this?

Best Answer

I've got a solution by creating three different scripts:

  1. auto-sorting table range a3:c16 columntosortby = 2 ;
  2. auto-sorting table range a3:c16 columntosortby = 2 and auto-sorting table range d18:f31 columntosortby = 5;
  3. auto-sorting table range a3:c16 columntosortby = 2 + auto-sorting table range d18:f31 columntosortby = 5 + table range g33:i46 columntosortby = 8

Doesn't seems very elegant to me but it functions.