I have the below script running to sort column E in an ascending order. Each row is a different task and column E is the deadline. I have another piece of script running which deletes the deadline in column E once 'Complete' has been selected in another column. Hence, completed tasks will show empty cells in column E.
The deadlines are currently sorting from closest to furthest away, however E cells that are now empty are not moving to the bottom. I am a Scripts newbie and cannot see the issue. Any help to resolve this issue would be much appreciated.
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("2020");
var sheet = ss.getActiveSheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 5;
var tableRange = "A2:I998";
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
}
Best Answer
Issue:
The issue is not the code.
As stated in the official documentation:
Namely, the
onEdit
trigger is activated only when the user edits the value of a particular sheet. If the edit comes from scripts or formulas then theonEdit
function won't be triggered.In your case, you have a script that deletes the content of cells in column E and hence
onEdit
is not triggered and as a result there no sorting is being done.Workaround Solution 1:
onEdit
since the deletion of a value happens by a script.Let's say that the
deleteCell
function is responsible for deleting the content of a cell in column E. As soon as this function runs, after deleting the cell, also sort the data range right after that by calling thesortE
function:In this case,
sortE
won't be manually called by you. You only need to make sure thatdeleteCell
is executed.Workaround Solution 2:
You can create a time trigger to check (every minute or so) if column E contains an empty cell. If it does, then run
sortE
of the script above (in workaround solution 1). To create such a trigger forsortE
, you can runcreateTrigger
once (just make suresortE
is somewhere in the script editor):and execute only and once
createTrigger
to initiate the trigger: