Reputation: 137
I have a Google Sheets that registers the entries from a Google Form. I set up a script to autosort the sheet every time the sheet is opened and a trigger to run the script on open, but no sorting is happening. I've limited knowledge of scripting and javascript. What's wrong with the following script?
function OrderByEvent() {
// The numeric index of the column you wish to keep auto-sorted. A = 1, B = 2,
// and so on.
var SORT_COLUMN_INDEX = 10;
// Whether to sort the data in ascending or descending order. false=ascending and true=descending
var ASCENDING = true;
// If you have header rows in your sheet, specify how many to exclude them from
// the sort.
var NUMBER_OF_HEADER_ROWS = 1;
// No need to edit anything below this line for general use.
// Make an improvement? Ping me on GitHub and let me know!
// Keep track of the active sheet.
var activeSheet;
/**
* Automatically sorts on the pre-defined column.
*
* @param {Sheet} sheet The sheet to sort.
*/
function autoSort(sheet) {
// Get the entire set of data for this sheet.
var range = sheet.getDataRange();
// Then, if there are any header rows, offset our range to remove them from
// it; otherwise, they will end up being sorted as well.
if (NUMBER_OF_HEADER_ROWS > 0) {
// Setting the second parameter of offset() to 0 to prevents it from
// shifting any columns. Note that row headers wouldn't make much
// sense here, but this is where you would modify it if you
// wanted support for those as well.
range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
}
// Perform the actual sort.
range.sort( {
column: SORT_COLUMN_INDEX,
ascending: ASCENDING
} );
}
function onOpen(event) {
activeSheet = SpreadsheetApp.getActiveSheet();
{autoSort(activeSheet);
}
}
/**
* Triggers when a sheet is edited, and calls the auto sort function if the
* edited cell is in the column we're looking to sort.
*
* @param {Object} event The triggering event.
*/
//function onEdit(event) {
// var editedCell;
// Update the active sheet in case it changed.
// activeSheet = SpreadsheetApp.getActiveSheet();
// Get the cell that was just modified.
// editedCell = activeSheet.getActiveCell();
// Only trigger a re-sort if the user edited data in the column they're
// sorting by; otherwise, we perform unnecessary additional sorts if
// the targeted sort column's data didn't change.
// if (editedCell.getColumn() == SORT_COLUMN_INDEX) {
// autoSort(activeSheet);
// }
//}
}
Upvotes: 1
Views: 355
Reputation: 201713
How about this answer?
onOpen()
which is used as a simple trigger is been putting in a function of OrderByEvent()
. By this, when the Spreadsheet is opened, onOpen()
is not run. Even when OrderByEvent()
is installed as the installable trigger, when OrderByEvent()
is run, onOpen()
is not run.
SpreadsheetApp.getActiveSheet()
can be modified to event.source
in your script. Of course, event.source.getActiveSheet()
can be used.When above points are reflected to your script, it becomes as follows. Please think of this as just one of several answers.
function autoSort(sheet) {
var SORT_COLUMN_INDEX = 10;
var ASCENDING = true;
var NUMBER_OF_HEADER_ROWS = 1;
var range = sheet.getDataRange();
if (NUMBER_OF_HEADER_ROWS > 0) {
range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
}
range.sort( {
column: SORT_COLUMN_INDEX,
ascending: ASCENDING
} );
}
function onOpen(event) {
autoSort(event.source);
}
onOpen()
is automatically run and works when the Spreadsheet is opened.If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 1