【发布时间】:2020-01-20 17:58:09
【问题描述】:
我正在使用 gspread 和 gspread_formatting 在 Google 中更新我的工作表。我的一些代码库已被重新设计为使用batch_update,因为我在另一个答案中找到了一些代码示例,可以用作参考。但是,我似乎无法转换其他两个操作。这是我的代码:
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
)
)
)
所以,我想做的是以某种方式将请求 1 和请求 2 添加到 bulk_update 方法中。是否可以使用bulk_update 插入一行并更改格式?如果是,我该怎么做?感谢您的帮助。
【问题讨论】:
标签: python google-sheets gspread