12#include <com/sun/star/beans/XPropertySet.hpp>
13#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
14#include <com/sun/star/sheet/XSpreadsheet.hpp>
15#include <com/sun/star/sheet/XSpreadsheets2.hpp>
16#include <com/sun/star/sheet/XNamedRanges.hpp>
17#include <com/sun/star/sheet/XNamedRange.hpp>
18#include <com/sun/star/table/XCell.hpp>
19#include <com/sun/star/text/XTextRange.hpp>
20#include <com/sun/star/container/XIndexAccess.hpp>
22#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
23#include <com/sun/star/container/XNameContainer.hpp>
24#include <com/sun/star/table/CellVertJustify.hpp>
25#include <com/sun/star/util/XCloseable.hpp>
27#include <rtl/ustring.hxx>
28#include <cppunit/TestAssert.h>
32using namespace css::uno;
51 uno::Reference<util::XCloseable> xCloseable(
xDestDoc, UNO_QUERY_THROW);
52 xCloseable->close(
true);
64 uno::Reference< container::XNameAccess > xDestSheetNameAccess(
xDestDoc->getSheets(), UNO_QUERY_THROW);
65 CPPUNIT_ASSERT_MESSAGE(
"Wrong sheet name", xDestSheetNameAccess->hasByName(
gaSrcSheetName));
76 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(0,0);
77 uno::Reference< text::XTextRange > xSrcTextRange(xSrcCell, UNO_QUERY_THROW);
78 OUString aSrcString = xSrcTextRange->getString();
80 uno::Reference< table::XCell > xDestCell =
xDestSheet->getCellByPosition(0,0);
81 uno::Reference< text::XTextRange > xDestTextRange(xDestCell, UNO_QUERY_THROW);
82 OUString aDestString = xDestTextRange->getString();
84 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong string imported", aSrcString, aDestString);
94 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(1,0);
95 sal_Int32 aSrcValue = xSrcCell->getValue();
97 uno::Reference< table::XCell > xDestCell =
xDestSheet->getCellByPosition(1,0);
98 sal_Int32 aDestValue = xDestCell->getValue();
100 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong value imported", aSrcValue, aDestValue);
110 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(2,0);
111 OUString aSrcFormula = xSrcCell->getFormula();
113 uno::Reference< table::XCell > xDestCell =
xDestSheet->getCellByPosition(2,0);
114 OUString aDestFormula = xDestCell->getFormula();
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong formula imported", aSrcFormula, aDestFormula);
128 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(3,0);
129 OUString aSrcFormula = xSrcCell->getFormula();
131 uno::Reference< table::XCell > xDestCell =
xDestSheet->getCellByPosition(3,0);
132 OUString aDestFormula = xDestCell->getFormula();
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong Namedrange formula imported", aSrcFormula, aDestFormula);
148 uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(
getNamedRanges(
xDestDoc), UNO_QUERY_THROW);
149 uno::Any aNr = xDestNamedRangesNameAccess->getByName(
"initial1");
150 uno::Reference< sheet::XNamedRange > xDestNamedRange(aNr, UNO_QUERY_THROW);
151 OUString aNrDestContent = xDestNamedRange->getContent();
153 std::cout <<
"testImportSheet : initial1 aNrDestContent " << aNrDestContent << std::endl;
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong address for initial1", OUString(
"$Sheet1.$B$1"), aNrDestContent);
168 OUString aNewInSheetNamedRangeString(
"InSheetRangeName");
169 uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(
getNamedRanges(
xDestDoc), UNO_QUERY_THROW);
170 CPPUNIT_ASSERT_MESSAGE(
"InSheetRangeName", xDestNamedRangesNameAccess->hasByName(aNewInSheetNamedRangeString));
172 uno::Any aNewInSheetNr = xDestNamedRangesNameAccess->getByName(aNewInSheetNamedRangeString);
173 uno::Reference< sheet::XNamedRange > xDestNewInSheetNamedRange(aNewInSheetNr, UNO_QUERY_THROW);
174 OUString aNewInSheetNrDestContent = xDestNewInSheetNamedRange->getContent();
175 OUString aNewInSheetExpectedContent(
"$SheetToCopy.$A$7");
177 std::cout <<
"testImportSheet : InSheetRangeName content " << aNewInSheetNrDestContent << std::endl;
178 std::cout <<
"testImportSheet : InSheetRangeName expected " << aNewInSheetExpectedContent << std::endl;
179 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong address for InSheetRangeName", aNewInSheetExpectedContent, aNewInSheetNrDestContent);
192 OUString aRedefinedInSheetNamedRangeString(
"initial2");
193 uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(
getNamedRanges(
xDestDoc), UNO_QUERY_THROW);
194 CPPUNIT_ASSERT_MESSAGE(
"aRedefinedInSheetNamedRangeString", xDestNamedRangesNameAccess->hasByName(aRedefinedInSheetNamedRangeString));
196 uno::Any aRedefinedInSheetNr = xDestNamedRangesNameAccess->getByName(aRedefinedInSheetNamedRangeString);
197 uno::Reference< sheet::XNamedRange > xDestRedefinedInSheetNamedRange(aRedefinedInSheetNr, UNO_QUERY_THROW);
198 OUString aRedefinedInSheetNrDestContent = xDestRedefinedInSheetNamedRange->getContent();
199 std::cout <<
"testImportSheet : initial2 content " << aRedefinedInSheetNrDestContent << std::endl;
200 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong address for Redefined InSheet named range", OUString(
"$Sheet1.$B$2"), aRedefinedInSheetNrDestContent);
213 OUString aNewNamedRangeString(
"new_rangename");
214 uno::Reference< container::XNameAccess > xDestNamedRangesNameAccess(
getNamedRanges(
xDestDoc), UNO_QUERY_THROW);
215 CPPUNIT_ASSERT_MESSAGE(
"New NamedRange not created", xDestNamedRangesNameAccess->hasByName(aNewNamedRangeString));
219 uno::Any aNewNr = xDestNamedRangesNameAccess->getByName(aNewNamedRangeString);
220 uno::Reference< sheet::XNamedRange > xDestNewNamedRange(aNewNr, UNO_QUERY_THROW);
221 OUString aNewNrDestContent = xDestNewNamedRange->getContent();
223 OUString aNewExpectedContent(
"$Sheet1.$B$1");
225 std::cout <<
"testImportSheet : new_rangename aNewExpectedContent " << aNewExpectedContent << std::endl;
226 std::cout <<
"testImportSheet : new_rangename aNewNrDestContent " << aNewNrDestContent << std::endl;
227 CPPUNIT_ASSERT_MESSAGE(
"Wrong New NamedRange formula string value",
isExternalReference(aNewNrDestContent, aNewExpectedContent));
241 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(3,0);
245 uno::Reference< beans::XPropertySet > xSrcCellPropSet (xSrcCell, UNO_QUERY_THROW);
246 static constexpr OUStringLiteral aCellProperty(
u"CellStyle");
247 OUString aSrcStyleName;
248 CPPUNIT_ASSERT(xSrcCellPropSet->getPropertyValue(aCellProperty) >>= aSrcStyleName);
250 uno::Reference< beans::XPropertySet > xDestCellPropSet (xSrcCell, UNO_QUERY_THROW);
251 OUString aDestStyleName;
252 CPPUNIT_ASSERT(xDestCellPropSet->getPropertyValue(aCellProperty) >>= aDestStyleName);
254 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong imported Cell Style", aSrcStyleName, aDestStyleName);
256 uno::Reference< style::XStyleFamiliesSupplier > xFamiliesSupplier (
xDestDoc, UNO_QUERY_THROW);
257 uno::Reference< container::XNameAccess > xFamiliesNameAccess (xFamiliesSupplier->getStyleFamilies(), UNO_SET_THROW);
258 uno::Any aCellStylesFamily = xFamiliesNameAccess->getByName(
"CellStyles");
259 uno::Reference< container::XNameContainer > xCellStylesFamilyNameAccess (aCellStylesFamily, UNO_QUERY_THROW);
261 CPPUNIT_ASSERT_MESSAGE(
"New cell style not present", xCellStylesFamilyNameAccess->hasByName(aDestStyleName));
263 uno::Any aCellStyle = xCellStylesFamilyNameAccess->getByName(aDestStyleName);
264 uno::Reference< beans::XPropertySet > xCellStyleProp (aCellStyle, UNO_QUERY_THROW);
265 sal_Int32 aVertJustify = 0;
266 CPPUNIT_ASSERT(xCellStyleProp->getPropertyValue(
"VertJustify") >>= aVertJustify);
268 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"New style: VertJustify not set", table::CellVertJustify_CENTER,
static_cast<table::CellVertJustify
>(aVertJustify));
279 constexpr sal_Int32 nCols = 7;
281 uno::Reference< container::XNameAccess> xSrcNameAccess(
init(),UNO_QUERY_THROW);
284 OUString aSrcString[nCols];
285 for (sal_Int32 nCol=0; nCol < nCols; ++nCol)
287 uno::Reference< table::XCell > xSrcCell =
xSrcSheet->getCellByPosition(nCol, 0);
288 uno::Reference< text::XTextRange > xSrcTextRange(xSrcCell, UNO_QUERY_THROW);
289 aSrcString[nCol] = xSrcTextRange->getString();
294 aSrcString[6] =
"content2";
298 uno::Reference< container::XIndexAccess > xDestSheetIndexAccess (
xDestDoc->getSheets(), UNO_QUERY_THROW);
299 CPPUNIT_ASSERT( nInsertedSheets < xDestSheetIndexAccess->getCount());
302 xDestSheet.set( xDestSheetIndexAccess->getByIndex(nSheet), UNO_QUERY_THROW);
303 for (sal_Int32 nCol=0; nCol < nCols; ++nCol)
305 uno::Reference< table::XCell > xDestCell =
xDestSheet->getCellByPosition(nCol, 0);
306 uno::Reference< text::XTextRange > xDestTextRange(xDestCell, UNO_QUERY_THROW);
307 OUString aDestString = xDestTextRange->getString();
309 if (nCol == 4 && aDestString ==
"Err:540")
314 OString aMessage(
"Imported result does not match, sheet " + OString::number(nSheet)
315 +
" column " + OString::number(nCol));
316 CPPUNIT_ASSERT_EQUAL_MESSAGE( aMessage.getStr(), aSrcString[nCol], aDestString);
324 uno::Reference< sheet::XSpreadsheetDocument > xDoc(xComp, UNO_QUERY_THROW);
330 uno::Reference< beans::XPropertySet > xPropSet (xDoc, UNO_QUERY_THROW);
331 uno::Reference< sheet::XNamedRanges > xNamedRanges(xPropSet->getPropertyValue(
"NamedRanges"), UNO_QUERY_THROW);
337 uno::Reference< container::XNameAccess> xSrcNameAccess(
init(),UNO_QUERY_THROW);
344 uno::Reference< sheet::XSpreadsheets2 > xDestSheets (
xDestDoc->getSheets(), UNO_QUERY_THROW);
345 sal_Int32 nDestPos = 0;
347 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Wrong sheet index", nDestPos, nDestPosEffective);
350 uno::Reference< container::XIndexAccess > xDestSheetIndexAccess (
xDestDoc->getSheets(), UNO_QUERY_THROW);
351 xDestSheet.set( xDestSheetIndexAccess->getByIndex(nDestPosEffective), UNO_QUERY_THROW);
361 return nPos != std::u16string_view::npos &&
nPos > 0;
css::uno::Reference< css::sheet::XSpreadsheet > xSrcSheet
css::uno::Reference< css::sheet::XSpreadsheet > xDestSheet
void testLastAfterInsertCopy()
void testImportNamedRangeDefinedInSource()
static css::uno::Reference< css::sheet::XNamedRanges > getNamedRanges(css::uno::Reference< css::sheet::XSpreadsheetDocument > const &)
void testImportFormulaBasicMath()
virtual css::uno::Reference< css::lang::XComponent > loadFromDesktop(const OUString &)=0
virtual css::uno::Reference< css::uno::XInterface > init()=0
static bool isExternalReference(std::u16string_view aDestContent, std::u16string_view aSrcContent)
virtual ~XSpreadsheets2()
void testImportCellStyle()
void testImportedSheetNameAndIndex()
void testImportNamedRangeRedefinedInSource()
css::uno::Reference< css::sheet::XSpreadsheetDocument > getDoc(const OUString &)
void testImportOverExistingNamedRange()
void testImportFormulaWithNamedRange()
css::uno::Reference< css::sheet::XSpreadsheetDocument > xDocument
css::uno::Reference< css::sheet::XSpreadsheetDocument > xDestDoc
void testImportNewNamedRange()
static sal_Int32 nInsertedSheets(0)
constexpr OUStringLiteral gaSrcFileName(u"rangenamessrc.ods")
constexpr OUStringLiteral gaSrcSheetName(u"SheetToCopy")
constexpr OUStringLiteral gaDestFileBase(u"ScNamedRangeObj.ods")
bool endsWithIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2, std::u16string_view *rest=nullptr)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept