Delete a row in Google Spreadsheets if value of cell in said row is 0 or blank

前端 未结 10 699
情书的邮戳
情书的邮戳 2020-11-29 02:01

I\'d like to be able to delete an entire row in a Google Spreadsheets if the value entered for say column \"C\" in that row is 0 or blank. Is there a simple script I could w

相关标签:
10条回答
  • 2020-11-29 02:26
    function onEdit(e) {
      //Logger.log(JSON.stringify(e)); 
      //{"source":{},"range":{"rowStart":1,"rowEnd":1,"columnEnd":1,"columnStart":1},"value":"1","user":{"email":"","nickname":""},"authMode":{}}
      try {
        var ss = e.source; // Just pull the spreadsheet object from the one already being passed to onEdit
        var s = ss.getActiveSheet();
    
        // Conditions are by sheet and a single cell in a certain column
        if (s.getName() == 'Sheet1' &&  // change to your own 
            e.range.columnStart == 3 && e.range.columnEnd == 3 &&  // only look at edits happening in col C which is 3
            e.range.rowStart == e.range.rowEnd ) {  // only look at single row edits which will equal a single cell
          checkCellValue(e); 
        }
      } catch (error) { Logger.log(error); }
    };
    
    function checkCellValue(e) {
      if ( !e.value || e.value == 0) {  // Delete if value is zero or empty
        e.source.getActiveSheet().deleteRow(e.range.rowStart);
      }
    }
    

    This only looks at the value from a single cell edit now and not the values in the whole sheet.

    0 讨论(0)
  • 2020-11-29 02:36

    I can suggest a simple solution without using a script !!

    Lets say you want to delete rows with empty text in column C. Sort the data (Data Menu -> Sort sheet by column C, A->Z) in the sheet w.r.t column C, so all your empty text rows will be available together.

    Just select those rows all together and right-click -> delete rows. Then you can re-sort your data according to the column you need. Done.

    0 讨论(0)
  • 2020-11-29 02:36

    I was having a few problems with scripts so my workaround was to use the "Filter" tool.

    1. Select all spreadsheet data
    2. Click filter tool icon (looks like wine glass)
    3. Click the newly available filter icon in the first cell of the column you wish to search.
    4. Select "Filter By Condition" > Set the conditions (I was using "Text Contains" > "word")

    This will leave the rows that contain the word your searching for and they can be deleted by bulk selecting them while holding the shift key > right click > delete rows.

    0 讨论(0)
  • 2020-11-29 02:37

    The below code was able to delete rows containing a date more than 50 days before today in a particular column G , move these row values to back up sheet and delete the rows from source sheet.

    The code is better as it deletes the rows at one go rather than deleting one by one. Runs much faster.

    It does not copy back values like some solutions suggested (by pushing into an array and copying back to sheet). If I follow that logic, I am losing formulas contained in these cells.

    I run the function everyday in the night (scheduled) when no one is using the sheet.

    function delete_old(){
      //delete > 50 day old records and copy to backup
      //run daily from owner login
      var ss = SpreadsheetApp.getActiveSpreadsheet();  
      var bill = ss.getSheetByName("Allotted");
      var backss = SpreadsheetApp.openById("..."); //backup spreadsheet
      var bill2 = backss.getSheetByName("Allotted");
      var today=new Date();
      //process allotted sheet (bills)
      bill.getRange(1, 1, bill.getMaxRows(), bill.getMaxColumns()).activate();
      ss.getActiveRange().offset(1, 0, ss.getActiveRange().getNumRows() - 1).sort({column: 7, ascending: true});
      var data = bill.getDataRange().getValues();
      var delData = new Array();
      for(n=data.length-1; n>1; n--){
        if(data[n][6] !=="" &&  data[n][6]  < today.getTime()-(50*24*3600*1000) ){  //change the condition as per your situation
          delData.push(data[n]);
        }//if
       }//for
       //get first and last row no to be deleted
       for(n=1;n<data.length; n++){
        if(data[n][6] !=="" &&  data[n][6]  < today.getTime()-(50*24*3600*1000) ){ 
          var strow=n+1 ; //first row
          break
        }//if
       }//for
       for(n=data.length-1; n>1; n--){
        if(data[n][6] !=="" &&  data[n][6]  < today.getTime()-(50*24*3600*1000) ){ 
          var ltrow=n+1 ; //last row
          break
        }//if
       }//for
       var bill2lr=bill2.getLastRow();
       bill2.getRange((bill2lr+1),1,delData.length,delData[0].length).setValues(delData);
       bill.deleteRows(strow, 1+ltrow-strow);
       bill.getRange(1, 1, bill.getMaxRows(), bill.getMaxColumns()).activate();
       ss.getActiveRange().offset(1, 0, ss.getActiveRange().getNumRows() - 1).sort({column: 6, ascending: true}); //get back ordinal sorting order as per column F
    }//function
    
    0 讨论(0)
  • 2020-11-29 02:38

    reading your question carefully, I came up with this solution:

    function onOpen() {
      // get active spreadsheet
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    
      // create menu
      var menu = [{name: "Evaluate Column C", functionName: "deleteRow"}];
    
      // add to menu
      ss.addMenu("Check", menu);
    }
    
    function deleteRow() {
      // get active spreadsheet
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    
      // get active/selected row
      var activeRow = ss.getActiveRange().getRowIndex();
    
      // get content column C
      var columnC = ss.getRange("C"+activeRow).getValue();
    
      // evaluate whether content is blank or 0 (null)
      if (columnC == '' || columnC == 0) {
        ss.deleteRow(parseInt(activeRow));
      }
    }
    

    This script will create a menu upon file load and will enable you to delete a row, based on those criteria set in column C, or not.

    0 讨论(0)
  • 2020-11-29 02:48

    quite simple request. Try this :

     function try_It(){
     deleteRow(2); //// choose col = 2 for column C
     }
    
     function deleteRow(col){ // col is the index of the column to check for 0 or empty
     var sh = SpreadsheetApp.getActiveSheet();
     var data = sh.getDataRange().getValues();
     var targetData = new Array();
     for(n=0;n<data.length;++n){
     if(data[n][col]!='' && data[n][col]!=0){ targetData.push(data[n])};
     }
     Logger.log(targetData);
     sh.getDataRange().clear();
     sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
     }
    

    EDIT : re-reading the question I'm not sure if the question is asking for a 'live' on Edit function or a function (like this above) to apply after data has been entered... It's not very clear to me... so feel free to be more accurate if necessary ;)

    0 讨论(0)
提交回复
热议问题