如何解决如何通过python正确更新Google表格图表数据范围? return语句弄乱了您的代码!参考:
我最近一直在努力通过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,left_title)
}
})
return
body = {
'requests': requests
}
service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id,body=body).execute()
def get_chart_spec(sheet_id,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,"startColumnIndex": 2,"endColumnIndex": 3,}
]
}
},"targetAxis": "LEFT_AXIS","type": "LINE","lineStyle": {
"width": 4
},"color": colors['blue']
},{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": sheet_id,"startColumnIndex": 3,"endColumnIndex": 4,"type": "COLUMN","color": colors['red']
},"startColumnIndex": 4,"endColumnIndex": 5,"color": colors['green']
},"startColumnIndex": 5,"endColumnIndex": 6,"color": colors['orange']
}
],"headerCount": 1,"stackedType": "STACKED"
}
}
return spec
更新图表规格时唯一发生变化的是last_row
变量,我刚刚对其进行调试,它是一个适当的值。这是一张图片(有些东西变白了,但与主题无关),底部应该包括所有星期数,但只包括最初生成图表的那几周:Incomplete chart
我在任何地方都找不到类似的东西,但是看到几乎没有关于自动Google工作表图表的讨论,对此我并不感到惊讶。 如果有帮助,图表也会被发布。
解决方法
return语句弄乱了您的代码!
找到图表(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
出现在这里。删除它以使您的图表成功更新。
参考:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。