Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Sorting on Sheets

I'm trying to figure out how to sort my sheet alphabetically automatically. Whenever I put a new entry under columns A-C, I would like it to automatically sort together with the rest of the data.

I've heard that this must be done using Google apps scripts. Can anyone help me with that?

Thanks in advance!

https://docs.google.com/spreadsheets/d/1XH4mrKa6W4se5WwM6oKqh959gG2kAQVtAmOnml13VoE/edit#gid=0

like image 993
bobatapioca Avatar asked Feb 22 '16 20:02

bobatapioca


People also ask

How do you sort in Google Sheets dynamically?

This is the simplest step. Just click on cell G1 and insert a Tick box from the Insert menu. For your information, when ticked, the vale in the cell G1 will be TRUE else FALSE. If the value is TRUE, in sorting, it can be used to sort the data in ascending (A->Z) order else descending (Z->A) order.

How do I sort data quickly in Google Sheets?

Sort an entire sheet On your computer, open a spreadsheet in Google Sheets. At the top, right-click the letter of the column you want to sort by. Click Sort sheet A to Z or Sort sheet Z to A.


2 Answers

Spreadsheets are easy to sort from a script and a script can easily be triggered by a spreadsheet "event".

onEdit is one of these events that should fit your demand. Doc here and here.

then the sort process is shown in the doc, I reproduce the code below :

var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A1:C7");

 // Sorts by the values in the first column (A)
 range.sort(1);

 // Sorts by the values in the second column (B)
 range.sort(2);

 // Sorts descending by column B
 range.sort({column: 2, ascending: false});

 // Sorts descending by column B, then ascending by column A
 // Note the use of an array
 range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]);

 // For rows that are sorted in ascending order, the "ascending" parameter is
 // optional, and just an integer with the column can be used instead. Note that
 // in general, keeping the sort specification consistent results in more readable
 // code. We could have expressed the earlier sort as:
 range.sort([{column: 2, ascending: false}, 1]);

 // Alternatively, if we wanted all columns to be in ascending order, we would use
 // the following (this would make column 2 ascending)
 range.sort([2, 1]);
 // ... which is equivalent to
 range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]);
like image 185
Serge insas Avatar answered Oct 02 '22 05:10

Serge insas


This might have been updated as I could not get the top answer to work. Here is what I was able to use.

Where you reverse the order of priority: In this example column 2 is the highest priority and column 3 is the secondary sort.

const  ss = SpreadsheetApp.getActiveSpreadsheet();
ss.sort(3).sort(2);
like image 30
cfahey Avatar answered Oct 02 '22 05:10

cfahey