如何通过python正确更新Google工作表数据范围?

如何通过python正确更新Google工作表数据范围?,python,charts,google-sheets-api,Python,Charts,Google Sheets Api,我最近一直在努力通过python更新google工作表数据范围。有一个json负载应该可以做到这一点,它看起来与我所做的完全相同,只是需要指定一个额外的chartId 这是我的密码: def add_sheet_chart(service, which_sheet, last_row): response = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False)

我最近一直在努力通过python更新google工作表数据范围。有一个json负载应该可以做到这一点,它看起来与我所做的完全相同,只是需要指定一个额外的
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
def添加工作表(服务,哪个工作表,最后一行):
response=service.spreadsheets().get(spreadsheetId=spreadsheet\u id,ranges=[],includeGridData=False)。execute()
sheet\u数组=response.get('sheets')
#在此处获取现有图表id,如果没有图表,则保留为-1。
工作表id=0
图表id=-1
对于图纸阵列中的图纸:
如果sheet.get('properties').get('title')==哪个工作表:
chart\u array=sheet.get('charts')
如果图表\u数组:
如果len(图表数组)!=0:
chart\u id=chart\u数组[0]。获取('chartId')
sheet_id=sheet.get('properties').get('sheetId'))
打破
title=“…”
底部标题=“…”
左标题=“…”
请求=[]
如果chart_id==-1:#返回语句会弄乱您的代码!
找到图表时(
chart\u 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!
没有理由让这个
返回到那里。删除它以使图表成功更新

参考:
返回语句弄乱了您的代码! 找到图表时(
chart\u 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!
没有理由让这个
返回到那里。删除它以使图表成功更新

参考:

您能否提供一份您正在处理的电子表格的副本,其中没有敏感信息,其中明确规定了图表的当前状态和所需状态?您能否提供一份您正在处理的电子表格的副本,没有敏感信息,其中明确规定了图表的当前状态和所需状态?谢谢!我想这与谷歌表单api无关,很抱歉给您带来麻烦。谢谢!我想这与google sheets api无关,很抱歉给您带来麻烦。