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.
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.
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).
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.
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.
I just stumbled across the answer on the app script documentation.
I am using sheet.getLastRow() now
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("") === "";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With