'Google sheet script to delete row if certain cells are empty and add row at the bottom [closed]

I have a sheet with a table with content in the range B6:Y1000. I would like a script that would delete all rows if column C-D-E-F-G are empty. I also need that this script add 5 new rows at the bottom after the last row with content in column C-D-E-F-G. All the added rows have to contain formulas. The added rows make it possible for me to continue filling the table, because the first function deletes all the rows. This is my sheet:

My Sheet

Thanks for your insight!



Solution 1:[1]

Try

function removeEmpty() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
  const empty_rows = [];
  const lastRow = sh.getLastRow()
  Browser.msgBox(lastRow)
  const data = sh.getRange("C6:G" + lastRow).getValues();
  for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
  empty_rows.reverse().forEach(x => sh.deleteRow(x));
  sh.insertRowsAfter(lastRow - empty_rows.length, 5)

  var rng = sh.getRange('A6:Z6')
  rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);

  // H-I-J-K-L
  var rng = sh.getRange('H' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('H' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  // O
  var rng = sh.getRange('O' + (lastRow - empty_rows.length) + ':O' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('O' + (lastRow - empty_rows.length + 1) + ':O' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  // Q-R-S-T-U
  var rng = sh.getRange('Q' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('Q' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  // X-Y 
  var rng = sh.getRange('X' + (lastRow - empty_rows.length) + ':Y' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('X' + (lastRow - empty_rows.length + 1) + ':Y' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);

}

Solution 2:[2]

Delete Rows

function drows() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet Name");
  let d = 0;
  sh.getRange("B6:Y1000").getValues().forEach((r, i) => {
    if (!r[2] && !r[3] && !r[4] && !r[5] && !r[6]) {
      sh.deleteRow(i + 6 - d++);
    }
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mozart75
Solution 2 Cooper