0
votes

I am looking to be pulling in some data into a Google Sheet from an external source at 1am daily. The amount of products imported will vary.

I then wish to sort the data by price after the data has finished uploading, this will be around 1.05am.

I see it is possible to run functions for Google Sheets on a timed basis.

What script would I need to sort the data by price, so the cheapest items is row 2 after running a script every morning.

To do this manually in Google Sheets I would do - *Highlight rows 2 to , Data, Sort Range, Sort by column I, A - Z

How would this translate into a function? the timed function look easy enough to do as a trigger (although a set time doesn;t seem possible only an hour range)

Here is the shared sheet

https://docs.google.com/spreadsheets/d/1hRW92xesCZzrTRU8DzrdJ_XtrQoJbbNhA3nVT452 apE/edit?usp=sharing

SOLUTION - Thanks to Ed & Cooper

First, freeze the first row manually. (Search for freeze) https://support.google.com/docs/answer/54813?hl=en

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the column I - the ninth across - , in ascending order 
sheet.sort(9);
}
1
do credit the correct answer by marking it as accepted answer - Rohan Sharma

1 Answers

0
votes

Set this function to run one hour after you data is available:

function sortPrice() {
  var ss=SpreadsheetApp.openById("1XHOlmI6LA4jvnh1YXossBrbrnIOdyRmvO2warz1vl9s")
  var s=ss.getSheetByName("4cat").activate()
  var lr =s.getLastRow()
  var lc= s.getLastColumn()
  var rng=s.getRange(2, 1, lr, lc)//get range to sort
  var sort= rng.sort({column: 9, ascending: true}) //sort on column I
}