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

前端 未结 10 700
情书的邮戳
情书的邮戳 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:48

    This simple code did the job for me!

        function myFunction() {
    
          var ss = SpreadsheetApp.getActiveSpreadsheet();  // get active spreadsheet
    
          var activeRow = ss.getActiveRange().getRowIndex();  // get active/selected row
    
    var start=1;
    var end=650;
    var match='';
    var match2=0;   //Edit this according to your choice.
    
         for (var i = start; i <= end; i++) {
          var columnC = ss.getRange("C"+i).getValue();
          if (columnC ==match  || columnC ==match2){ ss.deleteRow(i); }
         }
        }
    
    0 讨论(0)
  • 2020-11-29 02:50

    This is what I managed to make work. You can see that I looped backwards through the sheet so that as a row was deleted the next row wouldn't be skipped. I hope this helps somebody.

      function UpdateLog() {
    
      var returnSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RetLog');
      var rowCount = returnSheet.getLastRow();
    
      for (i = rowCount; i > 0; i--) {
        var rrCell = 'G' + i;
        var cell = returnSheet.getRange(rrCell).getValue();
        if (cell > 0 ){
          logSheet.
          returnSheet.deleteRow(i);
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 02:51

    There is a simpler way:

    1. Use filtering to only show the rows which you want to delete. For example, my column based on which I want to delete rows had categories on them, A, B, C. Through the filtering interface I selected only A and B, which I wanted to delete.
    2. Select all rows and delete them. Doing this, in my example, effectively selected all A and B rows and deleted them; now my spreadsheet does not show any rows.
    3. Turn off the filter. This unhides my C rows. Done!
    0 讨论(0)
  • 2020-11-29 02:52

    I wrote this script to do the same thing for one of my Google spreadsheets. I wanted to be able to run the script after all the data was in the spreadsheet so I have the script adding a menu option to run the script.

    /**
     * Deletes rows in the active spreadsheet that contain 0 or
     * a blank valuein column "C". 
     * For more information on using the Spreadsheet API, see
     * https://developers.google.com/apps-script/service_spreadsheet
     */
    function readRows() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var rows = sheet.getDataRange();
      var numRows = rows.getNumRows();
      var values = rows.getValues();
    
      var rowsDeleted = 0;
      for (var i = 0; i <= numRows - 1; i++) {
        var row = values[i];
        if (row[2] == 0 || row[2] == '') {
          sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
          rowsDeleted++;
        }
      }
    };
    
    /**
     * Adds a custom menu to the active spreadsheet, containing a single menu item
     * for invoking the readRows() function specified above.
     * The onOpen() function, when defined, is automatically invoked whenever the
     * spreadsheet is opened.
     * For more information on using the Spreadsheet API, see
     * https://developers.google.com/apps-script/service_spreadsheet
     */
    function onOpen() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var entries = [{
        name : "Remove rows where column C is 0 or blank",
        functionName : "readRows"
      }];
      sheet.addMenu("Script Center Menu", entries);
    };
    

    Test spreadsheet before:

    enter image description here

    Running script from menu:

    enter image description here

    After running script:

    enter image description here

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