Create Standard Filter

    From The Document Foundation Wiki
    < Macros‎ | Calc

    Other languages:


    This page provides a LibreOffice Basic macro procedure that creates a simple standard filter. It is an example drawn from Chapter 13 ("Calc as a Database") of the 7.0 Calc Guide.


    In a macro, filtering a data range is performed by calling the filter() method either on the range or the sheet in which the range is contained. Like a sort descriptor, a filter descriptor contains the filter settings for the current sheet, such as whether the first row or column in the sheet contains headers which should not be filtered. A filter descriptor can be generated by calling the createFilterDescriptor() method on a sheet or on a cell range:

    Creates a filter descriptor. If the boolean flag Empty is set to TRUE, then an empty filter descriptor is created. If Empty is FALSE, then the descriptor is filled with the previous settings of the current object (such as a database range).

    As with sort descriptors, filter descriptors can be created using this method, then modified and passed as an argument to the filter method. The macro below demonstrates this process by creating a simple standard filter for the first sheet in a document.

    Note pin.svg

    When the filter method is called on a sheet, every empty row in the sheet is hidden. When filter is called on a range, only empty rows in the range itself are hidden.


    Sub SimpleSheetFilter()
      Dim oSheet       ' Sheet that will contain the filter.
      Dim oFilterDesc  ' Filter descriptor.
      Dim oFields(0) As New
      oSheet = ThisComponent.getSheets().getByIndex(0)
      REM If argument is True, creates an empty filter
      REM descriptor. If argument is False, create a 
      REM descriptor with the previous settings.
      oFilterDesc = oSheet.createFilterDescriptor(True)
      With oFields(0)
        REM You could use the Connection property to indicate
        REM how to connect to the previous field. This is
        REM the first field, so this is not required.
        '.Connection =
        '.Connection =
        REM The Field property is the zero based column
        REM number to filter. If you have the cell, you
        REM can use .Field = oCell.CellAddress.Column.
        .Field = 5	' The Quiz #2 grades field
        REM Compare using a numeric or a string?
        .IsNumeric = True
        REM The NumericValue property is used 
        REM because .IsNumeric = True from above.
        .NumericValue = 80
        REM If IsNumeric was False, then the 
        REM StringValue property would be used.
        REM .StringValue = " what ever"
        REM Valid operators include EMPTY, NOT_EMPTY, EQUAL,
        .Operator =
      End With
      REM The filter descriptor supports the following
      REM properties: IsCaseSensitive, SkipDuplicates,
      REM UseRegularExpressions, 
      REM SaveOutputPosition, Orientation, ContainsHeader, 
      REM CopyOutputData, OutputPosition, and MaxFieldCount.
      oFilterDesc.ContainsHeader = True
    End Sub

    This Calc spreadsheet contains the above LibreOffice Basic code.