Google Sheets API cell merging using Python - not working

I’m trying to combine cells in my Google Sheets document using the Python API but it’s not working. I can create new sheets and add data to them just fine, but when I try to merge cells for my header section, nothing happens.

header_merge_config = [
    {'mergeCells': {
        'mergeType': 'MERGE_COLUMNS',
        'range': {
            'endColumnIndex': 4,
            'endRowIndex': 1,
            'sheetId': '998877665',
            'startColumnIndex': 0,
            'startRowIndex': 0
        }
    }},
    {'mergeCells': {
        'mergeType': 'MERGE_COLUMNS',
         'range': {
             'endColumnIndex': 7,
             'endRowIndex': 1,
             'sheetId': '998877665',
             'startColumnIndex': 4,
             'startRowIndex': 0
         }
    }}
]

sheets_service.spreadsheets().batchUpdate(
    spreadsheetId=my_spreadsheet_id,
    body={'requests': header_merge_config}
).execute()

The code runs without any errors and I get an empty response back, but the cells don’t actually merge in the spreadsheet. What am I doing wrong here?

make sure your cells arent empty b4 merging - empty ones dont merge right half the time. try MERGE_ALL instead of MERGE_COLUMNS, especially for headers. when it fails silently like that, its usually cause your range is wrong.

I’ve hit this exact issue before. You’re passing the sheetId as a string (998877665) but the API needs it as an integer without quotes. That’s usually why it fails silently. Change your sheetId to 998877665 (drop the quotes) and that should fix it. Also double-check your API permissions - you need ‘https://www.googleapis.com/auth/spreadsheets’ for write access since merging cells requires more than read-only permissions.

Had the same issue a few months ago. Besides the sheetId problem mentioned above, make sure you’re working with the right sheet. When you create sheets programmatically, the actual sheetId often doesn’t match what you think it should be. Use sheets().get() first to grab the real sheetId from your spreadsheet metadata before trying to merge. Also heads up - if there’s data in the cells you’re merging, the API can get weird. It usually keeps the top-left cell’s content and dumps the rest, but sometimes this breaks things silently depending on your data format.