如何使用 Google 工作表 API 在枢轴 table 中按值 "grand total" 排序

How to sort by value "grand total" in pivot table using Google Sheets API

我使用下面的代码通过引用 sheet id 自动创建一个数据透视表 table。我想知道如何使用总计列对数据透视表 table 进行排序。当我去 spreadsheet 时我可以手动完成,但我想自动完成。我检查了 google sheet 的文档 HERE 但无法弄清楚。也许它与创建存储桶有关但不确定。

这是我当前的工作代码,没有对值部分进行排序:

      {
        "requests": [
          {
            "updateCells": {
              "rows": {
                "values": [
                  {
                    "pivotTable": {
                      "source": {
                        "sheetId": source_sheet_id,
                        "startRowIndex": 0,
                        "startColumnIndex": 0,
                        "endRowIndex": max_row_count,
                        "endColumnIndex": 3 #total columns
                      },
                      "rows": [
                        {
                          "sourceColumnOffset": code_index,
                          "showTotals": True,
                          "sortOrder": "ASCENDING",
                          "valueBucket": {
                            "buckets": [
                              {
                                "stringValue": "West"
                              }
                            ]
                          }
                        }
                      ],
                      "columns": [
                        {
                          "sourceColumnOffset": month_index,
                          "sortOrder": "ASCENDING",
                          "showTotals": True,
                          "valueBucket": {
                             "buckets": [
                              {
                                "stringValue": "Panel"
                              }
                            ]
                          },
                        }
                      ],
                      "values": [
                        {
                          "summarizeFunction": "SUM",
                          "sourceColumnOffset": counts_index
                        }
                      ],
                      "valueLayout": "HORIZONTAL"
                    }
                  }
                ]
              },
              "start": {
                "sheetId": destination_sheet_id,
                "rowIndex": 0,
                "columnIndex": 0
              },
              "fields": "pivotTable"
            }
          }
        ]
      }

向以下 url 发送 POST 请求(填写您的电子表格 ID):https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate

包含以下有效负载(插入总计列号的实际值):

{
  "requests": [
    {
      "sortRange": {
        "range": {
          "sheetId": sheetId,
          "startRowIndex": 0,
          "endRowIndex": 10,
          "startColumnIndex": 0,
          "endColumnIndex": 6
        },
        "sortSpecs": [
          {
            "dimensionIndex": Index of GrandTotal Column,
            "sortOrder": "ASCENDING"
          }
        ]
      }
    }
  ]
}

请在此处查看完整文档:https://developers.google.com/sheets/api/samples/data(最底部的示例)

我遇到了同样的问题,经过长时间的研究后我修复了它。

诀窍是在每一行中添加一个空的 "valueBucket" 键,如下所示:

    "rows": [
  {
    "sourceColumnOffset": 0,
    "showTotals": true,
    "sortOrder": "DESCENDING",
    "valueBucket": {}
  },

然后行将按总计列排序。

我在这里找到了答案:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#SortOrder

它说:"For example, in a pivot table with one row group & two column groups, the row group can list up to two values. The first value corresponds to a value within the first column group, and the second value corresponds to a value in the second column group. If no values are listed, this would indicate that the row should be sorted according to the "Grand Total" over the column groups。如果列出单个值,这将对应于使用该存储桶的 "Total"。"。 =15=]

在您的情况下,数据取自此处:https://developers.google.com/sheets/api/samples/pivot-tables

检查此 Sheet 示例:https://docs.google.com/spreadsheets/d/1f5s03qLAVaLfUfkyvjZtllVNz0FxAmn2jEmL9wYVGh8/edit?usp=sharing

在示例中,此函数根据您的代码创建了数据透视表 table,其中的值按总计列排序。

function makePivot() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheetId = sheet.getSheetByName("Data").getSheetId(); 

  // Create a new sheet which will contain our Pivot Table
  var pivotTableSheet = sheet.insertSheet("Pivot1");
  var pivotTableSheetId = pivotTableSheet.getSheetId();  

  var request = {    
    "updateCells": {
      "rows": {
        "values": [
          {
            "pivotTable": {
              "source": {
                "sheetId": sourceSheetId,
                "startRowIndex": 0,
                "startColumnIndex": 0,
                "endRowIndex": 20,
                "endColumnIndex": 7
              },
              "rows": [
                {
                  "sourceColumnOffset": 0,
                  "showTotals": true,
                  "sortOrder": "DESCENDING",
                  "valueBucket": {}
                },
                {
                  "sourceColumnOffset": 1,
                  "showTotals": true,
                  "sortOrder": "DESCENDING",
                  "valueBucket": {}
                }
              ],
              "columns": [
                {
                  "sourceColumnOffset": 4,
                  "sortOrder": "ASCENDING",
                  "showTotals": true}
              ],
              "values": [
                {
                  "summarizeFunction": "SUM",
                  "sourceColumnOffset": 3
                }
              ],
              "valueLayout": "HORIZONTAL"
            }
          }
        ]
      },
      "start": {
        "sheetId": pivotTableSheetId,
        "rowIndex": 2,
        "columnIndex": 0
      },
      "fields": "pivotTable"
    }
  }

  Sheets.Spreadsheets.batchUpdate({'requests': [request]}, sheet.getId());
}

希望对您有所帮助!