From The Document Foundation Wiki
< Macros‎ | Calc
Jump to navigation Jump to search
This page is a translated version of the page Macros/Calc/ba025 and the translation is 10% complete.
Other languages:
English • ‎español • ‎italiano


This page provides a LibreOffice Basic macro procedure that retrieves the sorting information for a given cell range. It is an example drawn from Chapter 13 ("Calc as a Database") of the 7.0 Calc Guide.


You can use the method createSortDescriptor() to retrieve the sorting information for a given cell range. If this method is called on a database range, it will create a sort descriptor using the sorting information stored with that range. On the other hand, if createSortDescriptor() is called on a standard named range or an unnamed range, it will generate a sort descriptor with default properties. In either case, the newly-generated sort descriptor can be modified and passed as an argument to a sort method called on a given range.

The macro procedure below demonstrates how to generate and display the sorting information associated with a range. The output of this macro is a dialog box displaying sort descriptor properties.


Sub DisplaySortDescriptor
  On Error Resume Next
  Dim oSheet
  Dim oRange       ' A range is needed to create the sort descriptor.
  Dim oSortDescript
  Dim i%
  Dim s$
  Dim oDoc         ' Reference newly created calc document.
  oDoc = StarDesktop.loadComponentFromURL("private:factory/scalc", "_default", 0, Array())
  oSheet = oDoc.Sheets(0)
  oRange = oSheet.getCellRangeByName("B28:D33")
  oSortDescript = oRange.createSortDescriptor()
  For i = LBound(oSortDescript) To UBound(oSortDescript)
    s = s & oSortDescript(i).Name & " = "
    s = s & oSortDescript(i).Value
    s = s & CHR$(10)
  MsgBox s, 0, "Sort Descriptor"
End Sub

This Calc spreadsheet contains the above LibreOffice Basic code.