[]
        
(Showing Draft Content)

ICalculatedItems

Interface ICalculatedItems

All Superinterfaces:
Iterable<IPivotItem>

public interface ICalculatedItems extends Iterable<IPivotItem>
A collection of IPivotItem objects that represent all the calculated items in the specified PivotTable report.

Use this interface to access, add, and remove calculated items for a pivot field. Obtain an instance from 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()
      Returns the number of IPivotItem objects in the collection.

      This value represents the total number of calculated items defined for the associated pivot field. Returns 0 if the collection does not contain any calculated items.

      
       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:
      The number of IPivotItem objects in the collection.
    • get

      IPivotItem get(int index)
      Gets the IPivotItem at the specified index.

      Use this method to retrieve a calculated item from the collection returned by 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 - The zero-based index of the calculated item.
      Returns:
      The IPivotItem at the specified index.
    • get

      IPivotItem get(String itemName)
      Gets the IPivotItem item by name.

      Use this method to retrieve a calculated item from the ICalculatedItems collection of a pivot field.

      
       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 - The name of the calculated item to retrieve.
      Returns:
      The IPivotItem with the specified name.
    • add

      IPivotItem add(String name, String formula)
      Creates a new calculated item. Returns a IPivotItem object.

      Use the returned item to access the calculated item after it is added to the pivot field's calculated items collection obtained from 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 - The name of the calculated item.
      formula - The formula used to define the calculated item.
      Returns:
      The created IPivotItem.
    • remove

      void remove(String itemName)
      Removes the calculated item with the specified name.

      This method removes a calculated item from the current ICalculatedItems collection obtained from IPivotField.getCalculatedItems(). Item name matching is case-insensitive. If no calculated item with the specified name exists, this method does nothing.

      
       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 - The name of the calculated item to remove. If no matching calculated item exists, no item is removed.