Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if sheet is empty?

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.

like image 721
IHaveGasProblems Avatar asked Jul 04 '16 13:07

IHaveGasProblems


People also ask

How do you check a sheet is empty or not in VBA?

If you wish to test whether a worksheet cell is empty in VBA, you can not use the worksheet function called ISBLANK. In VBA, you must use the ISEMPTY function. In this example, we will test whether cell A1 is empty. If cell A1 is empty, the message "Cell A1 is empty" will be displayed.

How do you check if Excel sheet is empty using C#?

ThisAddIn with Excel. Application . If it finds any worksheet with a value in a cell, it returns false. Otherwise it returns true (the workbook is empty).

Is Empty function in VBA?

VBA IsEmpty is a logical function that tests whether selected is empty or not. Since it is a logical function it will return the results in Boolean values i.e. either TRUE or FALSE. If the selected cell is empty it will return TRUE or else it will return FALSE.

How do you check if cell has value in Excel VBA?

The IsEmpty VBA function can be used to check if a cell is blank, just like the Excel ISBLANK worksheet function. But, there's more! Not only can IsEmpty check if a cell or range is empty, it can check if a variable is uninitialized.


2 Answers

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

I am using sheet.getLastRow() now

like image 188
IHaveGasProblems Avatar answered Oct 16 '22 20:10

IHaveGasProblems


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("") === "";
}
like image 27
Amit Agarwal Avatar answered Oct 16 '22 19:10

Amit Agarwal