5

I am getting the data range of a sheet and using Range.getNumRows() to get the number of rows in a Google Spreadsheet using Google Apps Script.

But when the sheet happens to be completely empty, Range.getNumRows() still returns 1 instead of 0. I am guessing this is because a range has to have at least 1 cell.

Is there another (simple) way to get the number of rows in a sheet without having this Problem?

I know I could loop through all cells in the sheet to check it is completely empty, but this doesn't seem very efficient.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 1
    I figured this _had_ to be a duplicate... surely somebody had asked it before! But, no... Good question! – Mogsdad Jul 04 '16 at 14:38

3 Answers3

7

I just stumbled across the answer on the app script documentation.

I am using sheet.getLastRow() now

4

Another option would be to get the sheet range and concatenate it to check if any data is found.

function isSheetEmpty(sheet) {
  return sheet.getDataRange().getValues().join("") === "";
}
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
0

range.isBlank() work's well too.

Apps Script Range isBlank

Haskel
  • 1
  • 1