How to resize a worksheet with the Google Client Library

I am trying to resize a spreadsheet using the Google-provided library for API v4 (I have reasons not to use Gspread), but how can I do this?

I have tried a few methods and my current script is below; I am just looking for the request body I need to send. Also do I have to use batchUpdate or update?

Thank you in advance.

body = {
        "updateDimensionProperties": {
            "range": {
                "sheetId": utils.spreadsheet_handler.spreadsheet_id,
                "dimension": "COLUMNS",
                "startIndex": 0,
                "endIndex": 1
            }
        }
    }

    utils.spreadsheet_handler.sheet.values().batchUpdate(
        spreadsheetId=utils.spreadsheet_handler.spreadsheet_id,
        body=body).execute()  # Resizes the reservation spreadsheet

utils.spreadsheet_handler is a class that handles auth

Upvotes: 1

Views: 982

Answers (2)

Tanaike
Tanaike

Reputation: 201513

  • You want to increase and/or decrease the number of rows and columns of a worksheet in a Google Spreadsheet.
  • You want to achieve this using googleapis with Python.
  • You have aleady been able to get and put values for Google Spreadsheet using Sheets API.

For this, how about this answer? In this answer, UpdateSheetPropertiesRequest, AppendDimensionRequest and DeleteDimensionGroupRequest for the batchUpdate method in Sheets API are used.

Pattern 1:

In this pattern, the number of rows and columns of worksheet becomes 5 and 5.

Sample script:

service = build('sheets', 'v4', credentials=creds)
spreadsheetId = "###"  # Please set the Spreadsheet ID.
sheetId = "###"  # Please set the sheet ID.
body = {
    "requests": [
        {
            "updateSheetProperties": {
                "properties": {
                    "gridProperties": {
                        "rowCount": 5,
                        "columnCount": 5
                    },
                    "sheetId": sheetId
                },
                "fields": "gridProperties"
            }
        }
    ]
}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()

Pattern 2:

In this pattern, 10 rows and 10 columns are appended.

Sample script:

service = build('sheets', 'v4', credentials=creds)
spreadsheetId = "###"  # Please set the Spreadsheet ID.
sheetId = "###"  # Please set the sheet ID.
body = {
    "requests": [
        {
            "appendDimension": {
                "sheetId": sheetId,
                "dimension": "ROWS",
                "length": 10
            }
        },
        {
            "appendDimension": {
                "sheetId": sheetId,
                "dimension": "COLUMNS",
                "length": 10
            }
        }
    ]
}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()

Pattern 3:

In this pattern, 10 rows and 10 columns are reduced. At the sample script, rows 1 to 10 and columns A to J are deleted.

Sample script:

service = build('sheets', 'v4', credentials=creds)
spreadsheetId = "###"  # Please set the Spreadsheet ID.
sheetId = "###"  # Please set the sheet ID.
body = {
    "requests": [
        {
            "deleteDimension": {
                "range": {
                    "sheetId": sheetId,
                    "dimension": "ROWS",
                    "startIndex": 0,
                    "endIndex": 10
                }
            }
        },
        {
            "deleteDimension": {
                "range": {
                    "sheetId": sheetId,
                    "dimension": "COLUMNS",
                    "startIndex": 0,
                    "endIndex": 10
                }
            }
        }
    ]
}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()

Note:

  • Please select the sample script for your actual situation.

References:

Upvotes: 2

Talvalin
Talvalin

Reputation: 7889

From what I can see, the information you need is in the following links - from reading the GridProperties page it seems that you need to use rowCount and columnCount to resize your spreadsheet.

About whether batchUpdate is required or not, the UpdateSpreadsheetProperties request allows for more than one property can be specified, so it should be possible to use that instead. batchUpdate is only required if you intend to update multiple properties that require different request types.

Upvotes: 0

Related Questions