通过谷歌表api和python折叠谷歌表中数据透视表中的所有总数 [英] Collapse all totals in a pivot table in google sheet by google sheet api and python

查看:68
本文介绍了通过谷歌表api和python折叠谷歌表中数据透视表中的所有总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过python中的api创建了谷歌表数据透视表.但是,我无法用总计折叠行.我只是找不到解决方案来做到这一点.我正在使用 batchUpdate 函数来创建数据透视表.

I created google sheet pivot table through the api in python. But, I am unable to collapse rows with totals. I just cant find solution to do that. I am using batchUpdate function to create pivottable.

有没有可能?

谷歌表格示例视图

这是我创建的用于执行数据透视表的代码.

Here is the code I create to do the pivot table.

    def create_pivot_table(self):
    spreadsheet = self.google_drive.SHEETS.spreadsheets()   
    #result = sheet.values().get(spreadsheetId=self.file_id, range='Sheet1!B1:B10').execute()
    requests = []
    # Change the spreadsheet's title.
    # [START sheets_pivot_tables]
    requests.append({
        'updateCells': {
            'rows': {
                'values': [
                    {
                        'pivotTable': {
                            'source': {
                                'sheetId': self._get_sheet_id_by_name('Sheet1'),
                                'startRowIndex': 0,
                                'startColumnIndex': 0,
                            },
                            'rows': [
                                {
                                    'sourceColumnOffset': 8,
                                    'showTotals': True,
                                    'sortOrder': 'ASCENDING'
                                },
                                {
                                    'sourceColumnOffset': 1,
                                    'sortOrder': 'ASCENDING',
                                    'showTotals': True,
                                }
                            ],
                            'values': [
                                {
                                    'summarizeFunction': 'SUM',
                                    'sourceColumnOffset': 16
                                },
                                                                    {
                                    'summarizeFunction': 'SUM',
                                    'sourceColumnOffset': 17
                                },
                                                                    {
                                    'summarizeFunction': 'SUM',
                                    'sourceColumnOffset': 18
                                }
                            ],
                            'valueLayout': 'HORIZONTAL'
                        }
                    }
                ]
            },
            'start': {
                'sheetId': self._get_sheet_id_by_name('PivotTable'),
                'rowIndex': 0,
                'columnIndex': 0
            },
            'fields': 'pivotTable'
        }
    })

    body = {
        'requests': requests
    }

    response = spreadsheet.batchUpdate(spreadsheetId=self.file_id, body=body).execute()
    return response

推荐答案

检查 PivotGroupValueMetadata :

折叠布尔值

如果该值对应的数据被折叠,则为真.

True if the data corresponding to the value is collapsed.

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/pivot-tables#pivotgroupvaluemetadata

并检查:

https://developers.google.com/sheets/api/samples/pivot-tables#edit_pivot_table_columns_and_rows

折叠每个区域的列,West"除外,隐藏该地区的 Salesperson 组.这是通过设置完成的在 Region 中该列的 valueMetadata 中折叠为 true列组.

Collapses the column for each Region, with the exception of "West", hiding the Salesperson group for that region. This is done by setting collapsed to true in the valueMetadata for that column in the Region column group.

这篇关于通过谷歌表api和python折叠谷歌表中数据透视表中的所有总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