1
votes

I've made a script that takes data from a spreadsheet , creates a Google Doc and presents selected rows in a Table format in the Doc. Now I'd like to be able to sort that table Alphabetically before the table is created. I've tried using a couple of different approaches like this

function onEdit(){
   var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();   
   var editedCell = sh.getActiveRange().getColumnIndex();   
   if(editedCell == 2) {   
       var range = sh.getRange("A2:B10");   
       range.sort({column: 2});   
   }
 }  

This kind of sort works but only onEdit(). I'd like to sort before edit or find away to sort the table as the Google Doc is being generated. Does anyone have any suggestions.

Many thanks in advance.

1

1 Answers

0
votes

Use the onOpen() function instead of the onEdit() function.

Note that you will have to open the document, not just refresh the page to get the function to run.