Search code examples
pythongoogle-sheets-apigspread

GSpread Column Sizing


I am trying to adjust the width of columns in a google sheet using GSpread, however I can't find any documentation on the subject all across the web. I have tried the actual project documents itself, and stack overflow.

I have looked through the documentation, and stack overflow, and nobody has asked a question like this before.

No code to show, as I haven't found any that may be relevant.

I am expecting the widen column 'A' in my sheet by around 100.

Thanks in advance for the help.

Cheers.


Solution

    • You want to know how to adjust the width of the column of Google Spreadsheet using gspread.
      • You want to set the column "A" to 100 pixels.
    • You have already been able to put and get values to the Spreadsheet using gspread.

    If my understanding is correct, how about this answer? In this modification, batch_update() method is used.

    Sample script:

    Please set spreadsheetId, sheetName and the gridrange of range.

    spreadsheetId = "###"
    sheetName = "Sheet1"
    
    client = gspread.authorize(credentials)
    ss = client.open_by_key(spreadsheetId)
    sheetId = ss.worksheet(sheetName)._properties['sheetId']
    body = {
        "requests": [
            {
                "updateDimensionProperties": {
                    "range": {
                        "sheetId": sheetId,
                        "dimension": "COLUMNS",
                        "startIndex": 0,
                        "endIndex": 1
                    },
                    "properties": {
                        "pixelSize": 100
                    },
                    "fields": "pixelSize"
                }
            }
        ]
    }
    res = ss.batch_update(body)
    

    Note:

    • Please set the range as the gridrange.
      • In this sample script, startIndex: 0 and endIndex: 1 mean the column "A" because of dimension: "COLUMNS".
    • In this sample script, the width of the column "A" is set to 100 pixels.
    • When dimension is changed to ROWS, the height of rows can be adjusted.

    References:

    If this was not useful for your situation, I apologize.