Google Sheets Sort by Timestamp When Opening

Google Sheets Sort: You automatically want Google Sheets to sort by time stamp when you open it up.

Sometimes when you are working in your Google Forms response sheet things can get out of order. If your lazy like me and want the sheet to sort automatically you can use this simple script to do so. The google sheets sort script uses the first column to sort by. Both a descending and ascending sort script are included in the below example. I have also uploaded the script to GitHub if you want to grab it from there.

It is also possible to use the google sheets sort script to sort by a different column. Locate this line in the script:


range.sort({column: 1, ascending: false});

Change to column number to the desired column. In this example column: 1 is referring to column A. column: 2 would be column B, column:3 would be column C, and so on.

https://github.com/EtwasShawn/GAS-Sheets-Sort-Test

Google Sheets sort: Directions to install script

1. Open up the Google Spreadsheet where you are saving your form responses and select Tools->Script Editor.

Google Sheets sort 1

2. Copy and paste the following code in to the script editor (this script assumes the sheet name is Form Responses 1).

/*Script to to sort a spreadsheet by timestamp values
Author: Shawn Hodgson
Last Update: 2/24/2018
*/

//For Descending:
function sortSpreadSheetD(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Change Form Responses 1 to your sheetname
var sheet = ss.getSheetByName("Form Responses 1");
var range = sheet.getRange(2,1, sheet.getLastRow(), sheet.getLastColumn());
range.sort({column: 1, ascending: false});
}

//For Ascending:
function sortSpreadSheetA(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Change Form Responses 1 to your sheetname
var sheet = ss.getSheetByName("Form Responses 1");
var range = sheet.getRange(2,1, sheet.getLastRow(), sheet.getLastColumn());
range.sort({column: 1, ascending: true});
}

3. Save the script by clicking the save button in the tool bar. Alternatively you can use Save(Ctrl+S).

Google Sheets sort Save

4. Authorize the script to run by clicking on the “Play” button as shown below:Google Sheets sort Run to Authorize

Google Sheets sort Auth

5. Set the Trigger. When someone opens the spreadsheet it will automatically sort itself.

  • Click on the trigger icon and select: No triggers set up. Click here to add one now. 

Google Sheets sort trigger1

  • Set the drop downs just like below. If you named your function something different you will see that name instead. Save the Trigger, exit the Script Editor, and your done! Google Sheets sort Second Trigger Step

 

You May Also Like

About the Author: shodg001

Leave a Reply