11#include <rtl/ustring.hxx>
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>
25#include <com/sun/star/uno/Reference.hxx>
27#include <cppunit/TestAssert.h>
32using namespace css::uno;
38 uno::Reference<sheet::XNamedRanges> xNamedRanges(
init(), UNO_QUERY_THROW);
39 uno::Reference<container::XNameAccess> xNamedRangesNameAccess(
getXNamedRanges(1),
42 table::CellAddress aBaseAddress(0, 0, 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));
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));
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));
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));
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));
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));
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);
88 table::CellRangeAddress aCellRangeAddress(1, 0, 0, 3, 3);
91 uno::Reference<table::XCell> xCell;
93 xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_TOP);
95 for (sal_Int32
i = 1;
i < 4;
i++)
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));
105 uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
106 uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
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();
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);
120 xNamedRanges->addNewFromTitles(aCellRangeAddress, sheet::Border_LEFT);
122 for (sal_Int32
i = 1;
i < 4;
i++)
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));
132 uno::Any aNr = xNamedRangesNameAccess->getByName(aString);
133 uno::Reference<sheet::XNamedRange> xNamedRange(aNr, UNO_QUERY_THROW);
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();
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);
150 uno::Reference<sheet::XNamedRanges> xNamedRanges(
init(), UNO_QUERY_THROW);
151 uno::Reference<container::XIndexAccess> xIndex(xNamedRanges, UNO_QUERY_THROW);
154 CPPUNIT_ASSERT_MESSAGE(
"NamedRange initial1 does not exist, can't remove it", bHasIt);
160 sal_Int32 nInitialCount = xIndex->getCount();
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",
167 xNamedRanges->removeByName(
"dummyNonExistingNamedRange");
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();
177 xNamedRanges->outputList(aCellAddress);
180 uno::Reference<table::XCell> xCell;
182 for (sal_Int32
i = 0;
i < nElementsCount;
i++)
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));
virtual css::uno::Reference< css::uno::XInterface > getXNamedRanges(sal_Int32 nSheet=0)=0
void testAddNewFromTitles()
css::uno::Reference< css::sheet::XSpreadsheet > xSheet
virtual css::uno::Reference< css::uno::XInterface > init()=0