Selecting the last value of a column

前端 未结 23 1792
再見小時候
再見小時候 2020-11-28 19:15

I have a spreadsheet with some values in column G. Some cells are empty in between, and I need to get the last value from that column into another cell.

Something li

相关标签:
23条回答
  • 2020-11-28 19:34

    It looks like Google Apps Script now supports ranges as function parameters. This solution accepts a range:

    // Returns row number with the last non-blank value in a column, or the first row
    //   number if all are blank.
    // Example: =rowWithLastValue(a2:a, 2)
    // Arguments
    //   range: Spreadsheet range.
    //   firstRow: Row number of first row. It would be nice to pull this out of
    //     the range parameter, but the information is not available.
    function rowWithLastValue(range, firstRow) {
      // range is passed as an array of values from the indicated spreadsheet cells.
      for (var i = range.length - 1;  i >= 0;  -- i) {
        if (range[i] != "")  return i + firstRow;
      }
      return firstRow;
    }
    

    Also see discussion in Google Apps Script help forum: How do I force formulas to recalculate?

    0 讨论(0)
  • 2020-11-28 19:34
    function getDashboardSheet(spreadsheet) {
      var sheetName = 'Name';
      return spreadsheet.getSheetByName(sheetName);
    }
          var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);  
          var dashboardSheet = getDashboardSheet(spreadsheet);
          Logger.log('see:'+dashboardSheet.getLastRow());
    
    0 讨论(0)
  • 2020-11-28 19:36

    I looked at the previous answers and they seem like they're working too hard. Maybe scripting support has simply improved. I think the function is expressed like this:

    function lastValue(myRange) {
        lastRow = myRange.length;
        for (; myRange[lastRow - 1] == "" && lastRow > 0; lastRow--)
        { /*nothing to do*/ }
        return myRange[lastRow - 1];
    }
    

    In my spreadsheet I then use:

    = lastValue(E17:E999)
    

    In the function, I get an array of values with one per referenced cell and this just iterates from the end of the array backwards until it finds a non-empty value or runs out of elements. Sheet references should be interpreted before the data is passed to the function. Not fancy enough to handle multi-dimensions, either. The question did ask for the last cell in a single column, so it seems to fit. It will probably die on if you run out of data, too.

    Your mileage may vary, but this works for me.

    0 讨论(0)
  • 2020-11-28 19:36

    I found another way may be it will help you

    =INDEX( SORT( A5:D ; 1 ; FALSE) ; 1 ) -will return last row

    More info from anab here: https://groups.google.com/forum/?fromgroups=#!topic/How-to-Documents/if0_fGVINmI

    0 讨论(0)
  • 2020-11-28 19:36

    to get the last value from a column you can also use MAX function with IF function

    =ARRAYFORMULA(INDIRECT("G"&MAX(IF(G:G<>"", ROW(G:G), )), 4)))
    
    0 讨论(0)
  • 2020-11-28 19:36

    I have gone through way too many of these implementations of last-row for a specific column. Many solutions work but are slow for large or multiple datasets. One of my use cases requires me to check the last row in specific columns across multiple spreadsheets. What I have found is that taking the whole column as a range and then iterating through it is too slow, and adding a few of these together makes the script sluggish.

    My "hack" has been this formula:

    =ROW(index(sheet!A2:A,max(row(sheet!A2:A)*(sheet!A2:A<>""))))-1
    
    • Example: Add this to Cell A1, to find the last row in column A. Can be added anywhere, just make sure to manage the "-1" at the end depending on which row the formula is placed. You can also place this is another col, rather than the one you're trying to count, and you don't need to manage the -1. You could also count FROM a starting Row, like "C16:C" - will count values C16 onwards

    • This formula is reliably giving me the last row, including blanks in the middle of the dataset

    • To use this value in my GS code, I am simply reading the cell value from A1. I understand that Google is clear that spreadsheet functions like read/write are heavy (time-consuming), but this is much faster than column count last-row methods in my experience (for large datasets)

    • To make this efficient, I am getting the last row in a col once, then saving it as a global variable and incrementing in my code to track which rows I should be updating. Reading the cell every-time your loop needs to make an update will be too inefficient. Read once, iterate the value, and the A1 cell formula (above) is "storing" the updated value for the next time your function runs

    Please let me know if this was helpful to you! If I encounter any issues I will comment on this answer.

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