GSpread单元合并 [英] GSpread Cell Merging
问题描述
我正在尝试使用Gspread for Python合并Gsheets中的单元格,但是找不到任何文档.
I am trying to merge cells in Gsheets using Gspread for Python, however I can't find any documentation.
我已经使用gspread_formatting模块格式化单元格的文本和颜色,但是我发现与单元格的合并无关.
I have used the gspread_formatting module to format the text and colour of the cell, but I can't find anything to do with merging of the cells.
我没有写任何代码,因为我找不到任何示例.
I haven't wrote any code because I can't find any examples.
我希望能够根据范围合并单元格.
I want to be able to merge the cells based on a range.
有人可以帮助我吗?
干杯.
推荐答案
- 您想知道如何使用gspread合并Google Spreadsheet的单元格.
- 您已经能够使用gspread将值放入电子表格并获取到电子表格.
如果我的理解是正确的,那么这个答案如何?在此修改中,使用了batch_update()
方法.
If my understanding is correct, how about this answer? In this modification, batch_update()
method is used.
请设置spreadsheetId
,sheetName
和range
的网格范围.
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": [
{
"mergeCells": {
"mergeType": "MERGE_ALL",
"range": { # In this sample script, all cells of "A1:C3" of "Sheet1" are merged.
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": 3,
"startColumnIndex": 0,
"endColumnIndex": 3
}
}
}
]
}
res = ss.batch_update(body)
注意:
- 请将范围设置为gridrange.
- 在此示例脚本中,"Sheet1"的"A1:C3"的所有单元格都被合并.
- 将
MERGE_ALL
更改为MERGE_ROWS
时,"A1:C1","A2:C2"和"A3:C3"的行将合并. - 将
MERGE_ALL
更改为MERGE_COLUMNS
时,"A1:A3","B1:B3"和"C1:C3"列将合并. - Please set the range as the gridrange.
- In this sample script, all cells of "A1:C3" of "Sheet1" are merged.
- When
MERGE_ALL
is changed toMERGE_ROWS
, the rows of "A1:C1", "A2:C2" and "A3:C3" are merged. - When
MERGE_ALL
is changed toMERGE_COLUMNS
, the columns of "A1:A3", "B1:B3" and "C1:C3" are merged. - batch_update(body)
- Method: spreadsheets.batchUpdate
- MergeCellsRequest
- GridRange
Note:
如果这对您的情况没有帮助,我深表歉意.
If this was not useful for your situation, I apologize.
这篇关于GSpread单元合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!