1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="table_XCellRange" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8'  Licensed to the Apache Software Foundation (ASF) under one
9'  or more contributor license agreements.  See the NOTICE file
10'  distributed with this work for additional information
11'  regarding copyright ownership.  The ASF licenses this file
12'  to you under the Apache License, Version 2.0 (the
13'  "License"); you may not use this file except in compliance
14'  with the License.  You may obtain a copy of the License at
15'
16'    http://www.apache.org/licenses/LICENSE-2.0
17'
18'  Unless required by applicable law or agreed to in writing,
19'  software distributed under the License is distributed on an
20'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21'  KIND, either express or implied.  See the License for the
22'  specific language governing permissions and limitations
23'  under the License.
24'
25'*************************************************************************
26
27
28
29
30
31' Be sure that all variables are dimensioned:
32option explicit
33
34'*************************************************************************
35' You can only get ranges within your
36' object-range. That means is your object-range
37' is (A1:C3) you can get only a range within
38' (A1:C3).
39
40'*************************************************************************
41
42
43
44
45
46Sub RunTest()
47
48'*************************************************************************
49' INTERFACE:
50' com.sun.star.table.XCellRange
51'*************************************************************************
52On Error Goto ErrHndl
53    Dim bOK As Boolean
54
55    Dim oCell As Object
56    Dim oCellRange As Object
57    Dim oCellRange1 As Object
58    Dim bSupport As Boolean
59    Dim oAddress As Object
60    Dim nSCol As Long, nECol As Long
61    Dim nSRow As Long, nERow As Long
62    Dim nCol As Long, nRow As Long
63    Dim cSCol As String, cECol As String
64
65    bOK = true
66    'does the object support this service? If yes, the object is a range itself.
67    'So we must look for the range address to be get a valid range
68    if hasUnoInterfaces( oObj, "com.sun.star.sheet.XCellRangeAddressable" ) then
69        Out.Log("Object supports com.sun.star.sheet.XCellRangeAddressable")
70        bSupport = true
71        oAddress = oObj.getRangeAddress()
72        nSCol = oAddress.StartColumn
73        nECol = oAddress.EndColumn
74        nSRow = oAddress.StartRow
75        nERow = oAddress.EndRow
76        nCol = nECol - nSCol
77        nRow = nERow - nSRow
78    else
79        bSupport = false
80        nCol = 1
81        nRow = 1
82    end if
83
84    Test.StartMethod("getCellByPosition()")
85    bOK = true
86    Out.Log("try to getCellByPosition(" + nCol + "," + nRow + ")")
87    oCell = oObj.getCellByPosition(nCol, nRow)
88    bOK = bOK AND hasUnoInterfaces( oCell, "com.sun.star.table.XCell" )
89    Test.MethodTested("getCellByPosition()", bOK)
90
91    Test.StartMethod("getCellRangeByPosition()")
92    bOK = true
93    Out.Log("try to getCellRangeByPosition(0,0," + nCol + "," + nRow + ")")
94    oCellRange = oObj.getCellRangeByPosition(0, 0, nCol, nRow)
95    bOK = bOK AND hasUnoInterfaces( oCellRange, "com.sun.star.table.XCellRange" )
96    Test.MethodTested("getCellRangeByPosition()", bOK)
97
98    Test.StartMethod("getCellRangeByName()")
99    bOK = true
100    if bSupport then
101        oAddress = oObj.getRangeAddress()
102        nSCol = oAddress.StartColumn
103        nECol = oAddress.EndColumn
104        nSRow = oAddress.StartRow
105        nERow = oAddress.EndRow
106        cSCol = getCharacter(nSCol)
107        cECol = getCharacter(nECol)
108    else
109        nSRow = 0
110        nERow = 2
111        cSCol = "A"
112        cECol = "C"
113    end if
114    Out.Log("Try to getCellRangeByName(""" + cSCol + (nSRow + 1) + ":" + cECol + (nERow + 1) + """)")
115    oCellRange1 = oObj.getCellRangeByName("" + cSCol + (nSRow + 1) + ":" + cECol + (nERow + 1))
116    bOK = bOK AND hasUnoInterfaces( oCellRange1, "com.sun.star.table.XCellRange" )
117    Test.MethodTested("getCellRangeByName()", bOK)
118
119Exit Sub
120ErrHndl:
121    Test.Exception()
122    bOK = false
123    resume next
124End Sub
125Function getCharacter( nCol as Integer) as String
126    Dim Char As String
127    Dim nNum As Integer
128    nNum = nCol
129    Char = ""
130    if (nNum - 26) &gt; 0 Then
131        Char = Chr((nNum mod 26)+65)
132        nNum = Int((nNum - 26)/ 26)
133    end if
134    Char = Chr(nNum + 65) + Char
135    getCharacter = Char
136End Function
137</script:module>
138