LibreOffice Module test (master) 1
xnamedranges.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
11#include <rtl/ustring.hxx>
12
13#include <com/sun/star/container/XIndexAccess.hpp>
14#include <com/sun/star/sheet/Border.hpp>
15#include <com/sun/star/sheet/NamedRangeFlag.hpp>
16#include <com/sun/star/sheet/XCellRangeAddressable.hpp>
17#include <com/sun/star/sheet/XCellRangeReferrer.hpp>
18#include <com/sun/star/sheet/XNamedRange.hpp>
19#include <com/sun/star/sheet/XNamedRanges.hpp>
20#include <com/sun/star/table/CellAddress.hpp>
21#include <com/sun/star/table/CellRangeAddress.hpp>
22#include <com/sun/star/table/XCell.hpp>
23#include <com/sun/star/text/XTextRange.hpp>
24
25#include <com/sun/star/uno/Reference.hxx>
26
27#include <cppunit/TestAssert.h>
28
29#include <iostream>
30
31using namespace css;
32using namespace css::uno;
33
34namespace apitest
35{
37{
38 uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
39 uno::Reference<container::XNameAccess> xNamedRangesNameAccess(getXNamedRanges(1),
40 UNO_QUERY_THROW);
41
42 table::CellAddress aBaseAddress(0, 0, 0);
43
44 sal_Int32 nType = 0;
45 OUString aName1("type_0");
46 xNamedRanges->addNewByName(aName1, "D1", aBaseAddress, nType);
47 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type 0 - Normal case",
48 xNamedRanges->hasByName(aName1));
49
50 nType = ::sheet::NamedRangeFlag::COLUMN_HEADER;
51 OUString aName2("type_COLUMN_HEADER");
52 xNamedRanges->addNewByName(aName2, "D2", aBaseAddress, nType);
53 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type COLUMN_HEADER",
54 xNamedRanges->hasByName(aName2));
55
56 nType = ::sheet::NamedRangeFlag::FILTER_CRITERIA;
57 OUString aName3("type_FILTER_CRITERIA");
58 xNamedRanges->addNewByName(aName3, "D3", aBaseAddress, nType);
59 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type FILTER_CRITERIA",
60 xNamedRanges->hasByName(aName3));
61
62 nType = ::sheet::NamedRangeFlag::PRINT_AREA;
63 OUString aName4("type_PRINT_AREA");
64 xNamedRanges->addNewByName(aName4, "D4", aBaseAddress, nType);
65 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type PRINT_AREA",
66 xNamedRanges->hasByName(aName4));
67
68 nType = ::sheet::NamedRangeFlag::ROW_HEADER;
69 OUString aName5("type_ROW_HEADER");
70 xNamedRanges->addNewByName(aName5, "D5", aBaseAddress, nType);
71 CPPUNIT_ASSERT_MESSAGE("Failed to create Namedrange Type ROW_HEADER",
72 xNamedRanges->hasByName(aName5));
73
74 // tdf#119457 - check for a valid range name
75 OUString aName6("type_INVALID_BAD_STRING.+:");
76 CPPUNIT_ASSERT_THROW(xNamedRanges->addNewByName(aName6, "D6", aBaseAddress, 0),
77 uno::RuntimeException);
78 CPPUNIT_ASSERT_MESSAGE("Created Namedrange with invalid name",
79 !xNamedRanges->hasByName(aName6));
80}
81
83{
84 uno::Reference<sheet::XNamedRanges> xNamedRanges(getXNamedRanges(1), UNO_QUERY_THROW);
85 uno::Reference<container::XIndexAccess> xNamedRangesIndex(xNamedRanges, UNO_QUERY_THROW);
86 uno::Reference<container::XNameAccess> xNamedRangesNameAccess(xNamedRanges, UNO_QUERY_THROW);
87
88 table::CellRangeAddress aCellRangeAddress(1, 0, 0, 3, 3);
89
90 OUString aString;
91 uno::Reference<table::XCell> xCell;
92
93 xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
94
95 for (sal_Int32 i = 1; i < 4; i++)
96 {
97 // verify namedrange exists
98 xCell = xSheet->getCellByPosition(i, 0);
99 uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
100 aString = xTextRange->getString();
101 std::cout << "addNewFromTitles: verify " << aString << std::endl;
102 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
103
104 // verify it points on the right cell
105 uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
106 uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
107
108 uno::Reference<sheet::XCellRangeReferrer> xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
109 uno::Reference<sheet::XCellRangeAddressable> xCellRangeAdr(
110 xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
111 table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
112
113 CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.EndColumn);
114 CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.StartColumn);
115 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), cellRangeAddress.StartRow);
116 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), cellRangeAddress.EndRow);
117 CPPUNIT_ASSERT_EQUAL(sal_Int16(1), cellRangeAddress.Sheet);
118 }
119
120 xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
121
122 for (sal_Int32 i = 1; i < 4; i++)
123 {
124 // verify namedrange exists
125 xCell = xSheet->getCellByPosition(0, i);
126 uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
127 aString = xTextRange->getString();
128 std::cout << "verify " << aString << std::endl;
129 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
130
131 // verify it points on the right cell
132 uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
133 uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
134
135 uno::Reference<sheet::XCellRangeReferrer> xCellRangeRef(xNamedRange, UNO_QUERY_THROW);
136 uno::Reference<sheet::XCellRangeAddressable> xCellRangeAdr(
137 xCellRangeRef->getReferredCells(), UNO_QUERY_THROW);
138 table::CellRangeAddress cellRangeAddress = xCellRangeAdr->getRangeAddress();
139
140 CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.EndRow);
141 CPPUNIT_ASSERT_EQUAL(i, cellRangeAddress.StartRow);
142 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), cellRangeAddress.StartColumn);
143 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), cellRangeAddress.EndColumn);
144 CPPUNIT_ASSERT_EQUAL(sal_Int16(1), cellRangeAddress.Sheet);
145 }
146}
147
149{
150 uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
151 uno::Reference<container::XIndexAccess> xIndex(xNamedRanges, UNO_QUERY_THROW);
152
153 bool bHasIt = xNamedRanges->hasByName(maNameToRemove);
154 CPPUNIT_ASSERT_MESSAGE("NamedRange initial1 does not exist, can't remove it", bHasIt);
155
156 if (!bHasIt)
157 return;
158
159 // remove existing
160 sal_Int32 nInitialCount = xIndex->getCount();
161 xNamedRanges->removeByName(maNameToRemove);
162 sal_Int32 nNewCount = xIndex->getCount();
163 CPPUNIT_ASSERT_EQUAL_MESSAGE("NamedRange initial1 not removed", nNewCount, nInitialCount - 1);
164 CPPUNIT_ASSERT_MESSAGE("Wrong NamedRange removed, initial1 still present",
165 !xNamedRanges->hasByName(maNameToRemove));
166 // try to remove non existing
167 xNamedRanges->removeByName("dummyNonExistingNamedRange"); // an exception should be raised here
168}
169
171{
172 table::CellAddress aCellAddress(0, 2, 0);
173 uno::Reference<sheet::XNamedRanges> xNamedRanges(init(), UNO_QUERY_THROW);
174 uno::Reference<container::XIndexAccess> xNamedRangesIndex(init(), UNO_QUERY_THROW);
175 sal_Int32 nElementsCount = xNamedRangesIndex->getCount();
176
177 xNamedRanges->outputList(aCellAddress);
178
179 OUString aString;
180 uno::Reference<table::XCell> xCell;
181
182 for (sal_Int32 i = 0; i < nElementsCount; i++)
183 {
184 xCell = xSheet->getCellByPosition(2, i);
185 uno::Reference<text::XTextRange> xTextRange(xCell, UNO_QUERY_THROW);
186 aString = xTextRange->getString();
187 std::cout << "verify " << aString << std::endl;
188 CPPUNIT_ASSERT_MESSAGE("Non existing NamedRange", xNamedRanges->hasByName(aString));
189 }
190}
191
192} // namespace apitest
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::uno::XInterface > getXNamedRanges(sal_Int32 nSheet=0)=0
css::uno::Reference< css::sheet::XSpreadsheet > xSheet
virtual css::uno::Reference< css::uno::XInterface > init()=0
int i
QPRO_FUNC_TYPE nType