这是LibreOffice Calc宏。目标是从一个工作表上的列中过滤唯一数据,将其粘贴到另一工作表上,然后对粘贴的值进行排序。但是,输出列未得到排序。
对于工作表“ final”和“ lists”,正在从$final.$m2:$m100
读取数据,并最终从$lists.$C
开始在$lists.$C2
列中。过滤正常进行,仅捕获唯一的行,包括空单元格(这是需要的)。
共有三个子功能:
runit
(用于测试)filterDistinct
(过滤数据,设置范围)sortRange
(对结果进行排序)您看到代码有什么问题吗?
sub runit
filterDistinct( "final", "$m2:$m100", "lists", "$C2", false, false)
end sub
Sub filterDistinct( _
strSourceSheet As String, _
strSourceRange As String, _
strTargetSheet As String, _
strTargetCell As String, _
Optional bContainsHeader As Boolean, _
Optional bCaseSensitive As Boolean _
)
GlobalScope.BasicLibraries.loadLibrary("Tools")
' Uses a Filter to copy distinct rows from the
' specified Source Range into a new Range that starts from the specified Target Cell.
'
' <strSourceRange> : specifies the Range to find distinct rows in, e.g. "A1:B99".
' <strTargetCell> : specifies the Cell to put the first found distinct row in, e.g. "D1".
' <bContainsHeader> : OPTIONAL - pass TRUE if the Source Range contains a Header.
' <bCaseSensitive> : OPTIONAL - pass TRUE if case matters while searching for distinct rows.
Dim oSheet As Object, oTargetSheet As Object, oSourceRange As Object, oTargetRange As Object, oFilter As Object
oSheet = ThisComponent.getSheets().getByName(strSourceSheet)
oSourceRange = oSheet.getCellRangebyName( strSourceRange )
oTargetSheet = ThisComponent.getSheets().getByName(strTargetSheet)
oTargetRange = oTargetSheet.getCellRangebyName( strTargetCell )
oTargetSortRange = oTargetSheet.getCellRangebyName( "$C1:$C40000" )
'msgbox oSheet.getName()
'msgbox oSourceRange.AbsoluteName
'msgbox oTargetSheet.getName()
'msgbox oTargetRange.AbsoluteName
'msgbox oTargetSortRange.AbsoluteName
'sortRange( oTargetSortRange, 0, 1 )
'exit sub
oFilter = oSourceRange.createFilterDescriptor( True )
oFilter.SkipDuplicates = True
oFilter.CopyOutputData = True
oFilter.OutputPosition = oTargetRange.CellAddress
If Not IsMissing( bContainsHeader ) Then oFilter.ContainsHeader = bContainsHeader
If Not IsMissing( bCaseSensitive ) Then oFilter.IsCaseSensitive = bCaseSensitive
oSourceRange.filter( oFilter )
sortRange( oTargetSortRange, 1, 1 )
End Sub
' sorts the cell range xRange by the iColumn (0 = first) column in IsAscending (=T/F) order:
function sortRange( _
xRange As Object, _
iColumn As Integer, _
IsAscending As Boolean _
)
GlobalScope.BasicLibraries.loadLibrary("Tools")
Dim oSortDesc(4) As New com.sun.star.beans.PropertyValue
Dim aSortFields As Object
' Dim oField As New com.sun.star.sheet.TableFilterField2
'oField.Field=0
'oField.Operator=com.sun.star.sheet.FilterOperator2.NOT_EMPTY
'oFilterDesc.setFilterFields2(Array(oField))
' define the fields to sort
aSortFields = New com.sun.star.util.SortField
aSortFields.Field = iColumn
aSortFields.SortAscending = IsAscending
' define the sort descriptor
oSortDesc(0) = new com.sun.star.beans.PropertyValue
oSortDesc(0).Name = "SortFields"
oSortDesc(0).Value = aSortFields
oSortDesc(1) = new com.sun.star.beans.PropertyValue
oSortDesc(1).Name = "ContainsHeader"
oSortDesc(1).Value = true
oSortDesc(2) = new com.sun.star.beans.PropertyValue
oSortDesc(2).Name = "IsCaseSensitive"
oSortDesc(2).Value = False
oSortDesc(3) = new com.sun.star.beans.PropertyValue
oSortDesc(3).Name = "ContainsHeader"
oSortDesc(3).Value = True
' Select the range and sort it.
' ThisComponent.getCurrentController.select( xRange )
msgbox xRange.AbsoluteName
xRange.Sort( oSortDesc )
'dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
end function
答案 0 :(得分:1)
有几个小问题。
iColumn
必须为0才能使用第一列和唯一列。aSortFields
应该是一个数组。sortRange
用作子例程,而不是函数。它可能仍然有效,但是不正确。以下是基于清单6.28:从Andrew's macro document降到Calc文档中的排序的示例工作代码。
Sub doSort
oSheet = ThisComponent.getSheets().getByIndex(0)
oTargetSortRange = oSheet.getCellRangebyName( "$C1:$C40000" )
sortRange( oTargetSortRange, 0, 1 )
End Sub
' sorts the cell range xRange by the iColumn (0 = first) column in IsAscending (=T/F) order:
Sub sortRange( _
xRange As Object, _
iColumn As Integer, _
IsAscending As Boolean _
)
Dim oSortDesc(0) As New com.sun.star.beans.PropertyValue
Dim aSortFields(0) As New com.sun.star.util.SortField
aSortFields(0).Field = iColumn
aSortFields(0).SortAscending = IsAscending
oSortDesc(0).Name = "SortFields"
oSortDesc(0).Value = aSortFields()
xRange.Sort( oSortDesc )
End Sub