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.
Advertisement
Answer
- 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
.
JavaScript
x
26
26
1
spreadsheetId = "###"
2
sheetName = "Sheet1"
3
4
client = gspread.authorize(credentials)
5
ss = client.open_by_key(spreadsheetId)
6
sheetId = ss.worksheet(sheetName)._properties['sheetId']
7
body = {
8
"requests": [
9
{
10
"updateDimensionProperties": {
11
"range": {
12
"sheetId": sheetId,
13
"dimension": "COLUMNS",
14
"startIndex": 0,
15
"endIndex": 1
16
},
17
"properties": {
18
"pixelSize": 100
19
},
20
"fields": "pixelSize"
21
}
22
}
23
]
24
}
25
res = ss.batch_update(body)
26
Note:
- Please set the range as the gridrange.
- In this sample script,
startIndex: 0
andendIndex: 1
mean the column “A” because ofdimension: "COLUMNS"
.
- In this sample script,
- In this sample script, the width of the column “A” is set to 100 pixels.
- When
dimension
is changed toROWS
, the height of rows can be adjusted.
References:
If this was not useful for your situation, I apologize.