I'm working with Google sheet API and I have defined the following functions:
def service():
secret_id_gsuite_token = 'my_token' # AWS Secret with token
gsheet = GSheets(secret_id_gsuite_token)
return gsheet
To update a sheet with information present in a pandas dataframe:
def df_to_googlesheet(gsheet, df, gsheetId, sheet_name):
output = gsheet.spreadsheets.values().update(
spreadsheetId = gsheetId,
valueInputOption = 'RAW',
range = sheet_name + '!A1',
body = dict(
majorDimension = 'ROWS',
values = df.T.reset_index().T.values.tolist())
).execute()
To make a request to merge cells to form a "block":
def merge_cells_request(sheeId, start_row, end_row, start_col, end_col):
request = [{"mergeCells": { "range": { "sheetId": sheeId,
"startRowIndex": start_row,
"endRowIndex": end_row,
"startColumnIndex": start_col,
"endColumnIndex": end_col},
"mergeType": "MERGE_ALL"}}]
return request
I also have a similar function to make requests to color cells backgrounds. I put all my request in a requests list and apply them running:
gsheet.batch_update(gsheetId, requests)
This batch_update function uses the batchUpdate() method of google sheets api.
Finally this is the function to clear the sheet:
def clear_sheet(gsheet, spreadsheetId, sheeId):
clear_request = [{"updateCells": {"range": {"sheetId": sheeId},
"fields": "*"}}]
gsheet.batch_update(spreadsheetId, clear_request)
The problem is that this last function does not clear the merged cells, I want to unmerge all merged cells in order to have the sheet totally clean again, but none of the options I found online seems to fit my code...