I believe your goal as follows.
- You want to retrieve the values from the merged cells using gspread of python.
In your sample Spreadsheet, for example, the cells "A1:L12" are merged. Under this condition, when the values are retrieved from the merged cells of "A1:L12" using Sheets API, just to confirm, the value "row_merged" has been put to cell D2 originally. Value "col_merged" is in D6 initially. Since it's merged, the expected result should be found on all merged cells
is retrieved only cell "A1". So in order to retrieve the same values from all cells of "A1:L12", it is required to put the values to the cells "A2:L12". Unfortunately, it seems that there are no methods for directly achieving this situation in Sheets API. So, in this answer, I would like to propose this using a script.
By the way, about result = ws.acell('A2')
in your script, this cell is not merged and it's empty. So in this case, I think that the empty value is correct. I thought that you might have wanted to check the cell "B1". In this answer, this is also considered.
Sample script:
spreadsheet_id = '###' # Please set the Spreadsheet ID.
sheet_name = 'Sheet1' # Please set the sheet name.
client = gspread.authorize(credentials)
access_token = client.auth.token
url = "https://sheets.googleapis.com/v4/spreadsheets/" +
spreadsheet_id + "?fields=sheets&ranges=" + sheet_name
res = requests.get(url, headers={"Authorization": "Bearer " + access_token})
obj = res.json()
# print(obj['sheets'][0]['merges'])
sheet = client.open_by_key(spreadsheet_id)
ws = sheet.worksheet(sheet_name)
# 1. All values are retrieved.
values = ws.get_all_values()
# 2. Put the values to the merged cells.
if 'merges' in obj['sheets'][0].keys():
for e in obj['sheets'][0]['merges']:
value = values[e['startRowIndex']][e['startColumnIndex']]
rows = len(values)
if rows < e['endRowIndex']:
for i in range(0, e['endRowIndex'] - rows):
values.append([''])
for r in range(e['startRowIndex'], e['endRowIndex']):
cols = len(values[r])
if cols < e['endColumnIndex']:
values[r].extend([''] * (e['endColumnIndex'] - cols))
for c in range(e['startColumnIndex'], e['endColumnIndex']):
values[r][c] = value
# For A1
print('A1: '+values[0][0])
# For B1
# output: just to confirm, the value "row_merged" has been put to cell D2 originally. Value "col_merged" is in D6 initially. Since it's merged, the expected result should be found on all merged cells
print('B1: '+values[0][1])
# For C2
# output: simple cell
print('C2: '+values[1][2])
# For D2
# output: row_merged
print('D2: '+values[1][3])
# For E2
# output: row_merged
print('E2: '+values[1][4])
# For D6
# output: col_merged
print('D6: '+values[5][3])
# For D7
# output: col_merged
print('D7: '+values[6][3])
Note:
- In this sample script, the method of "spreadsheets.get" in Sheets API is used with
requests
using the access token retrieved from client = gspread.authorize(credentials)
of gspread.
- In this sample script, the values are processed in the list. So when you retrieved the values from the merged cells, please retrieve them from the list
values
.
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…