Updating Slicer

Updating Slicer

Updating a slicer means filtering the data of the pivot table or table that the slicer is connected to. Aspose.Cells for Python via Java updates the slicer and recalculates the linked pivot table or table directly from the filter items:

  • Slicer.selectItems(labels, append) — selects the specified labels and recalculates the linked pivot table or table. When append is True, the originally selected labels are kept and the specified labels are added to the selection; when append is False, all original selections are cleared and only the specified labels stay selected.
  • Slicer.unselectItems(labels) — unselects part of the currently selected items.
  • Slicer.clearFilter() — removes the filter and selects all items.

Sample Code

Replace the current selection (only the specified labels stay selected) and recalculate the linked pivot table or table:

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook
# Load sample Excel file containing slicer.
workbook = Workbook("sampleUpdatingSlicer.xlsx")
# Access first worksheet.
worksheet = workbook.getWorksheets().get(0)
# Access the first slicer inside the slicer collection.
slicer = worksheet.getSlicers().get(0)
# Select only "Blue" and "Pink"; the linked pivot table or table
# is recalculated directly from the filter items.
slicer.selectItems(["Blue", "Pink"], False)
# Save the modified workbook.
workbook.save("out.xlsx")
jpype.shutdownJVM()

Keep the currently selected labels and additionally select more items:

# Keep the current selection and add "Red" to it.
slicer.selectItems(["Red"], True)

Unselect part of the selected items:

# Remove "Pink" from the current selection.
slicer.unselectItems(["Pink"])

Remove the filter (select all items):

# Clear the filter; all slicer items become selected again.
slicer.clearFilter()