3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 from testcollections_base
import CollectionsTestBase
13 from com
.sun
.star
.table
import CellAddress
15 # TextTable instance factory
16 def getTextTableInstance(doc
):
17 return doc
.createInstance('com.sun.star.text.TextTable')
19 # Tests behaviour of objects implementing XCellRange using the new-style
20 # collection accessors
22 class TestXCellRange(CollectionsTestBase
):
24 # TODO negative indices
27 # cell = cellrange[0,0] # Access cell by indices
30 # Cell at Row 0, Col 0
31 def test_XCellRange_Spreadsheet_Cell_00(self
):
33 spr
= self
.createBlankSpreadsheet()
40 self
.assertEqual(0, cell
.CellAddress
.Sheet
)
41 self
.assertEqual(0, cell
.CellAddress
.Row
)
42 self
.assertEqual(0, cell
.CellAddress
.Column
)
47 # cell = cellrange[0,0] # Access cell by indices
50 # Cell at Row 0, Col 0
51 def test_XCellRange_Table_Cell_00(self
):
53 doc
= self
.createBlankTextDocument()
54 text_table
= getTextTableInstance(doc
)
55 text_table
.initialize(10, 10)
56 cursor
= doc
.Text
.createTextCursor()
57 doc
.Text
.insertTextContent(cursor
, text_table
, False)
58 tbl
= doc
.TextTables
[0]
64 self
.assertEqual('A1', cell
.CellName
)
69 # cell = cellrange[0,0] # Access cell by indices
72 # Cell at Row 3, Col 7
73 def test_XCellRange_Spreadsheet_Cell_37(self
):
75 spr
= self
.createBlankSpreadsheet()
82 self
.assertEqual(0, rng
.CellAddress
.Sheet
)
83 self
.assertEqual(3, rng
.CellAddress
.Row
)
84 self
.assertEqual(7, rng
.CellAddress
.Column
)
89 # cell = cellrange[0,0] # Access cell by indices
92 # Cell at Row 3, Col 7
93 def test_XCellRange_Table_Cell_37(self
):
95 doc
= self
.createBlankTextDocument()
96 text_table
= getTextTableInstance(doc
)
97 text_table
.initialize(10, 10)
98 cursor
= doc
.Text
.createTextCursor()
99 doc
.Text
.insertTextContent(cursor
, text_table
, False)
100 tbl
= doc
.TextTables
[0]
106 self
.assertEqual('H4', cell
.CellName
)
111 # rng = cellrange[0,1:2] # Access cell range by index,slice
114 def test_XCellRange_Spreadsheet_Range_Index_Slice(self
):
116 spr
= self
.createBlankSpreadsheet()
123 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
124 self
.assertEqual(0, rng
.RangeAddress
.StartRow
)
125 self
.assertEqual(1, rng
.RangeAddress
.StartColumn
)
126 self
.assertEqual(0, rng
.RangeAddress
.EndRow
)
127 self
.assertEqual(2, rng
.RangeAddress
.EndColumn
)
132 # rng = cellrange[0,1:2] # Access cell range by index,slice
135 def test_XCellRange_Table_Range_Index_Slice(self
):
137 doc
= self
.createBlankTextDocument()
138 text_table
= getTextTableInstance(doc
)
139 text_table
.initialize(10, 10)
140 cursor
= doc
.Text
.createTextCursor()
141 doc
.Text
.insertTextContent(cursor
, text_table
, False)
142 tbl
= doc
.TextTables
[0]
143 doc
.lockControllers()
144 tbl
.DataArray
= tuple(tuple(str(100 + y
) for y
in range(10*x
, 10*x
+ 10)) for x
in range(10))
145 doc
.unlockControllers()
151 self
.assertEqual((('101', '102'),), rng
.DataArray
)
156 # rng = cellrange[1:2,0] # Access cell range by slice,index
159 def test_XCellRange_Spreadsheet_Range_Slice_Index(self
):
161 spr
= self
.createBlankSpreadsheet()
168 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
169 self
.assertEqual(1, rng
.RangeAddress
.StartRow
)
170 self
.assertEqual(0, rng
.RangeAddress
.StartColumn
)
171 self
.assertEqual(2, rng
.RangeAddress
.EndRow
)
172 self
.assertEqual(0, rng
.RangeAddress
.EndColumn
)
177 # rng = cellrange[1:2,0] # Access cell range by index,slice
180 def test_XCellRange_Table_Range_Slice_Index(self
):
182 doc
= self
.createBlankTextDocument()
183 text_table
= getTextTableInstance(doc
)
184 text_table
.initialize(10, 10)
185 cursor
= doc
.Text
.createTextCursor()
186 doc
.Text
.insertTextContent(cursor
, text_table
, False)
187 tbl
= doc
.TextTables
[0]
188 doc
.lockControllers()
189 tbl
.DataArray
= tuple(tuple(str(100 + y
) for y
in range(10*x
, 10*x
+ 10)) for x
in range(10))
190 doc
.unlockControllers()
196 self
.assertEqual((('110',), ('120',)), rng
.DataArray
)
201 # rng = cellrange[0:1,2:3] # Access cell range by slices
204 def test_XCellRange_Spreadsheet_Range_Slices(self
):
206 spr
= self
.createBlankSpreadsheet()
213 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
214 self
.assertEqual(1, rng
.RangeAddress
.StartRow
)
215 self
.assertEqual(3, rng
.RangeAddress
.StartColumn
)
216 self
.assertEqual(2, rng
.RangeAddress
.EndRow
)
217 self
.assertEqual(4, rng
.RangeAddress
.EndColumn
)
222 # rng = cellrange[0:1,2:3] # Access cell range by slices
226 def test_XCellRange_Spreadsheet_Range_Slices_Invalid(self
):
228 spr
= self
.createBlankSpreadsheet()
232 with self
.assertRaises(KeyError):
234 with self
.assertRaises(KeyError):
240 # rng = cellrange[0:1,2:3] # Access cell range by slices
243 def test_XCellRange_Table_Range_Slices(self
):
245 doc
= self
.createBlankTextDocument()
246 text_table
= getTextTableInstance(doc
)
247 text_table
.initialize(10, 10)
248 cursor
= doc
.Text
.createTextCursor()
249 doc
.Text
.insertTextContent(cursor
, text_table
, False)
250 tbl
= doc
.TextTables
[0]
251 doc
.lockControllers()
252 tbl
.DataArray
= tuple(tuple(str(100 + y
) for y
in range(10*x
, 10*x
+ 10)) for x
in range(10))
253 doc
.unlockControllers()
259 self
.assertEqual((('113', '114'), ('123', '124')), rng
.DataArray
)
264 # rng = cellrange['A1:B2'] # Access cell range by descriptor
267 def test_XCellRange_Spreadsheet_Range_Descriptor(self
):
269 spr
= self
.createBlankSpreadsheet()
276 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
277 self
.assertEqual(2, rng
.RangeAddress
.StartRow
)
278 self
.assertEqual(0, rng
.RangeAddress
.StartColumn
)
279 self
.assertEqual(3, rng
.RangeAddress
.EndRow
)
280 self
.assertEqual(1, rng
.RangeAddress
.EndColumn
)
285 # rng = cellrange['A1:B2'] # Access cell range by descriptor
288 def test_XCellRange_Table_Range_Descriptor(self
):
290 doc
= self
.createBlankTextDocument()
291 text_table
= getTextTableInstance(doc
)
292 text_table
.initialize(10, 10)
293 cursor
= doc
.Text
.createTextCursor()
294 doc
.Text
.insertTextContent(cursor
, text_table
, False)
295 tbl
= doc
.TextTables
[0]
296 doc
.lockControllers()
297 tbl
.DataArray
= tuple(tuple(str(100 + y
) for y
in range(10*x
, 10*x
+ 10)) for x
in range(10))
298 doc
.unlockControllers()
304 self
.assertEqual((('120', '121'), ('130', '131')), rng
.DataArray
)
309 # rng = cellrange['Name'] # Access cell range by name
312 def test_XCellRange_Spreadsheet_Range_Name(self
):
314 spr
= self
.createBlankSpreadsheet()
316 expr
= '$' + sht
.Name
+ '.$C2:F10'
317 addr
= CellAddress(Sheet
=0, Row
=1, Column
=2)
318 sht
.NamedRanges
.addNewByName('foo', expr
, addr
, 0)
324 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
325 self
.assertEqual(1, rng
.RangeAddress
.StartRow
)
326 self
.assertEqual(2, rng
.RangeAddress
.StartColumn
)
327 self
.assertEqual(9, rng
.RangeAddress
.EndRow
)
328 self
.assertEqual(5, rng
.RangeAddress
.EndColumn
)
333 # rng = cellrange[0] # Access cell range by row index
336 def test_XCellRange_Spreadsheet_Range_RowIndex(self
):
338 spr
= self
.createBlankSpreadsheet()
345 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
346 self
.assertEqual(0, rng
.RangeAddress
.StartRow
)
347 self
.assertEqual(0, rng
.RangeAddress
.StartColumn
)
348 self
.assertEqual(0, rng
.RangeAddress
.EndRow
)
349 self
.assertEqual(16383, rng
.RangeAddress
.EndColumn
)
354 # rng = cellrange[0,:] # Access cell range by row index
357 def test_XCellRange_Spreadsheet_Range_RowIndex_FullSlice(self
):
359 spr
= self
.createBlankSpreadsheet()
366 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
367 self
.assertEqual(0, rng
.RangeAddress
.StartRow
)
368 self
.assertEqual(0, rng
.RangeAddress
.StartColumn
)
369 self
.assertEqual(0, rng
.RangeAddress
.EndRow
)
370 self
.assertEqual(16383, rng
.RangeAddress
.EndColumn
)
375 # rng = cellrange[:,0] # Access cell range by column index
378 def test_XCellRange_Spreadsheet_Range_FullSlice_ColumnIndex(self
):
380 spr
= self
.createBlankSpreadsheet()
387 self
.assertEqual(0, rng
.RangeAddress
.Sheet
)
388 self
.assertEqual(0, rng
.RangeAddress
.StartRow
)
389 self
.assertEqual(0, rng
.RangeAddress
.StartColumn
)
390 self
.assertEqual(1048575, rng
.RangeAddress
.EndRow
)
391 self
.assertEqual(0, rng
.RangeAddress
.EndColumn
)
396 if __name__
== '__main__':
399 # vim:set shiftwidth=4 softtabstop=4 expandtab: