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

April 7, 2019
val service = ...
val spreadsheetId = ...
val sheetName = 'Sheet1'

val rows = listOf(
    listOf("Col 01", "Col 02", "Col 03")
)
val body = ValueRange().setValues(rows)
val range = "'$sheetName'!A1"
val result = service.spreadsheets().values().append(spreadsheetId, range, body)
    .setValueInputOption("RAW")
    .setInsertDataOption("INSERT_ROWS")
    .execute()
Timber.d("newRows=${result.updates.updatedRows}")

range

  • If you have a header row, you use A2. If will find an empty row from A2 and below and perform insert.
  • If you need to start at second column from row 2 onwards, you can use B2.

NOTE: Refer to Sheets Range A1 Notation and Appending Values

NOTE: Refer to Get Sheet Name

setInsertDataOption("INSERT_ROWS")

Additionally, you can choose if you want to overwrite existing data after a table or insert new rows for the new data. By default, the input overwrites data after the table. To write the new data into new rows, specify insertDataOption=INSERT_ROWS.

NOTE: service is Sheets. Refer to Setup Google Sheets API for Android (Java/Kotlin) to get service/Sheets instance.

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.