Search code examples
pythongoogle-sheetsgoogle-sheets-apigspread

Is there a way to unmerge Google Sheets cells with gspread or some other Python library?


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.


Solution

  • 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: