更新切片器

更新切片器

更新切片器是指对切片器所连接的数据透视表或表格的数据进行筛选。Aspose.Cells for Python via Java 会更新切片器,并直接根据筛选项重新计算所关联的数据透视表或表格:

  • Slicer.selectItems(labels, append) —— 选择指定的标签并重新计算所关联的数据透视表或表格。当 append 为 True 时,将保留原先选定的标签,并把指定的标签添加到选定内容中;当 append 为 False 时,将清除所有原选定内容,仅保留指定的标签处于选定状态。
  • Slicer.unselectItems(labels) —— 取消选择当前选定项中的一部分。
  • Slicer.clearFilter() —— 移除筛选并选择所有项目。

示例代码

替换当前选定内容(仅保留指定的标签处于选定状态)并重新计算所关联的数据透视表或表格:

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 current selection and add "Red" to it.
slicer.selectItems(["Red"], True)

取消选择部分已选定的项目:

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

移除筛选(选择所有项目):

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