[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
        
(Showing Draft Content)

排序

SpreadJS 允许您对数据透视表数据进行排序,以便您可以对其进行组织以便快速方便地进行分析。

处理大量数据时,排序特别有用。您可以使用PivotTable.sort接受对象sortInfo作为参数的函数对数据进行排序。

SpreadJS 允许用户以四种不同的方式对数据透视表数据进行排序和排序:

  1. 按字段项名称排序

  2. 按值排序

  3. 按自定义字段项值排序

  4. 按自定义回调排序

按字段项名称排序

要按字段对数据透视表数据进行排序,您可以指定字段名称并使用属性sortType并将sortInfo排序顺序设置为升序、降序或自定义。

下图演示了应用于“类别”字段的降序排序顺序。

枢轴排序字段名称

以下代码示例显示了如何按字段项名称对数据透视表数据进行排序:

let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
  sortType: GC.Pivot.SortType.desc
 };
pivotTable.sort("Category", sortInfo);

使用设计器

您还可以通过单击字段的筛选器下拉列表中的更多排序选项...,通过设计器对数据进行排序。此选项会打开一个排序字段对话框,您可以在其中选择目标字段并将排序类型设置为升序或降序。

下图演示了如何使用设计器按字段项名称排序:

枢轴排序字段名称

按值排序

要按字段值对数据透视表数据进行排序,您可以设置类的sortTypesortValueFieldName属性sortInfo。sortValueFieldName 属性允许您指定要作为数据排序依据的字段的名称。此功能允许您按“总计”列中的值对数据透视表数据进行排序。

下图演示了按“数量”值升序排序的“类别”字段。

按值排序

以下代码示例显示如何对数据透视表应用按值排序。

let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
var sortInfo = {
    sortType: GC.Pivot.SortType.asc,
    sortValueFieldName: "Quantity"
 };
  pivotTable.sort("Category", sortInfo);

使用设计器

您还可以通过单击字段的筛选器下拉列表中的更多排序选项...,通过设计器对数据进行排序。此选项会打开一个排序字段对话框,您可以在其中选择目标字段并将排序类型设置为升序或降序。

下图演示了如何使用设计器按值排序:

按值排序

您还可以使用值区域上下文菜单中的排序选项按值对数据透视表数据进行排序。上下文菜单提供以下三个排序选项:

  • 从小到大排序

  • 从大到小排序

  • 更多排序选项...

前两个选项分别按升序和降序对值进行排序,而“更多排序选项...”会根据值区域打开一个对话框。

下表描述了通过以下值区域的上下文菜单访问时此选项的行为方式:

价值区

对话

行为

正常值区

按值排序1


出现“按值排序”对话框。

总面积

按值排序2


出现“按值排序”对话框。但是,排序方向与以下条件一起被禁用:

  • 对于总计列,方向默认为从上到下。

  • 如果是总计行,方向默认是从左到右。

  • 如果是总计行和列,则不会出现该对话框。

行标题区/角区

按值排序3


打开“排序字段”对话框以及以下条件:

  • 在布局紧凑的情况下,排序选项不会出现在角落区域。

  • 如果所选字段是 ΣValues 字段,将出现按值排序对话框。

  • 如果选中的行区域是ΣValues项,则不会出现对话框。

列标题区域

按值排序4


打开“排序字段”对话框以及以下条件:

  • 如果所选字段是 ΣValues 字段,将出现按值排序对话框。

  • 如果选择的列区域是ΣValues项,则不会出现对话框。

按自定义字段项目值排序

要按自定义字段项值对数据透视表数据进行排序,您可以使用对象sortByPivotReferences的属性sortInfo

下图演示了根据OrderDate字段的Qtr1中的数据对Category字段进行升序排序。

按自定义值进行数据透视排序

以下代码示例显示了如何设置按自定义字段项值排序:

let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
  sortType: GC.Pivot.SortType.asc,
  sortValueFieldName: "Quantity",
  sortByPivotReferences: [
     {
        fieldName: "OrderDate",
        items: ["Qtr1"]
     }
    ]
}
pivotTable.sort("Category", sortInfo);

使用设计器

您还可以通过单击字段的过滤器下拉列表中的更多排序选项...,通过设计器对数据进行排序此选项将打开一个排序字段对话框,您可以在其中选择目标字段的排序顺序,然后单击更多选项...按钮以打开另一个排序对话框。在此对话框中,默认情况下,GrandTotal 处于选中状态,其结果与按值排序的结果相同。要按自定义字段项值排序,请使用所选列中的值的范围选择器选择一个范围,然后单击确定。

行或列区域字段的对话框选项被禁用。例如,在这种情况下,如果您单击类别字段的更多选项...按钮,它将被禁用,因此您必须选择数量字段,然后单击更多选项...按钮

下图演示了如何使用设计器按自定义字段项值排序:

按自定义字段值进行数据透视排序

按自定义回调排序

要使用自定义逻辑对数据透视表数据进行排序,而不是按值的升序或降序排序,您可以使用自定义回调函数。

下图演示了使用自定义回调函数对TotalSales列进行排序。

按自定义回调进行数据透视排序

以下代码示例显示了使用自定义回调对 TotalSales 列进行排序:

let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
       sortType: GC.Pivot.SortType.desc,
       customSortCallback: (fieldItemNameArray)=>{
       alert("sorting from custom callback")
       return fieldItemNameArray.sort((a,b)=>b-a);
    }
  };
pivotTable.sort("TotalSales", sortInfo);