Search code examples
pythongoogle-sheets-apigspread

Hide rows in google sheets using python


I would like to hide rows in a document using python, to authomatize a project, but I tried with spread and there is no option.

So I would like to know if there's any possibility to do it using python


Solution

  • I believe your goal and your current situation as follows.

    • You want to hide the rows of a sheet in Google Spreadsheet.
    • You want to achieve this using Sheets API with gspread of python.
    • You have already been able to use Sheets API and gspread.

    In this case, I think that your goal can be achieved using UpdateDimensionPropertiesRequest with the method of batch_update. When this is reflected to the script, it becomes as follows.

    Sample script:

    client = gspread.authorize(credentials) # Please use your authorization script for this.
    spreadsheetId = "###" # Please set your Spreadsheet ID.
    sheetName = "Sheet1" # Please set the sheet name.
    
    spreadsheet = client.open_by_key(spreadsheetId)
    sheetId = spreadsheet.worksheet(sheetName).id
    body = {
        "requests": [
            {
                "updateDimensionProperties": {
                    "properties": {
                        "hiddenByUser": True
                    },
                    "range": {
                        "sheetId": sheetId,
                        "dimension": "ROWS",
                        "startIndex": 1,
                        "endIndex": 10
                    },
                    "fields": "hiddenByUser"
                }
            }
        ]
    }
    spreadsheet.batch_update(body)
    
    • When above script is run, the rows 2 to 10 of "Sheet1" in Google Spreadsheet spreadsheetId are hidden.

    Note:

    • This sample script supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API with gspread. Please be careful this.

    References: