tdf#123090 Handle removed column with gridSpan.

Change-Id: If477845972ce4c8e7bf09ea8718c58851a5b9740
Signed-off-by: Gülşah Köse <gulsah.kose@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/68102
Tested-by: Jenkins
Reviewed-by: Andras Timar <andras.timar@collabora.com>
diff --git a/oox/source/drawingml/table/tableproperties.cxx b/oox/source/drawingml/table/tableproperties.cxx
index e0afeb7c..5bd61e8 100644
--- a/oox/source/drawingml/table/tableproperties.cxx
+++ b/oox/source/drawingml/table/tableproperties.cxx
@@ -282,21 +282,63 @@ void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase& rFilterBa
    std::unique_ptr<TableStyle> xTableStyleToDelete;
    const TableStyle& rTableStyle( getUsedTableStyle( rFilterBase, xTableStyleToDelete ) );
    sal_Int32 nRow = 0;

    for (auto & tableRow : mvTableRows)
    {
        sal_Int32 nColumn = 0;
        for (auto & tableCell : tableRow.getTableCells())
        sal_Int32 nColumnSize = tableRow.getTableCells().size();
        sal_Int32 nRemovedColumn = 0; //

        for (sal_Int32 nColIndex = 0; nColIndex < nColumnSize; nColIndex++)
        {
            TableCell& rTableCell(tableCell);
            TableCell& rTableCell(tableRow.getTableCells().at(nColIndex));

            if ( !rTableCell.getvMerge() && !rTableCell.gethMerge() )
            {
                uno::Reference< XTable > xTable( xColumnRowRange, uno::UNO_QUERY_THROW );
                bool bMerged = false;

                if ( ( rTableCell.getRowSpan() > 1 ) || ( rTableCell.getGridSpan() > 1 ) )
                {
                    MergeCells( xTable, nColumn, nRow, rTableCell.getGridSpan(), rTableCell.getRowSpan() );

                    if(rTableCell.getGridSpan() > 1)
                    {
                        nRemovedColumn = (rTableCell.getGridSpan() - 1);
                        // MergeCells removes columns. So our loop don't know that removed column and we are skip the handlingthat removed column(s).
                        nColIndex += nRemovedColumn;
                        bMerged = true; // it will adjust new column number after push current column's props with pushToXCell.
                    }
                }

                Reference< XCellRange > xCellRange( xTable, UNO_QUERY_THROW );
                rTableCell.pushToXCell( rFilterBase, pMasterTextListStyle, xCellRange->getCellByPosition( nColumn, nRow ), *this, rTableStyle,
                    nColumn, tableRow.getTableCells().size()-1, nRow, mvTableRows.size()-1 );
                Reference < XCell > xCell;

                if(nRemovedColumn)
                {
                    try
                    {
                        xCell = xCellRange->getCellByPosition( nColumn, nRow );
                    }
                    catch(Exception&) //Exception can come from TableModel::getCellByPosition when an column removed while merging columns. So adjust again here.
                    {
                        xCell = xCellRange->getCellByPosition( nColumn-nRemovedColumn, nRow );
                    }
                }
                else
                    xCell = xCellRange->getCellByPosition( nColumn, nRow );

                rTableCell.pushToXCell( rFilterBase,
                                        pMasterTextListStyle,
                                        xCell,
                                        *this,
                                        rTableStyle,
                                        nColumn,
                                        tableRow.getTableCells().size()-1,
                                        nRow,
                                        mvTableRows.size()-1 );
                if (bMerged)
                    nColumn += nRemovedColumn;
            }
            ++nColumn;
        }
diff --git a/sd/qa/unit/import-tests.cxx b/sd/qa/unit/import-tests.cxx
index 6308567..a085a1a 100644
--- a/sd/qa/unit/import-tests.cxx
+++ b/sd/qa/unit/import-tests.cxx
@@ -2518,7 +2518,7 @@ void SdImportTest::testTdf119015()
    uno::Reference<table::XTable> xTable(pTableObj->getTable());

    // Test that we actually have three cells: this threw css.lang.IndexOutOfBoundsException
    uno::Reference<text::XTextRange> xTextRange(xTable->getCellByPosition(2, 0),
    uno::Reference<text::XTextRange> xTextRange(xTable->getCellByPosition(1, 0),
                                                uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("A3"), xTextRange->getString());

diff --git a/svx/source/table/cellcursor.cxx b/svx/source/table/cellcursor.cxx
index bdd7156..d77dbb3 100644
--- a/svx/source/table/cellcursor.cxx
+++ b/svx/source/table/cellcursor.cxx
@@ -251,6 +251,7 @@ void SAL_CALL CellCursor::merge(  )
    try
    {
        mxTable->merge( aStart.mnCol, aStart.mnRow, aEnd.mnCol - aStart.mnCol + 1, aEnd.mnRow - aStart.mnRow + 1 );
        mxTable->optimize();
        mxTable->setModified(true);
    }
    catch( Exception& )
diff --git a/svx/source/table/tablecontroller.cxx b/svx/source/table/tablecontroller.cxx
index d79e094..57b3171 100644
--- a/svx/source/table/tablecontroller.cxx
+++ b/svx/source/table/tablecontroller.cxx
@@ -1839,7 +1839,6 @@ void SvxTableController::MergeRange( sal_Int32 nFirstCol, sal_Int32 nFirstRow, s
            }

            xRange->merge();
            mxTable->optimize();
            mbHasJustMerged = true;
            setSelectedCells( maCursorFirstPos, maCursorFirstPos );