How to get the data of automatically filtered cells in a sheet using Python?

I want to get the current data filter information, create filters or remove filters based on certain rules, but I do not see appropriate attributes on the sheet object.

To handle filters in Collabora Online using PyUNO:

  1. Check for Applied Filters: Use sheet.createFilterDescriptor(True) to access filter fields and criteria if any are applied.
    LibreOffice: XSheetFilterable Interface Reference

  2. Set Filters: Create a TableFilterField structure with your criteria and apply it with sheet.filter(filter_descriptor).
    LibreOffice: TableFilterField Struct Reference

  3. Remove Filters: Apply an empty descriptor sheet.filter(empty_filter_descriptor) to clear existing filters.

  4. Access Filtered Rows: Loop through rows and check .IsVisible to skip hidden rows, reading only visible (filtered-in) data.

This approach leverages PyUNO to manage and retrieve filtered data in a Collabora Online sheet.

def has_auto_filter():
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
fd = sheet.createFilterDescriptor(True)
print(len(fd.getFilterFields())==0) # Always returning true

@darshan Hello, I tried but it still didn’t work. I selected the first row as the header in a sheet, clicked the auto filter button, and chose to filter data in a specific column. Then I executed the following code. However, I couldn’t get any information about the filtered data. Above is my code

Hello @gulixiang ,

Thanks for reaching out! I see you’ve tried applying a filter to a sheet with code but couldn’t retrieve the filtered data. To help us assist you more effectively, could you clarify a few details? Specifically, it would be helpful to know:

  • Which product you’re working with?
  • Any specific requirements or goals you have in mind while using the LibreOffice API?

We’ve seen a rise in complex queries recently that often require significant R&D. As much as we’d love to support all these use cases, our current capacity is limited. To prioritize fairly, we focus primarily on our partners and customers to ensure quality service. Offering the same level of support to both paid and community support could be unfair to our dedicated partners.

If you’re looking for comprehensive and dedicated assistance, we encourage you to explore our engineering support options for in-depth help.

Also, if you’re interested, we have a sample integration project you could explore or even contribute to, which would be greatly appreciated! You can check it out here: Collabora Online SDK Examples.

Thank you,
Darshan