0
votes

So I have a spreadsheet that has some meta data in the first ~6 rows, and starting on row 7, is a list of names (A7 is the start), and each row has additional data in other columns. I need the entire row sorted, but by A-Z using the first column.

How would I create a google script to automatically sort these rows using the first column as an index, with an offset to only apply starting at row 7?

1
Are you looking for step-by-step instructions? What will trigger the sort?Rubén

1 Answers

0
votes

Try this:

function myFunction() {
  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var s=ss.getActiveSheet()
  var lr= s.getLastRow()
  var lc=s.getLastColumn()
  var data=s.getRange(7,1,lr,lc)
  data.sort( { column : 1, ascending: true } )
}

Or you could run it onEdit of column A and row >=7 like this:

function onEdit(e) {
  var editRow=e.range.getSheet().getActiveCell().getRow()
  var editColumn=e.range.getSheet().getActiveCell().getColumn()
  if(editColumn==1 && editRow>=7){ 
    var ss=SpreadsheetApp.getActiveSpreadsheet()
    var s=ss.getActiveSheet()
    var lr= s.getLastRow()
    var lc=s.getLastColumn()
    var data=s.getRange(7,1,lr,lc)
      data.sort( { column : 1, ascending: true } )
  }}