GSpread Column Sizing
Asked Answered
S

2

8

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.

Shortfall answered 24/7, 2019 at 7:59 Comment(0)
B
9
  • 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.

Billbillabong answered 24/7, 2019 at 8:24 Comment(2)
Also worked perfectly! Really appreciate the help with this.Shortfall
@Dylan Logan Thank you for replying. I'm glad your issue was resolved.Billbillabong
M
12

If you are using gspread I recommend to use gspread-formatting for this task.

Example from the documentation:

set_row_height(worksheet, 1, 42)
set_row_height(worksheet, '1:100', 42)
set_row_heights(worksheet, [ ('1:100', 42), ('101:', 22) ])
set_column_width(worksheet, 'A', 190)
set_column_width(worksheet, 'A:D', 100)
set_column_widths(worksheet, [ ('A', 200), ('B:', 100) ])

Check it here: https://pypi.org/project/gspread-formatting/#setting-row-heights-and-column-widths

Materialism answered 19/12, 2020 at 15:54 Comment(1)
Works great for my needs, thanks!Tenno
B
9
  • 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.

Billbillabong answered 24/7, 2019 at 8:24 Comment(2)
Also worked perfectly! Really appreciate the help with this.Shortfall
@Dylan Logan Thank you for replying. I'm glad your issue was resolved.Billbillabong

© 2022 - 2024 — McMap. All rights reserved.