Kasper Odgaard
Kasper Odgaard

Reputation: 113

Automatically sort rows when data is changed

In my Google sheet, I have 2 columns, A and B, which I would like to sort descending by values of Column B. This I know can be done using filter/sort options in sheets. This works as expected but this is a manual step that should be repeated every time data in column B changes.

I would like all rows to be automatically sorted when data in Column B changes. Any ideas to do this?

Example of data in column A and B

Team    Score
Team A  13
Team C  12
Team B  11
Team D  5

Upvotes: 1

Views: 2402

Answers (1)

player0
player0

Reputation: 1

add this script to your sheet:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A1:Z");

function onEdit(e)  {
  range.sort([{column: 2, ascending: false}]);
}

  • Sheet1 = name of the sheet
  • A1:Z = range to be sorted
  • column: 2 = column B
  • ascending: false = descending

Upvotes: 1

Related Questions