Deleting ALL empty rows in a Google Spreadsheet











up vote
5
down vote

favorite
1












I've just started using Google Apps script to manage some sheets for a project i'm working on, I am new to Javascript so please go easy if there are any howlers in my code!.



We have and app called forms2mobile that captures data and drops it into a Google spreadsheet. It actually drops different data into different sheets depending on which part of the app you use.



I've hacked together a script that pulls all data from one sheet (source), and drops only certain columns into a second sheet (destination). It then deletes all rows from the source, and any blank rows from the destination.



The problem I have is with deleting blank rows from the destination. Typically the destination will have empty rows at the bottom, and the code I have will only delete empty rows within the range that contains data. So i'm always left with empty rows at the bottom.



The destination sheet will then be used as a data source for forms2mobile, which of course isn't happy with empty rows.



I've found the class getMaxRows() but i'm not sure how to implement it. If anyone could make any suggestions that would be great.



Cheers
Paul



    function NEW_copyColumnNumbers( ) {
var spreadsheet_source = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
var spreadsheet_target = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
var range_input = spreadsheet_source.getRange("A2:CC407");
var range_output = spreadsheet_target.getRange("A"+(spreadsheet_target.getLastRow()+1));
var keep_columns = [66,66,10,11,12,13,14,23,26,31,69,71,74,75,80];

copyColumnNumbers(range_input, range_output, keep_columns);
clearEmptyRows();
clearSourceData();
}
function copyColumnNumbers( range_input, range_output, columns_keep_num ) {
// Create an array of arrays containing the values in the input range.
var range_values = range_input.getValues();
// Loop through each inner array.
for ( var i = 0, row_count = range_values.length; i < row_count; i++ ) {
// Loop through the indices to keep and use these indices to
// select values from the inner array.
for ( j = 0, col_keep_count = columns_keep_num.length; j < col_keep_count; j++ ) {
// Capture the value to keep
var keep_val = range_values[i][columns_keep_num[j]];
// Write the value to the output using the offset method of the output range argument.
range_output.offset(i,j).setValue(keep_val);
}
}
}
function clearEmptyRows() {
var ss = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
var s = ss.getActiveSheet();
var values = s.getDataRange().getValues();
nextLine: for( var i = values.length-1; i >=0; i-- ) {
for( var j = 0; j < values[i].length; j++ )
if( values[i][j] != "" )
continue nextLine;
s.deleteRow(i+1);
}
//I iterate it backwards on purpose, so I do not have to calculate the indexes after a removal
}
function clearSourceData() {
var ss = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
var s = ss.getActiveSheet();
var data = s.getDataRange().getValues();
for(var n =data.length+1 ; n<0 ; n--){
if(data[n][0]!=''){n++;break}
}
s.deleteRows(2, (s.getLastRow()-1));
}









