Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formuladelete-row

Google Sheet script for deleting entire row after a cell in a specific column becomes empty


I am trying to figure out a script with the following process:

If a certain cell in column C becomes empty by removing its value (starting from C5 until the last cell) -> the entire row corresponding to the position of that cell should be deleted (i.e. either row 4,5, etc.) I need this to happen to 6 different sheets.

Can someone please help? I tried nothing yet. I am new to this.

UPDATE (05-01-2023)

Based on the answers below I've been using the following deleterowonChange script, and with an onEdit Google trigger it works.

function deleterowonChange (e){
 
   if (e.range.getColumn() == 3 && e.range.getRow() >= 5 && (!e.value )) {

    var sheets = ["FASHION NL", "FASHION BE","KIDS & UNDERWEAR BNL" ,"NEW BUSINESS BNL" ,"SPORTS & SHOES BNL", "HD&E BNL","test"]; // Please set your expected sheet names;
    var sheet = e.range.getSheet();
    if (sheets.includes(sheet.getSheetName())) {

    var sheet = e.range.getSheet(); // Sheet in which the change was made
      sheet.deleteRow(e.range.getRow());
       e.source.toast('Deletion complete.');
  }
}
}

However when I add my 2nd script with purpose to autosortonEdit, the 2nd one does not work anymore. Somehow they block each other.

function autoSortonEdit(e){

    if (e.range.columnStart == 3 && e.range.getRow() >= 5 && e.range.getValue() != '') {

    var sheets = ["FASHION NL", "FASHION BE","KIDS & UNDERWEAR BNL" ,"NEW BUSINESS BNL" ,"SPORTS & SHOES BNL", "HD&E BNL","test"]; // Please  set your expected sheet names.
    var sheet = e.range.getSheet();
    if (sheets.includes(sheet.getSheetName())){
      
    var range = sheet.getRange("A5:bY600");
    range.sort({ column: 11, ascending: true });
    e.source.toast('Sort complete.');
    }
  }

What does the 'AutosortonEdit' fucntion do?

When a value is added in column C (3), it automatically sorts the entire range based on the date in column 11.

Problem

By adding a value in column C (3), rather than sorting the entire range, it deletes the entire row, as is the initial purpose of the deleterow function.


Solution

  • You probably want to check the "onEdit()" trigger. It will detect changes in the spreadsheet and provide you with an event that describes the change.

    From there, you can check whether the change that was made is indeed the removal of the value in a cell that matches your conditions (third column, fourth row or below), and execute actions (like the deletion of the row) in consequence.

    A simple script to do that would be as follow:

    function onEdit(e) {
      /* When the spreadsheet is edited, check whether 
        - the event was the removal of the value 
        - of a cell in third column
        - and in a row lower than 4 (inclusive)
      */
      if (e.range.getColumn() == 3 && e.range.getRow() >= 4 && (!e.value ||e.value == "")) { // Verify both empty value or the absence of value, for deletions)
        var sheet = e.range.getSheet(); // Sheet in which the change was made
        sheet.deleteRow(e.range.getRow());
      }
    }
    

    If you need other conditions or other triggers (e.g. if there are more sheets than the 6 you mentioned and you want to add a check on the sheet's name as well, etc), check out the docs for that event object, that might give you an idea of other capabilities that might be useful. For actions on the sheets, ranges, etc, check the available methods on their respective documentation pages, if you're really new to this.