1

gspread has this method for merging cells but I don't find anything for unmerging. I see there's a way to do this in JavaScript, though, but my project is in Python so I would really like to stick to it.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
m_ocean
  • 327
  • 3
  • 11

1 Answers1

2

In that case, you can achieve your goal using batch_update method of gspread. The sample script is as follows.

Sample script:

client = gspread.authorize(credentials) # Here, please use your script.
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet name.

spreadsheet = client.open_by_key(spreadsheetId)
requests = [
    {
        "unmergeCells": {
            "range": {"sheetId": spreadsheet.worksheet(sheetName).id}
        }
    }
]
spreadsheet.batch_update({"requests": requests})
  • In this sample script, all merged cells of "Sheet1" in a Spreadsheet are unmerged.
  • When you want to unmerge the specific merged cells, please use the gridrange. Ref

Note:

  • This is a simple sample script. So please modify this for your actual situation.
  • This sample script supposes that you have already been able to get and put values for Google Spreadsheet using Sheets API. Please be careful about this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Oh wow, thanks! How have I not come across this?! I mean, I've seen `batch_update` but nothing came up when I searched for "batch_update unmerge"... – m_ocean Dec 15 '21 at 07:38
  • 1
    @m_ocean Thank you for replying. I'm glad your issue was resolved. Unfortunately, it seemed that the method of unmerging cells is not included in the methods of gspread. So I proposed to use `batch_update` method. About `batch_update`, in the case of this, the request body is required to be created by checking the official document. [Ref](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#unmergecellsrequest) Also, you can create and test the request body at ["Try this API"](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate). – Tanaike Dec 15 '21 at 08:14