Determining the last row in a single column

前端 未结 19 1258
野性不改
野性不改 2020-11-22 10:46

I have a sheet with data in cols A through H.

I need to determine the last row in column A that contains data (it\'s all conti

相关标签:
19条回答
  • 2020-11-22 11:06

    I am using getDataRange() followed by getNumRows(). The first function

    Returns a Range corresponding to the dimensions in which data is present

    and the second function

    Returns the number of rows in this range.

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var ws = ss.getActiveSheet();
    var lastRow = ws.getDataRange().getNumRows();
    

    P.S I hope this works for all cases.

    0 讨论(0)
  • 2020-11-22 11:07

    I realise this is quite an old thread but it's one of the first results when searching for this problem.

    There's a simple solution to this which afaik has always been available... This is also the "recommended" way of doing the same task in VBA.

    var lastCell = mySheet.getRange(mySheet.getLastRow(),1).getNextDataCell(
      SpreadsheetApp.Direction.UP
    );
    

    This will return the last full cell in the column you specify in getRange(row,column), remember to add 1 to this if you want to use the first empty row.

    0 讨论(0)
  • 2020-11-22 11:08

    personally I had a similar issue and went with something like this:

    function getLastRowinColumn (ws, column) {
      var page_lastrow = ws.getDataRange().getNumRows();
      var last_row_col = 0
      for (i=1; i<=page_lastrow;i++) {
        if (!(spread.getRange(column.concat("",i)).isBlank())) {last_row_col = i};
      }
      return last_row_col
    }
    

    It looks for the number of rows in the ws and loops through each cell in your column. When it finds a non-empty cell it updates the position of that cell in the last_row_col variable. It has the advantage of allowing you to have non-contiguous columns and still know the last row (assuming you are going through the whole column).

    0 讨论(0)
  • 2020-11-22 11:10

    Here's an alternative way of solving this. It uses a while loop but takes into consideration empty gaps between rows.

    function getLastRow (column) {
      var iLastRow = ss.getActiveSheet().getMaxRows();
      var aValues = ss.getActiveSheet().getRange(column + ":" + column).getValues();
      var row = "";
      while(row == ""){
        row = aValues[iLastRow-1];
        iLastRow--;
      }
      return iLastRow;
    }
    
    0 讨论(0)
  • 2020-11-22 11:12

    To get the number of columns or last column's index:

    var numColumns = sheet.getLastColumn()
    

    To get the no of rows or last row's index:

    var numRows = sheet.getLastRow()
    

    where

    var sheet = SpreadsheetApp.getActiveSheet()
    
    0 讨论(0)
  • 2020-11-22 11:17

    After a while trying to build a function to get an integer with the last row in a single column, this worked fine:

        function lastRow() {
        var spreadsheet = SpreadsheetApp.getActiveSheet();
        spreadsheet.getRange('B1').activate();
        var columnB = spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
        var numRows = columnB.getLastRow();
        var nextRow = numRows + 1; 
        }
    
    0 讨论(0)
提交回复
热议问题