don't allocate unnecessary columns when inserting a row

Change-Id: I616ef20dc1295ce17c4877ff367815bb6a90b7a1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134547
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
diff --git a/sc/inc/column.hxx b/sc/inc/column.hxx
index 290334f..e4b7eb1 100644
--- a/sc/inc/column.hxx
+++ b/sc/inc/column.hxx
@@ -171,6 +171,7 @@ public:
    void        ChangeSelectionIndent( bool bIncrement, const ScMarkData& rMark, SCCOL nCol );

    bool        TestInsertRow( SCSIZE nSize ) const;
    void        InsertRow( SCROW nStartRow, SCSIZE nSize );
};

// Use protected inheritance to prevent publishing some internal ScColumnData
@@ -1041,4 +1042,9 @@ inline bool ScColumnData::TestInsertRow( SCSIZE nSize ) const
    return pAttrArray->TestInsertRow( nSize );
}

inline void ScColumnData::InsertRow( SCROW nStartRow, SCSIZE nSize )
{
    pAttrArray->InsertRow( nStartRow, nSize );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/column.cxx b/sc/source/core/data/column.cxx
index 2353a47..57451b1 100644
--- a/sc/source/core/data/column.cxx
+++ b/sc/source/core/data/column.cxx
@@ -2391,13 +2391,13 @@ bool ScColumn::UpdateReferenceOnCopy( sc::RefUpdateContext& rCxt, ScDocument* pU

bool ScColumn::UpdateReference( sc::RefUpdateContext& rCxt, ScDocument* pUndoDoc )
{
    if (rCxt.meMode == URM_COPY)
        return UpdateReferenceOnCopy(rCxt, pUndoDoc);

    if (IsEmptyData() || GetDoc().IsClipOrUndo())
        // Cells in this column are all empty, or clip or undo doc. No update needed.
        return false;

    if (rCxt.meMode == URM_COPY)
        return UpdateReferenceOnCopy(rCxt, pUndoDoc);

    std::vector<SCROW> aBounds;

    bool bThisColShifted = (rCxt.maRange.aStart.Tab() <= nTab && nTab <= rCxt.maRange.aEnd.Tab() &&
diff --git a/sc/source/core/data/table1.cxx b/sc/source/core/data/table1.cxx
index 619947b..0b75dce 100644
--- a/sc/source/core/data/table1.cxx
+++ b/sc/source/core/data/table1.cxx
@@ -1819,19 +1819,6 @@ void ScTable::UpdateReference(
    sc::RefUpdateContext& rCxt, ScDocument* pUndoDoc, bool bIncludeDraw, bool bUpdateNoteCaptionPos )
{
    bool bUpdated = false;
    SCCOL i;
    SCCOL iMax;
    if (rCxt.meMode == URM_COPY )
    {
        i = rCxt.maRange.aStart.Col();
        iMax = rCxt.maRange.aEnd.Col();
    }
    else
    {
        i = 0;
        iMax = rDocument.MaxCol();
    }

    UpdateRefMode eUpdateRefMode = rCxt.meMode;
    SCCOL nDx = rCxt.mnColDelta;
    SCROW nDy = rCxt.mnRowDelta;
@@ -1844,8 +1831,16 @@ void ScTable::UpdateReference(
    if (mpRangeName)
        mpRangeName->UpdateReference(rCxt, nTab);

    for ( ; i<=iMax; i++)
        bUpdated |= CreateColumnIfNotExists(i).UpdateReference(rCxt, pUndoDoc);
    if (rCxt.meMode == URM_COPY )
    {
        for( SCCOL col : GetAllocatedColumnsRange( rCxt.maRange.aStart.Col(), rCxt.maRange.aEnd.Col()))
            bUpdated |= aCol[col].UpdateReference(rCxt, pUndoDoc);
    }
    else
    {
        for( SCCOL col : GetAllocatedColumnsRange( 0, rDocument.MaxCol()))
            bUpdated |= aCol[col].UpdateReference(rCxt, pUndoDoc);
    }

    if ( bIncludeDraw )
        UpdateDrawRef( eUpdateRefMode, nCol1, nRow1, nTab1, nCol2, nRow2, nTab2, nDx, nDy, nDz, bUpdateNoteCaptionPos );
diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx
index 2b67067..4e1d1f4 100644
--- a/sc/source/core/data/table2.cxx
+++ b/sc/source/core/data/table2.cxx
@@ -186,8 +186,9 @@ void ScTable::InsertRow( SCCOL nStartCol, SCCOL nEndCol, SCROW nStartRow, SCSIZE
        }
    }

    for (SCCOL j=nStartCol; j<=nEndCol; j++)
    for (SCCOL j : GetAllocatedColumnsRange(nStartCol, nEndCol))
        aCol[j].InsertRow( nStartRow, nSize );
    aDefaultColData.InsertRow( nStartRow, nSize );

    mpCondFormatList->InsertRow(nTab, nStartCol, nEndCol, nStartRow, nSize);