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

保罗·吉尼奥提斯

我最近一直在努力通过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工作表图表的讨论,我并不感到惊讶。如果有帮助,图表也会被发布。

am

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要在那里。删除它以使您的图表成功更新。

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何设置用于图表的表格数据范围

如何通过应用脚本从Google表格获取特定范围的行数据

Google表格导入范围返回了不正确的数据

如何使用应用脚本更新现有图形的数据范围?[Google表格]

访问Google表格并通过Python更新

如何通过自动选择正确的日期范围(星期一至星期日)来获取Google表格的小时数

如果图表没有数据,如何从 Google 幻灯片中删除 Google 表格图表

添加行时,如何更新Google表格中的公式范围?

Google图表(表格图表)

Google可视化图表数据范围

如何从excel更新powerpoint图表中的范围

Google表格图表-如何删除垂直标题

如何通过 API 在 Google 表格中启用“数据标签”?

自动更新Google表格范围

如何使用抓取的硒数据更新 Google 表格

如何正确更新图表值?(实时)

通过Google App脚本在Google表格上编辑多行时如何获得真实范围

如何按工作日在 Google 表格图表上显示 4 周的数据?

如何使用 Google Apps 脚本删除电子表格中特定范围内的图表集?

Google表格-水平范围的数据验证

从Google表格中的命名范围提取数据

如何使用API数据正确更新React&Chart.js应用中的图表

如何通过表格API v4更新Google电子表格

如何通过python删除谷歌表格中的重复数据

如何获取列范围内的特定数据列?Google Apps 脚本 - Google 表格

关于使用gspread API通过Python读取Google表格时范围值的问题

如何更新 Google 表格插件

通过PHP,MySQL更新通过ajax调用的图表数据

无法从Google表格正确获取数据