简体   繁体   中英

How can I get the end row of my sheet (not the last row with data) in Google Sheets Scripts?

I would like to know how can I get the end row of my sheet (not the last row with data). Is there a simple code to get that?

Solution:

You can use getMaxRows() to get the end row number.

Sample:

在此处输入图像描述

在此处输入图像描述

Reference:

getMaxRows()

A lot of people want to do this because they don't want to have to scroll down past all of their data so the typical approach is something like this:

function getFirstRowAfterAllData0() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  sh.getRange(sh.getLastRow()+1, 1).activate();
}

But if sh.getLastRow() equals sh.getMaxRows() then this won't work because the script can't go to a line that doesn't exist and so you'll find that this approach will work in that situation also. It adds another row to the bottom when there aren't any available rows after your data.

function getFirstRowAfterAllData() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  if (sh.getLastRow() < sh.getMaxRows()) {
    sh.getRange(sh.getLastRow()+1, 1).activate();
  } else {
    sh.insertRowAfter(sh.getLastRow());
    sh.getRange(sh.getLastRow()+1, 1).activate();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM