通过 google sheet api 和 python 折叠 table in google sheet 中的所有总计

Collapse all totals in a pivot table in google sheet by google sheet api and python

我通过 python 中的 api 创建了 google sheet 支点 table。 但是,我无法用总计折叠行。我只是找不到解决方案来做到这一点。我正在使用 batchUpdate 函数创建 pivottable.

有没有可能?

google sheets example view

这是我创建的用于执行枢轴的代码 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 :

collapsed boolean

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

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.