sc/source/ui/vba/vbarange.cxx |   23 +++++++++++++++++------
 sc/source/ui/vba/vbarange.hxx |    3 +++
 2 files changed, 20 insertions(+), 6 deletions(-)

New commits:
commit e6294617b93a8d763d9355c5ce517428dea38e09
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Mon Jun 13 17:00:43 2022 +0200
Commit:     Eike Rathke <er...@redhat.com>
CommitDate: Tue Jun 14 01:19:47 2022 +0200

    Resolves: tdf#149531 Use initial sheet range for VBA Columns and Rows
    
    ... if the ScTableSheetObj's ScRangeList is empty. It might even
    be that was never intended to be used and worked only by accident
    in the past (pre 6.0), but it's somewhat unclear. It may even get
    in the way in case it exists and the (first) range was modified,
    e.g. shrunk by a Delete, as the resulting column or row object
    could be different from the initial sheet range.
    
    Change-Id: Ib9911df1b23802054a5bb0621bb7f5559ef3f39b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135732
    Reviewed-by: Eike Rathke <er...@redhat.com>
    Tested-by: Jenkins
    (cherry picked from commit 3ad12672e924f7aef394119f9fe5f0b06a900b9e)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135738

diff --git a/sc/source/ui/vba/vbarange.cxx b/sc/source/ui/vba/vbarange.cxx
index 5a6c1f2f18b7..44e565700f71 100644
--- a/sc/source/ui/vba/vbarange.cxx
+++ b/sc/source/ui/vba/vbarange.cxx
@@ -2374,17 +2374,29 @@ ScVbaRange::Activate()
 
 }
 
+ScRange ScVbaRange::obtainRangeEvenIfRangeListIsEmpty( const ScCellRangesBase* 
pUnoRangesBase ) const
+{
+    // XXX It may be that using the current range list was never correct, but
+    // always the initial sheet range would be instead, history is unclear.
+
+    const ScRangeList& rCellRanges = pUnoRangesBase->GetRangeList();
+    if (!rCellRanges.empty())
+        return rCellRanges.front();
+
+    table::CellRangeAddress aRA( lclGetRangeAddress( mxRange ));
+    return ScRange( aRA.StartColumn, aRA.StartRow, aRA.Sheet, aRA.EndColumn, 
aRA.EndRow, aRA.Sheet);
+}
+
 uno::Reference< excel::XRange >
 ScVbaRange::Rows(const uno::Any& aIndex )
 {
     if ( aIndex.hasValue() )
     {
-        sal_Int32 nValue = 0;
         ScCellRangesBase* pUnoRangesBase = getCellRangesBase();
-        ScRangeList aCellRanges = pUnoRangesBase->GetRangeList();
-        OUString sAddress;
+        ScRange aRange( obtainRangeEvenIfRangeListIsEmpty( pUnoRangesBase));
 
-        ScRange aRange = aCellRanges.front();
+        sal_Int32 nValue = 0;
+        OUString sAddress;
         if( aIndex >>= nValue )
         {
             aRange.aStart.SetRow( aRange.aStart.Row() + --nValue );
@@ -2420,9 +2432,8 @@ uno::Reference< excel::XRange >
 ScVbaRange::Columns(const uno::Any& aIndex )
 {
     ScCellRangesBase* pUnoRangesBase = getCellRangesBase();
-    ScRangeList aCellRanges = pUnoRangesBase->GetRangeList();
+    ScRange aRange( obtainRangeEvenIfRangeListIsEmpty( pUnoRangesBase));
 
-    ScRange aRange = aCellRanges.front();
     if ( aIndex.hasValue() )
     {
         OUString sAddress;
diff --git a/sc/source/ui/vba/vbarange.hxx b/sc/source/ui/vba/vbarange.hxx
index b32cc1ba3a5d..a893115d3a75 100644
--- a/sc/source/ui/vba/vbarange.hxx
+++ b/sc/source/ui/vba/vbarange.hxx
@@ -119,6 +119,9 @@ class ScVbaRange : public ScVbaRange_BASE
     /** Fires a Worksheet_Change event for this range or range list. */
     void fireChangeEvent();
 
+    /// @throws css::uno::RuntimeException
+    ScRange obtainRangeEvenIfRangeListIsEmpty( const ScCellRangesBase* 
pUnoRangesBase ) const;
+
 protected:
     virtual ScCellRangesBase* getCellRangesBase() override;
     /// @throws css::uno::RuntimeException

Reply via email to