I am using gspread
and gspread_formatting
to update my sheets in Google. Some of my codebase has been reworked to use batch_update
, because I found some code samples in another answer that I could use as reference. However, I can't seem to convert two other operations. Here is my code:
import gspread
from gspread_formatting import *
from oauth2client.service_account import ServiceAccountCredentials
def operate():
scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'creds.json',
scope
)
gc = gspread.authorize(credentials)
sh = gc.open('My spreadsheet')
ws = sh.sheet1
sheet_id = ws._properties['sheetId']
# Setting the column sizes
body = {
'requests': [
{
'updateDimensionProperties': {
'range': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': 0,
'endIndex': 10
},
'properties': {
'pixelSize': 150
},
'fields': 'pixelSize'
}
},
{
'updateDimensionProperties': {
'range': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': 4,
'endIndex': 6
},
'properties': {
'pixelSize': 250
},
'fields': 'pixelSize'
}
}
]
}
res = sh.batch_update(body)
# Request 1
ws.insert_row(['One', 'Two', 'Three'], 1)
# Request 2
format_cell_range(
ws, 'A1:Z7',
cellFormat(
wrapStrategy='WRAP',
verticalAlignment='MIDDLE',
backgroundColor=color(0.886, 0.945, 0.988),
textFormat=textFormat(
foregroundColor=color(0, 0.129, 0.443),
fontFamily='Roboto',
bold=True
)
)
)
So, what I want to do is somehow add request 1 and request 2 into the bulk_update
method. Is it possible to insert and a row and change formatting using bulk_update
? If yes, how can I do this? Thanks for any help.