REM ***** BASIC *****
Option Explicit
Sub SAC
'calls: getSelectedRanges
dim oDoc as object
dim oView as object
'dim oSheet as object
dim oSel as object
dim oAddr as object
dim nSCol as long
dim nSRow as long
dim nERow as long
dim nECol as long
dim MySum as double
dim MyAverage as double
dim MyCount as double
dim MyRowC as LONG
dim MyColC as LONG
dim oRange as object
dim svc as Object
dim e
oDoc = ThisComponent
oView = oDoc.getCurrentController()
oSel = getSelectedRanges(oView)
e = oSel.createEnumeration()
while e.hasMoreElements()
oRange = e.nextElement
oAddr = oRange.getRangeAddress()
nSCol = oAddr.StartColumn
nSRow = oAddr.StartRow
nECol = oAddr.EndColumn
nERow = oAddr.EndRow
MyRowC= MyRowC +nERow - nSRow +1
MyColC= MyColC +nECol - nSCol +1
wend
MySum = oSel.computeFunction(com.sun.star.sheet.GeneralFunction.SUM)
MyCount = oSel.computeFunction(com.sun.star.sheet.GeneralFunction.COUNT)
MyAverage = oSel.computeFunction(com.sun.star.sheet.GeneralFunction.AVERAGE)
MsgBox("Sum = " & MySum & Chr(13) & "Average = " & MyAverage & Chr(13) & "Count = " & MyCount & Chr(13) & "Row count = " & MyRowC & Chr(13) & "Col count = " & MyColC, 64, "SAC : ")
end sub
Function getSelectedRanges(oController)
REM calls: getActiveCell
Dim oSelect
REM return a collection of ranges, regardless of the actual selection
oSelect = oController.getSelection()
If oSelect.supportsService("com.sun.star.sheet.SheetCellRanges") then
getSelectedRanges = oSelect
elseif oSelect.supportsService("com.sun.star.sheet.SheetCellRange") then
REM convert single range to collection of one range:
getSelectedRanges = oSelect.queryIntersection(oSelect.getRangeAddress())
else
REM collection of active cell
oSelect = getActiveCell(oController)
getSelectedRanges = oSelect.queryIntersection(oSelect.getRangeAddress())
endif
End Function
'-------usefull helper-function, returning focussed cell
'by UROS > http://www.oooforum.org/forum/viewtopic.phtml?t=19348
REM 2006-08-09: fixed error when row > 8191
' ;sh; ;lSheet +3
'100/60/0;1;tw:309;2/2/0/0/0/0/2/0/0/0/0;253/8191/0/0/0/0/2/246/0/0/8158;0/0/0/0/0/0/2/0/0/0/0
'100/60/0;1;tw:309;2/2/0/0/0/0/2/0/0/0/0;253+8192+0+0+0+0+2+246+0+0+8158;0/0/0/0/0/0/2/0/0/0/0
Function getActiveCell(oView)
Dim as1(), lSheet&,lCol&,lRow$, sDum as String,bErr as Boolean
as1() = Split(oView.ViewData, ";")
lSheet = CLng(as1(1))
sDum = as1(lSheet +3)
as1() = Split(sDum, "/")
on error goto errSlash
lCol = CLng(as1(0))
lRow = CLng(as1(1))
on error goto 0
getActiveCell = oView.Model.getSheets.getByIndex(lSheet).getcellByPosition(lCol,lRow)
exit Function
errSlash:
if NOT(bErr) then
bErr = True
as1() = Split(sDum, "+")
resume
endif
End Function
This is a nice technique, perhaps the easiest way, however:Villeroy wrote:Hit F2 in any cell and select the range in question. This will fill a reference while showing the dimensions in a tooltip. Then hit Escape to undo the edit.
MyAverage = oSel.computeFunction(com.sun.star.sheet.GeneralFunction.AVERAGE)
if MyCount=0 then MyAverage=0 else MyAverage = svc.callFunction("AVERAGE", Array(oRange))
Users browsing this forum: No registered users and 12 guests