sc/qa/unit/data/xls/tdf168589.xls       |binary
 sc/qa/unit/subsequent_filters_test5.cxx |    9 +++++++++
 sc/source/core/tool/compiler.cxx        |   14 +++++++++-----
 3 files changed, 18 insertions(+), 5 deletions(-)

New commits:
commit 83f80f46bfec8e023727db2d87c22876a2ddf6bd
Author:     Balazs Varga <[email protected]>
AuthorDate: Tue Sep 30 19:05:23 2025 +0200
Commit:     Xisco Fauli <[email protected]>
CommitDate: Thu Oct 2 10:08:07 2025 +0200

    tdf#168589 - sc fix xls import with subtotal function
    
    Check if the trimmed data range of Subtotal double references
    is valid otherwise do not trim.
    
    regression after: b89047a0f0100fb30121084cf42815aa792c1f88
    (tdf#164843 - sc optimize "SubTotal" function's reference ranges)
    
    Change-Id: I5f1fa4f67a685deb4a391e19e3acd9849b03c1b4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191691
    Tested-by: Jenkins
    Reviewed-by: Balazs Varga <[email protected]>
    Signed-off-by: Xisco Fauli <[email protected]>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191760

diff --git a/sc/qa/unit/data/xls/tdf168589.xls 
b/sc/qa/unit/data/xls/tdf168589.xls
new file mode 100644
index 000000000000..103b35f64f6c
Binary files /dev/null and b/sc/qa/unit/data/xls/tdf168589.xls differ
diff --git a/sc/qa/unit/subsequent_filters_test5.cxx 
b/sc/qa/unit/subsequent_filters_test5.cxx
index 46d16c34648a..1e410381494d 100644
--- a/sc/qa/unit/subsequent_filters_test5.cxx
+++ b/sc/qa/unit/subsequent_filters_test5.cxx
@@ -221,6 +221,15 @@ CPPUNIT_TEST_FIXTURE(ScFiltersTest5, testTdf94627)
     ASSERT_DOUBLES_EQUAL(2, fVal);
 }
 
+CPPUNIT_TEST_FIXTURE(ScFiltersTest5, testTdf168589)
+{
+    // Open xls document with Subtotal function
+    createScDoc("xls/tdf168589.xls");
+    ScDocument* pDoc = getScDoc();
+    CPPUNIT_ASSERT(pDoc);
+    ASSERT_DOUBLES_EQUAL(6.0, pDoc->GetValue(4, 5, 0));
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index b31ff276c4bd..86c5cc4e3117 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -6880,17 +6880,21 @@ void ScCompiler::AnnotateTrimOnDoubleRefs()
             if (pTok->GetType() == svDoubleRef)
             {
                 ScComplexRefData* pRefData = pTok->GetDoubleRef();
-                // no need to set pRefData->SetTrimToData(true); because we 
already trim here
+                // do no set pRefData->SetTrimToData(true); because we need to 
trim here if possible
                 ScRange rRange = pRefData->toAbs(rDoc, aPos);
                 SCCOL nTempStartCol = rRange.aStart.Col();
                 SCROW nTempStartRow = rRange.aStart.Row();
                 SCCOL nTempEndCol = rRange.aEnd.Col();
                 SCROW nTempEndRow = rRange.aEnd.Row();
                 rDoc.ShrinkToDataArea(rRange.aStart.Tab(), nTempStartCol, 
nTempStartRow, nTempEndCol, nTempEndRow);
-                rRange.aStart.Set(nTempStartCol, nTempStartRow, 
rRange.aStart.Tab());
-                rRange.aEnd.Set(nTempEndCol, nTempEndRow, rRange.aEnd.Tab());
-                rRange.PutInOrder();
-                pRefData->SetRange(rDoc.GetSheetLimits(), rRange, aPos);
+                // check if range is still valid
+                if (nTempStartRow <= nTempEndRow && nTempStartCol <= 
nTempEndCol)
+                {
+                    rRange.aStart.Set(nTempStartCol, nTempStartRow, 
rRange.aStart.Tab());
+                    rRange.aEnd.Set(nTempEndCol, nTempEndRow, 
rRange.aEnd.Tab());
+                    if (rRange.IsValid())
+                        pRefData->SetRange(rDoc.GetSheetLimits(), rRange, 
aPos);
+                }
             }
             --ppTok;
         }

Reply via email to