I have a tab on a spreadsheet where if you add a new row to the first row specifically, it breaks the formulas in that tab as they are on the first row. Therefore, if someone adds a row to row 1, I want it to be automatically deleted.
The solution I've thought of, is creating a script with an On change trigger. Cell D2 SHOULD have a formula with an output. If someone adds a new row above this, cell D2 will lose that formula and output. So the logic is that if cell D2 in row 1 is blank after being changed, then that row should be deleted. Once it is deleted then the formula should return back.
So far, I have this, but I don't really understand why it's not working, any help is appreciated. The spreadsheet has multiple tabs and this tab is called Enrollment.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange("D2");
var cellContent = cell.getValue();
if (cellContent < 1){
var row = cell.getRow();
sheet.deleteRow(row);
}
}