 | OO-Snippets: Improve sorting capabilitiesCommons| Keywords | sort, sorting |
|---|
| Language | OOBasic |
|---|
| Application | Calc |
|---|
| Authors | Stefan Weigel (initial)
|
|---|
| Supported Versions | 2.1.0 2.0.x |
|---|
| Supported OS | All |
|---|
| Question |
|
|---|
| Answer | This macro provides an improved sorting function for the user. It enables to sort using as many sort criteria as desired. (Calc normally allows max. 3 criteria.) The sort criteria to be used is determined by the currently active cell. (Calc normally uses the first column.) The macro recognizes if there are column headers (Calc normally does not recognize column header when using the sort icons from the toolbar) The macro overcomes issue #7277 and issue #20491. For background info and long description see http://www.stefan-weigel.de/?ID=83. For German text see http://www.stefan-weigel.de/?ID=81 |
|---|
option explicit
sub SWsortUp()
thisComponent.lockcontrollers
SWSort true
thisComponent.unlockcontrollers
end sub
sub SWsortDown()
thisComponent.lockcontrollers
SWSort false
thisComponent.unlockcontrollers
end sub
sub SWsort(blnUpDown)
Dim oSheet
Dim oList as Object
Dim intListStartColumn
Dim intListEndColumn
Dim lngListStartRow
Dim lngListEndRow
Dim intListCountColumnn
Dim lngListCountRown
Dim intCriteriaColumn as Integer
Dim blnShowHeader
Dim i as Integer
Dim oRange as Object
Dim aSortFields(1) as New com.sun.star.table.TableSortField
Dim aSortDesc(1) as New com.sun.star.beans.PropertyValue
oSheet = ThisComponent.CurrentController.ActiveSheet
oList = thisComponent.CurrentSelection
if oList.supportsService("com.sun.star.sheet.SheetCellRanges") then
msgbox "It's not allowed to sort more than one cell-range!",,"© Ingenieurbüro Weigel"
exit sub
end if
oRange = thisComponent.createInstance("com.sun.star.sheet.SheetCellRanges")
ThisComponent.CurrentController.Select(oRange)
intCriteriaColumn = ThisComponent.CurrentSelection.getCellAddress.Column
ThisComponent.CurrentController.Select(oList)
SelectCurrentRange
intListStartColumn = ThisComponent.CurrentSelection.getRangeAddress.StartColumn
intListEndColumn = ThisComponent.CurrentSelection.getRangeAddress.EndColumn
intListCountColumnn = intListEndColumn - intListStartColumn
lngListStartRow = ThisComponent.CurrentSelection.getRangeAddress.StartRow
lngListEndRow = ThisComponent.CurrentSelection.getRangeAddress.EndRow
lngListCountRown = lngListEndRow - lngListStartRow + 1
intCriteriaColumn = intCriteriaColumn - intListStartColumn
if lngListCountRown = 1 then exit sub
blnShowHeader = false
for i=intListStartColumn to intListEndColumn
if oSheet.getCellByPosition(i,lngListStartRow).FormulaResultType <> oSheet.getCellByPosition(i,lngListStartRow+1).FormulaResultType and _
oSheet.getCellByPosition(i,lngListStartRow).FormulaResultType <> 0 and _
oSheet.getCellByPosition(i,lngListStartRow+1).FormulaResultType <> 0 then
blnShowHeader = true
exit for
end if
next i
if blnShowHeader = false then
for i=intListStartColumn to intListEndColumn
if oSheet.getCellByPosition(i,lngListStartRow).CellStyle <> oSheet.getCellByPosition(i,lngListStartRow+1).CellStyle then
blnShowHeader = true
exit for
end if
next i
end if
oSheet.Columns.insertByIndex(intListEndColumn+1,1)
for i=lngListStartRow to lngListEndRow
oSheet.getCellByPosition(intListEndColumn+1,i).value=i
next i
oList =oSheet.getCellRangeByPosition(intListStartColumn,lngListStartRow,intListEndColumn+1,lngListEndRow)
aSortFields(0).Field = intCriteriaColumn
aSortFields(0).IsAscending = blnUpDown
aSortFields(0).IsCaseSensitive = false
aSortFields(1).Field = intListEndColumn+1
aSortFields(1).IsAscending = true
aSortFields(1).IsCaseSensitive = false
aSortDesc(0).Name = "SortFields"
aSortDesc(0).Value = aSortFields()
aSortDesc(1).Name = "ContainsHeader"
aSortDesc(1).Value = blnShowHeader
oList.sort(aSortDesc())
oSheet.Columns.removeByIndex(intListEndColumn+1,1)
oList =oSheet.getCellRangeByPosition(intListStartColumn,lngListStartRow,intListEndColumn,lngListEndRow)
ThisComponent.CurrentController.Select(oList)
end sub
sub SelectCurrentRange
dim oDisp as object
dim oDoc as object
dim Array()
oDoc = ThisComponent.CurrentController.Frame
oDisp = createUnoService("com.sun.star.frame.DispatchHelper")
oDisp.executeDispatch(oDoc, ".uno:SortAscending", "", 0, Array())
oDisp.executeDispatch(ThisComponent.CurrentController.Frame,".uno:Undo", "",0, Array())
End Sub
|
Changelog| Date | User | Modification |
|---|
| 2007-02-08 | tomsontom | Translated to english | | 2007-01-25 | swubuntu | Bug Fix | | 2006-10-22 | swubuntu | Minor code improvements |
|