Android (Java/Kotlin) Google Sheets API: Add New Sheet

fun addNewSheet(service: Sheets, spreadsheetId: String, name: String): Int? {    val requests = listOf<Request>(        // https://developers.google.com/sheets/api/samples/sheet        Request().setAddSheet(AddSheetRequest().apply {            properties = SheetProperties().apply {                title = name            }        })    )    val body = BatchUpdateSpreadsheetRequest()        .setRequests(requests)    val result = service.spreadsheets().batchUpdate(spreadsheetId, body).execute()    var sheetId: Int? = null    val spreadsheet = result.updatedSpreadsheet    for (reply in result.replies) {       sheetId = reply.addSheet.properties.sheetId    }    return sheetId}

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

❤️ 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.