我最近一直在努力通过python更新Google表格图表数据范围。应该有一个json负载updateChartSpec可以做到这一点,它看起来和addChart完全一样,对我来说也很好,只是chartId
需要指定其他内容。
这是我的代码:
def add_sheet_chart(service, which_sheet, last_row):
response = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False).execute()
sheet_array = response.get('sheets')
# Get existing chart id here, if there are not charts, it's left as -1.
sheet_id = 0
chart_id = -1
for sheet in sheet_array:
if sheet.get('properties').get('title') == which_sheet:
chart_array = sheet.get('charts')
if chart_array:
if len(chart_array) != 0:
chart_id = chart_array[0].get('chartId')
sheet_id = sheet.get('properties').get('sheetId')
break
title = "..."
bottom_title = "..."
left_title = "..."
requests = []
if chart_id == -1: # <- insert new chart here (works fine)
requests.append({
"addChart": {
"chart": {
"spec": get_chart_spec(sheet_id, last_row, title, bottom_title, left_title),
"position": {
"overlayPosition": {
"anchorCell": {
"sheetId": sheet_id,
"rowIndex": 4,
"columnIndex": 8
},
"widthPixels": 900,
"heightPixels": 600
}
}
}
}
})
else: # <- update existing chart here (doesn't work)
requests.append({
"updateChartSpec": {
"chartId": chart_id,
"spec": get_chart_spec(sheet_id, last_row, title, bottom_title, left_title)
}
})
return
body = {
'requests': requests
}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
def get_chart_spec(sheet_id, last_row, title, bottom_title, left_title):
spec = {
"title": title,
"basicChart": {
"chartType": "COMBO",
"legendPosition": "TOP_LEGEND",
"axis": [
{
"position": "BOTTOM_AXIS",
"title": bottom_title
},
{
"position": "LEFT_AXIS",
"title": left_title
}
],
"domains": [
{
"domain": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 1,
"endColumnIndex": 2
}
]
}
}
}
],
"series": [
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 2,
"endColumnIndex": 3,
}
]
}
},
"targetAxis": "LEFT_AXIS",
"type": "LINE",
"lineStyle": {
"width": 4
},
"color": colors['blue']
},
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 3,
"endColumnIndex": 4,
}
]
}
},
"targetAxis": "LEFT_AXIS",
"type": "COLUMN",
"color": colors['red']
},
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 4,
"endColumnIndex": 5,
}
]
}
},
"targetAxis": "LEFT_AXIS",
"type": "COLUMN",
"color": colors['green']
},
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,
"startRowIndex": 0,
"endRowIndex": last_row,
"startColumnIndex": 5,
"endColumnIndex": 6,
}
]
}
},
"targetAxis": "LEFT_AXIS",
"type": "COLUMN",
"color": colors['orange']
}
],
"headerCount": 1,
"stackedType": "STACKED"
}
}
return spec
更新图表规格时唯一发生变化的是last_row
变量,我刚刚对其进行调试,它是一个适当的值。这是一张图片(有些东西变白了,但与主题无关),底部应该包括所有星期数,但只包括最初生成图表的那几周:不完整的图表
我在任何地方都找不到类似的东西,但是看到几乎没有关于自动Google工作表图表的讨论,我并不感到惊讶。如果有帮助,图表也会被发布。
找到图表(chart_id != -1
)后,代码进入一个else
块。在该块的末尾,追加了图表更新请求后,您添加了一条return
语句,该语句结束了函数的执行,因此该代码永远不会到达batchUpdate
下面的调用。因此,该图表不会更新:
else:
requests.append({
"updateChartSpec": {
"chartId": chart_id,
"spec": get_chart_spec(sheet_id, last_row, title, bottom_title, left_title)
}
})
return # REMOVE THIS!
没有理由return
要在那里。删除它以使您的图表成功更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句