share|improve this question


























    up vote
    5
    down vote

    favorite
    1












    I've just started using Google Apps script to manage some sheets for a project i'm working on, I am new to Javascript so please go easy if there are any howlers in my code!.



    We have and app called forms2mobile that captures data and drops it into a Google spreadsheet. It actually drops different data into different sheets depending on which part of the app you use.



    I've hacked together a script that pulls all data from one sheet (source), and drops only certain columns into a second sheet (destination). It then deletes all rows from the source, and any blank rows from the destination.



    The problem I have is with deleting blank rows from the destination. Typically the destination will have empty rows at the bottom, and the code I have will only delete empty rows within the range that contains data. So i'm always left with empty rows at the bottom.



    The destination sheet will then be used as a data source for forms2mobile, which of course isn't happy with empty rows.



    I've found the class getMaxRows() but i'm not sure how to implement it. If anyone could make any suggestions that would be great.



    Cheers
    Paul



        function NEW_copyColumnNumbers( ) {
    var spreadsheet_source = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
    var spreadsheet_target = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
    var range_input = spreadsheet_source.getRange("A2:CC407");
    var range_output = spreadsheet_target.getRange("A"+(spreadsheet_target.getLastRow()+1));
    var keep_columns = [66,66,10,11,12,13,14,23,26,31,69,71,74,75,80];

    copyColumnNumbers(range_input, range_output, keep_columns);
    clearEmptyRows();
    clearSourceData();
    }
    function copyColumnNumbers( range_input, range_output, columns_keep_num ) {
    // Create an array of arrays containing the values in the input range.
    var range_values = range_input.getValues();
    // Loop through each inner array.
    for ( var i = 0, row_count = range_values.length; i < row_count; i++ ) {
    // Loop through the indices to keep and use these indices to
    // select values from the inner array.
    for ( j = 0, col_keep_count = columns_keep_num.length; j < col_keep_count; j++ ) {
    // Capture the value to keep
    var keep_val = range_values[i][columns_keep_num[j]];
    // Write the value to the output using the offset method of the output range argument.
    range_output.offset(i,j).setValue(keep_val);
    }
    }
    }
    function clearEmptyRows() {
    var ss = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
    var s = ss.getActiveSheet();
    var values = s.getDataRange().getValues();
    nextLine: for( var i = values.length-1; i >=0; i-- ) {
    for( var j = 0; j < values[i].length; j++ )
    if( values[i][j] != "" )
    continue nextLine;
    s.deleteRow(i+1);
    }
    //I iterate it backwards on purpose, so I do not have to calculate the indexes after a removal
    }
    function clearSourceData() {
    var ss = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
    var s = ss.getActiveSheet();
    var data = s.getDataRange().getValues();
    for(var n =data.length+1 ; n<0 ; n--){
    if(data[n][0]!=''){n++;break}
    }
    s.deleteRows(2, (s.getLastRow()-1));
    }









    share|improve this question
























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      I've just started using Google Apps script to manage some sheets for a project i'm working on, I am new to Javascript so please go easy if there are any howlers in my code!.



      We have and app called forms2mobile that captures data and drops it into a Google spreadsheet. It actually drops different data into different sheets depending on which part of the app you use.



      I've hacked together a script that pulls all data from one sheet (source), and drops only certain columns into a second sheet (destination). It then deletes all rows from the source, and any blank rows from the destination.



      The problem I have is with deleting blank rows from the destination. Typically the destination will have empty rows at the bottom, and the code I have will only delete empty rows within the range that contains data. So i'm always left with empty rows at the bottom.



      The destination sheet will then be used as a data source for forms2mobile, which of course isn't happy with empty rows.



      I've found the class getMaxRows() but i'm not sure how to implement it. If anyone could make any suggestions that would be great.



      Cheers
      Paul



          function NEW_copyColumnNumbers( ) {
      var spreadsheet_source = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
      var spreadsheet_target = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
      var range_input = spreadsheet_source.getRange("A2:CC407");
      var range_output = spreadsheet_target.getRange("A"+(spreadsheet_target.getLastRow()+1));
      var keep_columns = [66,66,10,11,12,13,14,23,26,31,69,71,74,75,80];

      copyColumnNumbers(range_input, range_output, keep_columns);
      clearEmptyRows();
      clearSourceData();
      }
      function copyColumnNumbers( range_input, range_output, columns_keep_num ) {
      // Create an array of arrays containing the values in the input range.
      var range_values = range_input.getValues();
      // Loop through each inner array.
      for ( var i = 0, row_count = range_values.length; i < row_count; i++ ) {
      // Loop through the indices to keep and use these indices to
      // select values from the inner array.
      for ( j = 0, col_keep_count = columns_keep_num.length; j < col_keep_count; j++ ) {
      // Capture the value to keep
      var keep_val = range_values[i][columns_keep_num[j]];
      // Write the value to the output using the offset method of the output range argument.
      range_output.offset(i,j).setValue(keep_val);
      }
      }
      }
      function clearEmptyRows() {
      var ss = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
      var s = ss.getActiveSheet();
      var values = s.getDataRange().getValues();
      nextLine: for( var i = values.length-1; i >=0; i-- ) {
      for( var j = 0; j < values[i].length; j++ )
      if( values[i][j] != "" )
      continue nextLine;
      s.deleteRow(i+1);
      }
      //I iterate it backwards on purpose, so I do not have to calculate the indexes after a removal
      }
      function clearSourceData() {
      var ss = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
      var s = ss.getActiveSheet();
      var data = s.getDataRange().getValues();
      for(var n =data.length+1 ; n<0 ; n--){
      if(data[n][0]!=''){n++;break}
      }
      s.deleteRows(2, (s.getLastRow()-1));
      }









      share|improve this question













      I've just started using Google Apps script to manage some sheets for a project i'm working on, I am new to Javascript so please go easy if there are any howlers in my code!.



      We have and app called forms2mobile that captures data and drops it into a Google spreadsheet. It actually drops different data into different sheets depending on which part of the app you use.



      I've hacked together a script that pulls all data from one sheet (source), and drops only certain columns into a second sheet (destination). It then deletes all rows from the source, and any blank rows from the destination.



      The problem I have is with deleting blank rows from the destination. Typically the destination will have empty rows at the bottom, and the code I have will only delete empty rows within the range that contains data. So i'm always left with empty rows at the bottom.



      The destination sheet will then be used as a data source for forms2mobile, which of course isn't happy with empty rows.



      I've found the class getMaxRows() but i'm not sure how to implement it. If anyone could make any suggestions that would be great.



      Cheers
      Paul



          function NEW_copyColumnNumbers( ) {
      var spreadsheet_source = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
      var spreadsheet_target = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
      var range_input = spreadsheet_source.getRange("A2:CC407");
      var range_output = spreadsheet_target.getRange("A"+(spreadsheet_target.getLastRow()+1));
      var keep_columns = [66,66,10,11,12,13,14,23,26,31,69,71,74,75,80];

      copyColumnNumbers(range_input, range_output, keep_columns);
      clearEmptyRows();
      clearSourceData();
      }
      function copyColumnNumbers( range_input, range_output, columns_keep_num ) {
      // Create an array of arrays containing the values in the input range.
      var range_values = range_input.getValues();
      // Loop through each inner array.
      for ( var i = 0, row_count = range_values.length; i < row_count; i++ ) {
      // Loop through the indices to keep and use these indices to
      // select values from the inner array.
      for ( j = 0, col_keep_count = columns_keep_num.length; j < col_keep_count; j++ ) {
      // Capture the value to keep
      var keep_val = range_values[i][columns_keep_num[j]];
      // Write the value to the output using the offset method of the output range argument.
      range_output.offset(i,j).setValue(keep_val);
      }
      }
      }
      function clearEmptyRows() {
      var ss = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
      var s = ss.getActiveSheet();
      var values = s.getDataRange().getValues();
      nextLine: for( var i = values.length-1; i >=0; i-- ) {
      for( var j = 0; j < values[i].length; j++ )
      if( values[i][j] != "" )
      continue nextLine;
      s.deleteRow(i+1);
      }
      //I iterate it backwards on purpose, so I do not have to calculate the indexes after a removal
      }
      function clearSourceData() {
      var ss = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
      var s = ss.getActiveSheet();
      var data = s.getDataRange().getValues();
      for(var n =data.length+1 ; n<0 ; n--){
      if(data[n][0]!=''){n++;break}
      }
      s.deleteRows(2, (s.getLastRow()-1));
      }






      javascript google-apps-script google-sheets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 15 '14 at 15:36









      user2136368

      28113




      28113
























          6 Answers
          6






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          This is how it works :



          function removeEmptyRows(){
          var sh = SpreadsheetApp.getActiveSheet();
          var maxRows = sh.getMaxRows();
          var lastRow = sh.getLastRow();
          sh.deleteRows(lastRow+1, maxRows-lastRow);
          }


          Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)



          EDIT



          by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !



          function deleteEmptyRows(){ 
          var sh = SpreadsheetApp.getActiveSheet();
          var data = sh.getDataRange().getValues();
          var targetData = new Array();
          for(n=0;n<data.length;++n){
          if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
          Logger.log(data[n].join().replace(/,/g,''))
          }
          sh.getDataRange().clear();
          sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
          }


          Demo sheet in view only - make a copy to use






          share|improve this answer























          • Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
            – user2136368
            Oct 20 '14 at 12:56












          • Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
            – Serge insas
            Oct 20 '14 at 14:55


















          up vote
          4
          down vote













          Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.




          Place this if around the last line function removeEmptyRows() and it
          will not throw an error:




            if (maxRows-lastRow != 0){
          sh.deleteRows(lastRow+1, maxRows-lastRow);
          }





          share|improve this answer




























            up vote
            3
            down vote













            Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.



            //Remove All Empty Columns in the Entire Workbook
            function removeEmptyColumns() {
            var ss = SpreadsheetApp.getActive();
            var allsheets = ss.getSheets();
            for (var s in allsheets){
            var sheet=allsheets[s]
            var maxColumns = sheet.getMaxColumns();
            var lastColumn = sheet.getLastColumn();
            if (maxColumns-lastColumn != 0){
            sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
            }
            }
            }

            //Remove All Empty Rows in the Entire Workbook
            function removeEmptyRows() {
            var ss = SpreadsheetApp.getActive();
            var allsheets = ss.getSheets();
            for (var s in allsheets){
            var sheet=allsheets[s]
            var maxRows = sheet.getMaxRows();
            var lastRow = sheet.getLastRow();
            if (maxRows-lastRow != 0){
            sheet.deleteRows(lastRow+1, maxRows-lastRow);
            }
            }
            }





            share|improve this answer




























              up vote
              1
              down vote













              Removing all empty lines (bottom-up)



              before



              enter image description here



              after



              enter image description here



              function isEmptyRow(row){
              for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
              var cell = row[columnIndex];
              if (cell){
              return false;
              }
              }
              return true;
              }

              function removeEmptyLines(sheet){
              var lastRowIndex = sheet.getLastRow();
              var lastColumnIndex = sheet.getLastColumn();
              var maxRowIndex = sheet.getMaxRows();
              var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
              var data = range.getValues();
              sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);

              for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
              var row = data[rowIndex];

              if (isEmptyRow(row)){
              sheet.deleteRow(rowIndex + 1);
              }
              }

              }


              function removeEmptyLinesFromAllSheets(){
              SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
              }




              Removing only empty lines from below and above the data



              before



              enter image description here



              after



              enter image description here



              function isEmptyRow(row){
              for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
              var cell = row[columnIndex];
              if (cell){
              return false;
              }
              }
              return true;
              }

              function getFirstNonBlankRowIndex(data){
              for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
              var row = data[rowIndex];

              if (!isEmptyRow(row)){
              return rowIndex;
              }
              }
              return 0;
              }

              function removePaddedEmptyLines(sheet){
              var lastRowIndex = sheet.getLastRow();
              var lastColumnIndex = sheet.getLastColumn();
              var maxRowIndex = sheet.getMaxRows();
              var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
              var data = range.getValues();
              var firstRowIndex = getFirstNonBlankRowIndex(data);
              sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
              sheet.deleteRows(1, firstRowIndex);
              }


              function removePaddedEmptyLinesFromAllSheets(){
              SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
              }





              share|improve this answer




























                up vote
                0
                down vote













                I have tried this piece of code and it works good, you may take a look and try it:



                function DeleteBlankRows(){
                var sh = SpreadsheetApp.getActiveSheet();
                var maxRows = sh.getMaxRows();
                var lastRow = sh.getLastRow();
                for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
                {
                if( sh.getRange('A'+Raw).getValue() == '')
                {
                sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
                }
                }





                share|improve this answer






























                  up vote
                  0
                  down vote













                  This works perfectly for me.



                  function removeEmptyRows(){
                  var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
                  var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
                  var maxRows = sh.getMaxRows();
                  var lastRow = sh.getLastRow();
                  sh.deleteRows(lastRow+1, maxRows-lastRow);
                  }





                  share|improve this answer





















                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26386492%2fdeleting-all-empty-rows-in-a-google-spreadsheet%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    9
                    down vote



                    accepted










                    This is how it works :



                    function removeEmptyRows(){
                    var sh = SpreadsheetApp.getActiveSheet();
                    var maxRows = sh.getMaxRows();
                    var lastRow = sh.getLastRow();
                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                    }


                    Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)



                    EDIT



                    by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !



                    function deleteEmptyRows(){ 
                    var sh = SpreadsheetApp.getActiveSheet();
                    var data = sh.getDataRange().getValues();
                    var targetData = new Array();
                    for(n=0;n<data.length;++n){
                    if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
                    Logger.log(data[n].join().replace(/,/g,''))
                    }
                    sh.getDataRange().clear();
                    sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
                    }


                    Demo sheet in view only - make a copy to use






                    share|improve this answer























                    • Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                      – user2136368
                      Oct 20 '14 at 12:56












                    • Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                      – Serge insas
                      Oct 20 '14 at 14:55















                    up vote
                    9
                    down vote



                    accepted










                    This is how it works :



                    function removeEmptyRows(){
                    var sh = SpreadsheetApp.getActiveSheet();
                    var maxRows = sh.getMaxRows();
                    var lastRow = sh.getLastRow();
                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                    }


                    Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)



                    EDIT



                    by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !



                    function deleteEmptyRows(){ 
                    var sh = SpreadsheetApp.getActiveSheet();
                    var data = sh.getDataRange().getValues();
                    var targetData = new Array();
                    for(n=0;n<data.length;++n){
                    if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
                    Logger.log(data[n].join().replace(/,/g,''))
                    }
                    sh.getDataRange().clear();
                    sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
                    }


                    Demo sheet in view only - make a copy to use






                    share|improve this answer























                    • Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                      – user2136368
                      Oct 20 '14 at 12:56












                    • Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                      – Serge insas
                      Oct 20 '14 at 14:55













                    up vote
                    9
                    down vote



                    accepted







                    up vote
                    9
                    down vote



                    accepted






                    This is how it works :



                    function removeEmptyRows(){
                    var sh = SpreadsheetApp.getActiveSheet();
                    var maxRows = sh.getMaxRows();
                    var lastRow = sh.getLastRow();
                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                    }


                    Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)



                    EDIT



                    by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !



                    function deleteEmptyRows(){ 
                    var sh = SpreadsheetApp.getActiveSheet();
                    var data = sh.getDataRange().getValues();
                    var targetData = new Array();
                    for(n=0;n<data.length;++n){
                    if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
                    Logger.log(data[n].join().replace(/,/g,''))
                    }
                    sh.getDataRange().clear();
                    sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
                    }


                    Demo sheet in view only - make a copy to use






                    share|improve this answer














                    This is how it works :



                    function removeEmptyRows(){
                    var sh = SpreadsheetApp.getActiveSheet();
                    var maxRows = sh.getMaxRows();
                    var lastRow = sh.getLastRow();
                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                    }


                    Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)



                    EDIT



                    by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !



                    function deleteEmptyRows(){ 
                    var sh = SpreadsheetApp.getActiveSheet();
                    var data = sh.getDataRange().getValues();
                    var targetData = new Array();
                    for(n=0;n<data.length;++n){
                    if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
                    Logger.log(data[n].join().replace(/,/g,''))
                    }
                    sh.getDataRange().clear();
                    sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
                    }


                    Demo sheet in view only - make a copy to use







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 15 '14 at 17:56

























                    answered Oct 15 '14 at 16:14









                    Serge insas

                    34.9k45388




                    34.9k45388












                    • Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                      – user2136368
                      Oct 20 '14 at 12:56












                    • Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                      – Serge insas
                      Oct 20 '14 at 14:55


















                    • Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                      – user2136368
                      Oct 20 '14 at 12:56












                    • Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                      – Serge insas
                      Oct 20 '14 at 14:55
















                    Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                    – user2136368
                    Oct 20 '14 at 12:56






                    Thanks Serge, I incorporated removeEmptyRows() into my own script and it worked like a charm. I also tried your demo sheet, which also worked perfectly. However when I copied the whole of your sample script to my sheet, it failed at sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);. I'm using the new version of GSheets, and I noticed your GSheet is the old version. Is 'getRange()' something that only works in old GSheets?
                    – user2136368
                    Oct 20 '14 at 12:56














                    Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                    – Serge insas
                    Oct 20 '14 at 14:55




                    Not at all...you probably get this error because targetDatais empty and its length=0 which is not a valid value.... Add a condition to execute getRange only if targetRange.length >0.
                    – Serge insas
                    Oct 20 '14 at 14:55












                    up vote
                    4
                    down vote













                    Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.




                    Place this if around the last line function removeEmptyRows() and it
                    will not throw an error:




                      if (maxRows-lastRow != 0){
                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                    }





                    share|improve this answer

























                      up vote
                      4
                      down vote













                      Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.




                      Place this if around the last line function removeEmptyRows() and it
                      will not throw an error:




                        if (maxRows-lastRow != 0){
                      sh.deleteRows(lastRow+1, maxRows-lastRow);
                      }





                      share|improve this answer























                        up vote
                        4
                        down vote










                        up vote
                        4
                        down vote









                        Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.




                        Place this if around the last line function removeEmptyRows() and it
                        will not throw an error:




                          if (maxRows-lastRow != 0){
                        sh.deleteRows(lastRow+1, maxRows-lastRow);
                        }





                        share|improve this answer












                        Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.




                        Place this if around the last line function removeEmptyRows() and it
                        will not throw an error:




                          if (maxRows-lastRow != 0){
                        sh.deleteRows(lastRow+1, maxRows-lastRow);
                        }






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 8 '15 at 22:19









                        apptailor

                        486




                        486






















                            up vote
                            3
                            down vote













                            Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.



                            //Remove All Empty Columns in the Entire Workbook
                            function removeEmptyColumns() {
                            var ss = SpreadsheetApp.getActive();
                            var allsheets = ss.getSheets();
                            for (var s in allsheets){
                            var sheet=allsheets[s]
                            var maxColumns = sheet.getMaxColumns();
                            var lastColumn = sheet.getLastColumn();
                            if (maxColumns-lastColumn != 0){
                            sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
                            }
                            }
                            }

                            //Remove All Empty Rows in the Entire Workbook
                            function removeEmptyRows() {
                            var ss = SpreadsheetApp.getActive();
                            var allsheets = ss.getSheets();
                            for (var s in allsheets){
                            var sheet=allsheets[s]
                            var maxRows = sheet.getMaxRows();
                            var lastRow = sheet.getLastRow();
                            if (maxRows-lastRow != 0){
                            sheet.deleteRows(lastRow+1, maxRows-lastRow);
                            }
                            }
                            }





                            share|improve this answer

























                              up vote
                              3
                              down vote













                              Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.



                              //Remove All Empty Columns in the Entire Workbook
                              function removeEmptyColumns() {
                              var ss = SpreadsheetApp.getActive();
                              var allsheets = ss.getSheets();
                              for (var s in allsheets){
                              var sheet=allsheets[s]
                              var maxColumns = sheet.getMaxColumns();
                              var lastColumn = sheet.getLastColumn();
                              if (maxColumns-lastColumn != 0){
                              sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
                              }
                              }
                              }

                              //Remove All Empty Rows in the Entire Workbook
                              function removeEmptyRows() {
                              var ss = SpreadsheetApp.getActive();
                              var allsheets = ss.getSheets();
                              for (var s in allsheets){
                              var sheet=allsheets[s]
                              var maxRows = sheet.getMaxRows();
                              var lastRow = sheet.getLastRow();
                              if (maxRows-lastRow != 0){
                              sheet.deleteRows(lastRow+1, maxRows-lastRow);
                              }
                              }
                              }





                              share|improve this answer























                                up vote
                                3
                                down vote










                                up vote
                                3
                                down vote









                                Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.



                                //Remove All Empty Columns in the Entire Workbook
                                function removeEmptyColumns() {
                                var ss = SpreadsheetApp.getActive();
                                var allsheets = ss.getSheets();
                                for (var s in allsheets){
                                var sheet=allsheets[s]
                                var maxColumns = sheet.getMaxColumns();
                                var lastColumn = sheet.getLastColumn();
                                if (maxColumns-lastColumn != 0){
                                sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
                                }
                                }
                                }

                                //Remove All Empty Rows in the Entire Workbook
                                function removeEmptyRows() {
                                var ss = SpreadsheetApp.getActive();
                                var allsheets = ss.getSheets();
                                for (var s in allsheets){
                                var sheet=allsheets[s]
                                var maxRows = sheet.getMaxRows();
                                var lastRow = sheet.getLastRow();
                                if (maxRows-lastRow != 0){
                                sheet.deleteRows(lastRow+1, maxRows-lastRow);
                                }
                                }
                                }





                                share|improve this answer












                                Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.



                                //Remove All Empty Columns in the Entire Workbook
                                function removeEmptyColumns() {
                                var ss = SpreadsheetApp.getActive();
                                var allsheets = ss.getSheets();
                                for (var s in allsheets){
                                var sheet=allsheets[s]
                                var maxColumns = sheet.getMaxColumns();
                                var lastColumn = sheet.getLastColumn();
                                if (maxColumns-lastColumn != 0){
                                sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
                                }
                                }
                                }

                                //Remove All Empty Rows in the Entire Workbook
                                function removeEmptyRows() {
                                var ss = SpreadsheetApp.getActive();
                                var allsheets = ss.getSheets();
                                for (var s in allsheets){
                                var sheet=allsheets[s]
                                var maxRows = sheet.getMaxRows();
                                var lastRow = sheet.getLastRow();
                                if (maxRows-lastRow != 0){
                                sheet.deleteRows(lastRow+1, maxRows-lastRow);
                                }
                                }
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 17 '15 at 19:11









                                Ryan Praskievicz

                                28117




                                28117






















                                    up vote
                                    1
                                    down vote













                                    Removing all empty lines (bottom-up)



                                    before



                                    enter image description here



                                    after



                                    enter image description here



                                    function isEmptyRow(row){
                                    for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                    var cell = row[columnIndex];
                                    if (cell){
                                    return false;
                                    }
                                    }
                                    return true;
                                    }

                                    function removeEmptyLines(sheet){
                                    var lastRowIndex = sheet.getLastRow();
                                    var lastColumnIndex = sheet.getLastColumn();
                                    var maxRowIndex = sheet.getMaxRows();
                                    var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                    var data = range.getValues();
                                    sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);

                                    for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
                                    var row = data[rowIndex];

                                    if (isEmptyRow(row)){
                                    sheet.deleteRow(rowIndex + 1);
                                    }
                                    }

                                    }


                                    function removeEmptyLinesFromAllSheets(){
                                    SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
                                    }




                                    Removing only empty lines from below and above the data



                                    before



                                    enter image description here



                                    after



                                    enter image description here



                                    function isEmptyRow(row){
                                    for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                    var cell = row[columnIndex];
                                    if (cell){
                                    return false;
                                    }
                                    }
                                    return true;
                                    }

                                    function getFirstNonBlankRowIndex(data){
                                    for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
                                    var row = data[rowIndex];

                                    if (!isEmptyRow(row)){
                                    return rowIndex;
                                    }
                                    }
                                    return 0;
                                    }

                                    function removePaddedEmptyLines(sheet){
                                    var lastRowIndex = sheet.getLastRow();
                                    var lastColumnIndex = sheet.getLastColumn();
                                    var maxRowIndex = sheet.getMaxRows();
                                    var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                    var data = range.getValues();
                                    var firstRowIndex = getFirstNonBlankRowIndex(data);
                                    sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
                                    sheet.deleteRows(1, firstRowIndex);
                                    }


                                    function removePaddedEmptyLinesFromAllSheets(){
                                    SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
                                    }





                                    share|improve this answer

























                                      up vote
                                      1
                                      down vote













                                      Removing all empty lines (bottom-up)



                                      before



                                      enter image description here



                                      after



                                      enter image description here



                                      function isEmptyRow(row){
                                      for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                      var cell = row[columnIndex];
                                      if (cell){
                                      return false;
                                      }
                                      }
                                      return true;
                                      }

                                      function removeEmptyLines(sheet){
                                      var lastRowIndex = sheet.getLastRow();
                                      var lastColumnIndex = sheet.getLastColumn();
                                      var maxRowIndex = sheet.getMaxRows();
                                      var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                      var data = range.getValues();
                                      sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);

                                      for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
                                      var row = data[rowIndex];

                                      if (isEmptyRow(row)){
                                      sheet.deleteRow(rowIndex + 1);
                                      }
                                      }

                                      }


                                      function removeEmptyLinesFromAllSheets(){
                                      SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
                                      }




                                      Removing only empty lines from below and above the data



                                      before



                                      enter image description here



                                      after



                                      enter image description here



                                      function isEmptyRow(row){
                                      for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                      var cell = row[columnIndex];
                                      if (cell){
                                      return false;
                                      }
                                      }
                                      return true;
                                      }

                                      function getFirstNonBlankRowIndex(data){
                                      for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
                                      var row = data[rowIndex];

                                      if (!isEmptyRow(row)){
                                      return rowIndex;
                                      }
                                      }
                                      return 0;
                                      }

                                      function removePaddedEmptyLines(sheet){
                                      var lastRowIndex = sheet.getLastRow();
                                      var lastColumnIndex = sheet.getLastColumn();
                                      var maxRowIndex = sheet.getMaxRows();
                                      var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                      var data = range.getValues();
                                      var firstRowIndex = getFirstNonBlankRowIndex(data);
                                      sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
                                      sheet.deleteRows(1, firstRowIndex);
                                      }


                                      function removePaddedEmptyLinesFromAllSheets(){
                                      SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
                                      }





                                      share|improve this answer























                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        Removing all empty lines (bottom-up)



                                        before



                                        enter image description here



                                        after



                                        enter image description here



                                        function isEmptyRow(row){
                                        for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                        var cell = row[columnIndex];
                                        if (cell){
                                        return false;
                                        }
                                        }
                                        return true;
                                        }

                                        function removeEmptyLines(sheet){
                                        var lastRowIndex = sheet.getLastRow();
                                        var lastColumnIndex = sheet.getLastColumn();
                                        var maxRowIndex = sheet.getMaxRows();
                                        var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                        var data = range.getValues();
                                        sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);

                                        for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
                                        var row = data[rowIndex];

                                        if (isEmptyRow(row)){
                                        sheet.deleteRow(rowIndex + 1);
                                        }
                                        }

                                        }


                                        function removeEmptyLinesFromAllSheets(){
                                        SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
                                        }




                                        Removing only empty lines from below and above the data



                                        before



                                        enter image description here



                                        after



                                        enter image description here



                                        function isEmptyRow(row){
                                        for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                        var cell = row[columnIndex];
                                        if (cell){
                                        return false;
                                        }
                                        }
                                        return true;
                                        }

                                        function getFirstNonBlankRowIndex(data){
                                        for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
                                        var row = data[rowIndex];

                                        if (!isEmptyRow(row)){
                                        return rowIndex;
                                        }
                                        }
                                        return 0;
                                        }

                                        function removePaddedEmptyLines(sheet){
                                        var lastRowIndex = sheet.getLastRow();
                                        var lastColumnIndex = sheet.getLastColumn();
                                        var maxRowIndex = sheet.getMaxRows();
                                        var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                        var data = range.getValues();
                                        var firstRowIndex = getFirstNonBlankRowIndex(data);
                                        sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
                                        sheet.deleteRows(1, firstRowIndex);
                                        }


                                        function removePaddedEmptyLinesFromAllSheets(){
                                        SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
                                        }





                                        share|improve this answer












                                        Removing all empty lines (bottom-up)



                                        before



                                        enter image description here



                                        after



                                        enter image description here



                                        function isEmptyRow(row){
                                        for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                        var cell = row[columnIndex];
                                        if (cell){
                                        return false;
                                        }
                                        }
                                        return true;
                                        }

                                        function removeEmptyLines(sheet){
                                        var lastRowIndex = sheet.getLastRow();
                                        var lastColumnIndex = sheet.getLastColumn();
                                        var maxRowIndex = sheet.getMaxRows();
                                        var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                        var data = range.getValues();
                                        sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);

                                        for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
                                        var row = data[rowIndex];

                                        if (isEmptyRow(row)){
                                        sheet.deleteRow(rowIndex + 1);
                                        }
                                        }

                                        }


                                        function removeEmptyLinesFromAllSheets(){
                                        SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
                                        }




                                        Removing only empty lines from below and above the data



                                        before



                                        enter image description here



                                        after



                                        enter image description here



                                        function isEmptyRow(row){
                                        for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
                                        var cell = row[columnIndex];
                                        if (cell){
                                        return false;
                                        }
                                        }
                                        return true;
                                        }

                                        function getFirstNonBlankRowIndex(data){
                                        for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
                                        var row = data[rowIndex];

                                        if (!isEmptyRow(row)){
                                        return rowIndex;
                                        }
                                        }
                                        return 0;
                                        }

                                        function removePaddedEmptyLines(sheet){
                                        var lastRowIndex = sheet.getLastRow();
                                        var lastColumnIndex = sheet.getLastColumn();
                                        var maxRowIndex = sheet.getMaxRows();
                                        var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
                                        var data = range.getValues();
                                        var firstRowIndex = getFirstNonBlankRowIndex(data);
                                        sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
                                        sheet.deleteRows(1, firstRowIndex);
                                        }


                                        function removePaddedEmptyLinesFromAllSheets(){
                                        SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 22 at 15:03









                                        Jossef Harush

                                        15.1k45671




                                        15.1k45671






















                                            up vote
                                            0
                                            down vote













                                            I have tried this piece of code and it works good, you may take a look and try it:



                                            function DeleteBlankRows(){
                                            var sh = SpreadsheetApp.getActiveSheet();
                                            var maxRows = sh.getMaxRows();
                                            var lastRow = sh.getLastRow();
                                            for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
                                            {
                                            if( sh.getRange('A'+Raw).getValue() == '')
                                            {
                                            sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
                                            }
                                            }





                                            share|improve this answer



























                                              up vote
                                              0
                                              down vote













                                              I have tried this piece of code and it works good, you may take a look and try it:



                                              function DeleteBlankRows(){
                                              var sh = SpreadsheetApp.getActiveSheet();
                                              var maxRows = sh.getMaxRows();
                                              var lastRow = sh.getLastRow();
                                              for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
                                              {
                                              if( sh.getRange('A'+Raw).getValue() == '')
                                              {
                                              sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
                                              }
                                              }





                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                I have tried this piece of code and it works good, you may take a look and try it:



                                                function DeleteBlankRows(){
                                                var sh = SpreadsheetApp.getActiveSheet();
                                                var maxRows = sh.getMaxRows();
                                                var lastRow = sh.getLastRow();
                                                for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
                                                {
                                                if( sh.getRange('A'+Raw).getValue() == '')
                                                {
                                                sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
                                                }
                                                }





                                                share|improve this answer














                                                I have tried this piece of code and it works good, you may take a look and try it:



                                                function DeleteBlankRows(){
                                                var sh = SpreadsheetApp.getActiveSheet();
                                                var maxRows = sh.getMaxRows();
                                                var lastRow = sh.getLastRow();
                                                for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
                                                {
                                                if( sh.getRange('A'+Raw).getValue() == '')
                                                {
                                                sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
                                                }
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Apr 3 '16 at 8:49









                                                Pang

                                                6,8281563101




                                                6,8281563101










                                                answered Apr 3 '16 at 8:23









                                                Khazragy

                                                11




                                                11






















                                                    up vote
                                                    0
                                                    down vote













                                                    This works perfectly for me.



                                                    function removeEmptyRows(){
                                                    var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
                                                    var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
                                                    var maxRows = sh.getMaxRows();
                                                    var lastRow = sh.getLastRow();
                                                    sh.deleteRows(lastRow+1, maxRows-lastRow);
                                                    }





                                                    share|improve this answer

























                                                      up vote
                                                      0
                                                      down vote













                                                      This works perfectly for me.



                                                      function removeEmptyRows(){
                                                      var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
                                                      var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
                                                      var maxRows = sh.getMaxRows();
                                                      var lastRow = sh.getLastRow();
                                                      sh.deleteRows(lastRow+1, maxRows-lastRow);
                                                      }





                                                      share|improve this answer























                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote









                                                        This works perfectly for me.



                                                        function removeEmptyRows(){
                                                        var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
                                                        var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
                                                        var maxRows = sh.getMaxRows();
                                                        var lastRow = sh.getLastRow();
                                                        sh.deleteRows(lastRow+1, maxRows-lastRow);
                                                        }





                                                        share|improve this answer












                                                        This works perfectly for me.



                                                        function removeEmptyRows(){
                                                        var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
                                                        var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
                                                        var maxRows = sh.getMaxRows();
                                                        var lastRow = sh.getLastRow();
                                                        sh.deleteRows(lastRow+1, maxRows-lastRow);
                                                        }






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 1 '17 at 17:52









                                                        Wizardeesh

                                                        63




                                                        63






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26386492%2fdeleting-all-empty-rows-in-a-google-spreadsheet%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            What visual should I use to simply compare current year value vs last year in Power BI desktop

                                                            Alexandru Averescu

                                                            Trompette piccolo