Android (Java/Kotlin) Google Sheets API: Update Column Width (or Auto Resize)

April 7, 2019
val service = ...
val spreadsheetId = ...
val sheetId = 0

val requests = listOf<Request>(
    // set Col A to AutoResize
    Request().setAutoResizeDimensions(
        AutoResizeDimensionsRequest().apply {
            dimensions = DimensionRange().apply {
                this.sheetId = sheetId
                dimension = "COLUMNS"
                startIndex = 0 // A
                endIndex = 1
            }
        }
    ),
    // set Col B to 350
    Request().setUpdateDimensionProperties(
        UpdateDimensionPropertiesRequest().apply {
            range = DimensionRange().apply {
                this.sheetId = sheetId
                dimension = "COLUMNS"
                startIndex = 1 // B
                endIndex = 2
            }
            properties = DimensionProperties().apply {
                pixelSize = 300
            }
            fields = "pixelSize"
        }
    )
)

val body = BatchUpdateSpreadsheetRequest().setRequests(requests)

service.spreadsheets().batchUpdate(spreadsheetId, body).execute()

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

NOTE: Refer to Get Sheet Id.

References:

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