As per discussion on Python dataframe to Google Sheet: Freeze rows and sketch vertical lines around a sets of columns , I have been able to use the code below (thank you @Tanaike!) and sketch vertical and horizontal borders around dataframe df_final
that I publish into Google Sheet at cell B7 using python.
Please chime in about 2 issues I have
As can be seen in the attached photo, the lines don't extend all the way to the column AE, but rather stop at Z (see red lines in photo). I tried to edit the code and make
endColumnIndex
higher than 26 but it doesn't help.How do I make the contents of the sheet to be center aligned? I already auto fit them as shown in code below.
def Publish_Gsheet(df_final, service, spreadsheet_id, sheet_id):
### Clear the Google Sheet:
service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheet_id, body={"requests": [{
"repeatCell": {
"range": {
"sheetId": sheet_id
},
"fields": "userEnteredValue,userEnteredFormat.borders"
}
}
]}).execute()
##########################
### Write df_final to Google Sheet:
cell_range_insert = 'B7'
# values = df_final.to_json() # It seems that this is not used.
# body = {'values': values} # It seems that this is not used.
v = df_final.T.reset_index().T.values.tolist()
response_date = service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id,
valueInputOption='RAW',
range=cell_range_insert,
body=dict(
majorDimension='ROWS',
values=v
)
).execute()
###################################
### Autofit Columns and Rows width:
request_body = {
'requests': [
{
'autoResizeDimensions': {
'dimensions': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': 0,
'endIndex': 26
}
}
},
{
'autoResizeDimensions': {
'dimensions': {
'sheetId': sheet_id,
'dimension': 'ROWS',
'startIndex': 0,
'endIndex': 1000
}
}
}
]
}
##### Sketching horizontal rows after each Week:
temp = -1
n = []
for index, row in df_final.iloc[:, 7:8].iterrows():
s = ''.join(row.astype(str).tolist())
if temp != s:
n.append(index)
temp = s
offset = 7
requests = [{
"repeatCell": {
"cell": {"userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}},
"range": {
"sheetId": sheet_id,
"startRowIndex": e + offset,
"endRowIndex": e + 1 + offset,
"startColumnIndex": 1,
"endColumnIndex": 26
},
"fields": "userEnteredFormat.borders"
}
} for e in n]
##### Sketching vertical lines in between predefined columns:
end = len(v) + offset - 1
add_requests1 = [{
"repeatCell": {
"cell": {"userEnteredFormat": {"borders": {"left": {"style": "SOLID_THICK"}}}},
"range": {
"sheetId": sheet_id,
"startRowIndex": 6,
"endRowIndex": end,
"startColumnIndex": a,
"endColumnIndex": b
},
"fields": "userEnteredFormat.borders.left"
}
} for [a, b] in [[1, 2], [2, 3], [6, 7], [10, 11], [15, 16], [17, 18], [21,22], [22,23], [23,24], [27, 28]]]
add_requests2 = [{
"repeatCell": {
"cell": {"userEnteredFormat": {"borders": {"top": {"style": "SOLID_THICK"}}}},
"range": {
"sheetId": sheet_id,
"startRowIndex": a,
"endRowIndex": b,
"startColumnIndex": 1,
"endColumnIndex": 26
},
"fields": "userEnteredFormat.borders.top"
}
} for [a, b] in [[6, 7], [end, end + 1]]]
add_requests3 = [{
"updateSheetProperties": {
"properties": {"gridProperties": {"frozenRowCount": offset}, "sheetId": sheet_id},
"fields": "gridProperties.frozenRowCount"
}
}]
requests.extend(add_requests1)
requests.extend(add_requests2)
requests.extend(add_requests3)
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests}).execute()