[]
        
首页
开发者学堂
文档
论坛
市场
生态机会
活动
立即试用
(Showing Draft Content)

ICalculatedItems

Interface ICalculatedItems

All Superinterfaces:
Iterable<IPivotItem>

public interface ICalculatedItems extends Iterable<IPivotItem>
表示指定数据透视表报表中所有计算项的 IPivotItem 对象集合。

使用此接口可以访问、添加和移除数据透视字段的计算项。可从 IPivotField.getCalculatedItems() 获取实例。


 worksheet.getRange("A1:C4").setValue(new Object[][] {
     {"Product", "Category", "Amount"},
     {"Apple", "Fruit", 120},
     {"Banana", "Fruit", 80},
     {"Bran", "Snack", 60}
 });
 IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
 IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
 pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
 pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
 pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
 ICalculatedItems calculatedItems = pivotTable.getPivotFields().get("Product").getCalculatedItems();
 IPivotItem item = calculatedItems.add("TotalFruit", "=Product[Apple] + Product[Banana]");
 
  • Method Details

    • getCount

      int getCount()
      返回集合中的 IPivotItem 对象数量。

      此值表示为关联数据透视字段定义的计算项总数。如果集合不包含任何计算项,则返回 0。

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Region", "Amount"},
           {"North", 100},
           {"South", 200}
       });
       IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B3"));
       IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
       pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
       ICalculatedItems items = pivotTable.getPivotFields().get("Region").getCalculatedItems();
       items.add("NorthAndSouth", "=Region[North]+Region[South]");
       int count = items.getCount();
       
      Returns:
      集合中的 IPivotItem 对象数量。
    • get

      IPivotItem get(int index)
      获取指定索引处的 IPivotItem。

      使用此方法可以从 IPivotField.getCalculatedItems() 返回的集合中检索计算项。

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Country", "Amount"},
           {"France", 100},
           {"Germany", 200},
           {"Canada", 150}
       });
       IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
       IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
       pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.RowField);
       pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
       ICalculatedItems calculatedItems = pivotTable.getPivotFields().get("Country").getCalculatedItems();
       calculatedItems.add("Europe", "=France+Germany");
       IPivotItem item = calculatedItems.get(0);
       
      Parameters:
      index - 计算项的零基索引。
      Returns:
      指定索引处的 IPivotItem。
    • get

      IPivotItem get(String itemName)
      按名称获取 IPivotItem 项。

      使用此方法可以从数据透视字段的 ICalculatedItems 集合中检索计算项。

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Status", "Amount"},
           {"Pending", 100},
           {"Backorder", 200},
           {"Shipped", 300}
       });
       IWorksheet pivotSheet = workbook.getWorksheets().add();
       IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
       IPivotTable pivotTable = pivotSheet.getPivotTables().add(pivotCache, pivotSheet.getRange("A1"));
       pivotTable.getPivotFields().get("Status").setOrientation(PivotFieldOrientation.RowField);
       pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
       ICalculatedItems calculatedItems = pivotTable.getPivotFields().get("Status").getCalculatedItems();
       calculatedItems.add("Open Orders", "=Pending+Backorder");
       IPivotItem item = calculatedItems.get("Open Orders");
       
      Parameters:
      itemName - 要检索的计算项的名称。
      Returns:
      具有指定名称的 IPivotItem。
    • add

      IPivotItem add(String name, String formula)
      创建新的计算项。返回 IPivotItem 对象。

      将计算项添加到从 IPivotField.getCalculatedItems() 获取的数据透视字段计算项集合后,可以使用返回的项访问该计算项。

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Country", "Amount"},
           {"France", 100},
           {"Germany", 200},
           {"Canada", 150}
       });
       IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
       IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
       IPivotItem item = pivotTable.getPivotFields().get("Country").getCalculatedItems().add("Europe", "=France+Germany");
       
      Parameters:
      name - 计算项的名称。
      formula - 用于定义计算项的公式。
      Returns:
      创建的 IPivotItem。
    • remove

      void remove(String itemName)
      移除具有指定名称的计算项。

      此方法从当前 ICalculatedItems 集合中移除计算项,该集合从 IPivotField.getCalculatedItems() 获取。项名称匹配不区分大小写。如果不存在具有指定名称的计算项,此方法不执行任何操作。

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Status", "Amount"},
           {"Pending", 100},
           {"Backorder", 200},
           {"Shipped", 300}
       });
       IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
       IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
       pivotTable.getPivotFields().get("Status").setOrientation(PivotFieldOrientation.RowField);
       pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
       ICalculatedItems calculatedItems = pivotTable.getPivotFields().get("Status").getCalculatedItems();
       calculatedItems.add("Open Orders", "=Pending+Backorder");
       calculatedItems.remove("Open Orders");
       
      Parameters:
      itemName - 要移除的计算项名称。如果不存在匹配的计算项,则不移除任何项。