GSpread Cell Merging

北城余情 提交于 2020-01-04 07:54:48

问题


I am trying to merge cells in Gsheets using Gspread for Python, however I can't find any documentation.

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.

Can anyone please help me?

Cheers.


回答1:


  • You want to know how to merge cells of Google Spreadsheet using gspread.
  • 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": [
        {
            "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)

Note:

  • 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 to MERGE_ROWS, the rows of "A1:C1", "A2:C2" and "A3:C3" are merged.
  • When MERGE_ALL is changed to MERGE_COLUMNS, the columns of "A1:A3", "B1:B3" and "C1:C3" are merged.

References:

  • batch_update(body)
  • Method: spreadsheets.batchUpdate
  • MergeCellsRequest
  • GridRange

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



来源:https://stackoverflow.com/questions/57166738/gspread-cell-merging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!