How to resize a worksheet with the Google Client Library

可紊 提交于 2021-01-28 05:31:29

问题


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


回答1:


  • 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:

  • Method: spreadsheets.batchUpdate
  • UpdateSheetPropertiesRequest
  • AppendDimensionRequest
  • DeleteDimensionGroupRequest



回答2:


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.

  • UpdateSpreadsheetProperties request
  • GridProperties

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.



来源:https://stackoverflow.com/questions/61791361/how-to-resize-a-worksheet-with-the-google-client-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!