Academy ↓
When Urgent, Move to Top of Google Sheet
This Google Apps Script function, onEdit(e), automatically moves rows to the top of "Sheet1" when a specific condition is met. Here's how it works:
It detects when a user edits a cell in the sheet.
If the edited cell is in column 3 (C), the row is greater than 1, the new value is "Urgent", and the sheet name is "Sheet1", the function triggers.
It extracts the row's data from columns A to E.
A new row is inserted at the second row (just below the headers).
The extracted data is copied into this new row.
The original row is deleted, ensuring that urgent tasks always appear at the top.
This script is useful for prioritizing urgent tasks dynamically in a Google Sheet.
function onEdit(e) {
var active = SpreadsheetApp.getActiveSheet()
var sheet = SpreadsheetApp.getActiveSheet().getName()
var row = e.range.getRow()
var col = e.range.getColumn()
var value = e.value
if (col == 3 && row > 1 && value == "Urgent" && sheet == "Sheet1"){
// move the row to the top
var data = active.getRange(row,1,1,5).getValues()[0]
// insert a row at the top (before 2)
active.insertRowBefore(2)
// copy the data to that row
active.getRange(2,1,1,5).setValues([data])
// delete the existing row
active.deleteRow(row + 1)
}
}