2010年1月19日 星期二

學習記錄:OpenOffice Basic (10)

 
取得 activecell 的行數及列數

方法一

Sub Main
Set oCell = ThisComponent.getCurrentSelection
msgbox oCell.CellAddress.Column
msgbox oCell.CellAddress.Row

End Sub

方法二

Sub Main

ActiveColumn=ThisComponent.CurrentController.getSelection().RangeAddress.StartColumn
ActiveRow=ThisComponent.CurrentController.getSelection().RangeAddress.StartRow
print ActiveColumn,ActiveRow

End Sub

方法三

Sub Macro1
Dim oRange As Object
Dim SC As String
Dim EC As String
Dim SR As String
Dim ER As String
oRange = ThisComponent.CurrentSelection.getRangeAddress
SC = oRange.StartColumn+1 '行列??????????????1?加??
EC = oRange.EndColumn+1
SR = oRange.StartRow+1
ER = oRange.EndRow+1

If SR = ER AND SC = EC Then '1?????場合???範??場合?分??表示??
MsgBox "R" & SR & "C" & SC
Else
MsgBox "R" & SR & "C" & SC & ":R" & ER & "C" & EC
End If
End Sub

'取得指定 cell 的最後一欄及最後一列

方法四

REM ***** BASIC *****
rem Which cells are used in a sheet?
rem A SheetCellCursor implements the methods
rem gotoStartOfUsedArea(boolean) and gotoEndOfUsedArea(boolean),
rem which move the cursor to the start and end of the area used on a spreadsheet.
rem Thanks to Johnny Rosenberg for an improved version of these functions.

Sub testEndColRow
Dim oSheet
Dim oCell
Dim nEndCol As Integer
Dim nEndRow As Integer
oSheet = ThisComponent.Sheets.getByIndex( 0 )
nEndCol = getLastUsedColumn(oSheet)
nEndRow = getLastUsedRow(oSheet)
oCell = oSheet.GetCellByPosition( nEndCol + 1, nEndRow + 1 )
oCell.String = "test"

End Sub

Function GetLastUsedColumn(oSheet) As Integer
Dim oCursor
oCursor = oSheet.createCursor
oCursor.GotoEndOfUsedArea(True)
GetLastUsedColumn = oCursor.RangeAddress.EndColumn

msgbox "GetLastUsedColumn is " & GetLastUsedColumn

End Function

Function GetLastUsedRow(oSheet) As Integer
Dim oCursor
oCursor = oSheet.createCursor
oCursor.GotoEndOfUsedArea(True)
GetLastUsedRow = oCursor.RangeAddress.EndRow

msgbox "GetLastUsedRow is " & GetLastUsedRow
End Function
 
 
 

沒有留言: