Android (Java/Kotlin) Google Sheets API: Get Sheet Name or Id

val service = ...val spreadsheetId = ...val findSheetName = 'Sheet1'val spreadsheet = service.spreadsheets().get(spreadsheetId).execute()/*for (sheet in spreadsheet.sheets) {    Timber.d("sheetId=${sheet.properties.sheetId}, title=${sheet.properties.title}")} */val sheet = if (spreadsheet.sheets.size == 1) {    spreadsheet.sheets[0]}else {    var defaultSheet: Sheet? = null    var findSheet: Sheet? = null    for (sheet in spreadsheet.sheets) {        if (sheet.properties.sheetId == 0) {            defaultSheet = sheet        }        else if (sheet.properties.title == findSheetName) {            findSheet = sheet        }    }    findSheet ?: defaultSheet!!}val sheetName = sheet.properties.titleval sheetId = sheet.properties.sheetId

NOTE: The default/first sheet created (e.g. 'Sheet1') have an Id of 0.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.