Updating Slicer

Updating Slicer

Aspose.Cells for Python via Java supports updating slicers. For this, the API provides the Slicer.SlicerCache.SlicerCacheItems property that is used to select or unselect slicer items. The following code snippet loads the sample Excel file that contains a slicer.  It unselects the 2nd and 3rd items of the slicer and refreshes the slicer using the Slicer.refresh() method. It then saves the workbook as the output Excel file. The following screenshot shows the effect of the sample code on the sample Excel file. As you can see in the screenshot, refreshing the slicer with selected items has also refreshed the pivot table accordingly.

todo:image_alt_text

Sample Code

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, SaveFormat
# Load Source Excel file
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)
# Access the slicer items
scItems = slicer.getSlicerCache().getSlicerCacheItems()
# Unselect 2nd and 3rd slicer items
scItems.get(1).setSelected(False)
scItems.get(2).setSelected(False)
# Refresh the slicer
slicer.refresh()
# Save the workbook in output XLSX format
workbook.save("outputUpdatingSlicer.xlsx", SaveFormat.XLSX)
jpype.shutdownJVM()