3

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);
  }
}

1 Answer 1

2

and there you have it:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("preEnrollment");
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Enrollment");
  var cell = sheet.getRange("C2");
  var cellContent = cell.getValue();
  if (cellContent == 0){
    var row = cell.getRow();
    sheet.deleteRow(row);
    sheet2.deleteRow(row);
  }
}
// aww yeah!!!! 
1
  • Congrats on solving your own problem. You said "I don't really understand why it's not working,". Would you add a brief note to explain why this code resolves the problem?
    – Tedinoz
    Commented Aug 28, 2022 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.