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

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:

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