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.
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
If my understanding is correct, how about this answer? In this modification, batch_update() method is used.
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)
startIndex: 0 and endIndex: 1 mean the column "A" because of dimension: "COLUMNS".dimension is changed to ROWS, the height of rows can be adjusted.If this was not useful for your situation, I apologize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With