Android (Java/Kotlin) Google Sheets API: Find/Get Number of Rows

April 7, 2019

Append Rows

If you just need to append new rows, you don’t need to get the number of rows.

Solution 1: Retrieve all cells of a column

val service = ...
val spreadsheetId = ...
val sheetName = ...

val result = service.spreadsheets().values().get(spreadsheetId, "'$sheetName'!A:A").execute()
Timber.d("rows=${result.getValues().size}")

NOTE: result.getValues() and result.values return different things.

NOTE: service is Sheets. Refer to Get Google Sheets service instance.

NOTE: Refer to Get Sheet Name.

Doesn’t Work: gridProperties.rowCount

gridProperties.rowCount return all rows including blank rows.

val spreadsheet = service.spreadsheets().get(spreadsheetId).execute()

for (sheet in spreadsheet.sheets) {
    Timber.d("sheetId=${sheet.properties.sheetId}, title=${sheet.properties.title}, rows=${sheet.properties.gridProperties.rowCount}")
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.