Bug 47479 - LibO Calc Macro .getCellRangeByName with named range
Summary: LibO Calc Macro .getCellRangeByName with named range
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
3.5.1 release
Hardware: x86-64 (AMD64) Windows (All)
: high major
Assignee: Mike Kaganski
URL:
Whiteboard: target:25.8.0 target:25.2.4
Keywords:
Depends on:
Blocks: Macro-UNOAPI
  Show dependency treegraph
 
Reported: 2012-03-18 12:42 UTC by Kossem
Modified: 2025-05-03 15:04 UTC (History)
7 users (show)

See Also:
Crash report or crash signature:


Attachments
BUG after calling RemoveRange(UP). (13.12 KB, application/vnd.oasis.opendocument.spreadsheet)
2025-05-03 14:59 UTC, Oleg
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kossem 2012-03-18 12:42:23 UTC
I got an error message after updating Calc to LibO 3.5(.0 and still now with .1.2) when running a previously flawless macro.
The offending (?) statement is  .getCellRangeByName("namedcell")  where namedcell is a global named cell range (of one cell, in this instance).

Response from system:

    Basic runtime error.
    An exception occurred
    Type: com.sun.star.uno.RuntimeException
    Message:.

The (empty) returned message is quite cryptic, without any error number to start searching.

- Same error when tried with a multiple cells named range, and with different (plain and short) names for the range.
- It works well when the named cell range is replaced by its coordinates value (e.g. "K12").
- Works also ok with getCellByPosition (but I want it to work correctly, as it did before, with a named cell range).

Thanks in advance for a fast and complete solution, and/or explanation.
Cheers.
Comment 1 Jeremy 2013-04-08 11:31:34 UTC
I am getting the same error on 4.0.2.2 and previous release.  I only get the error if I have deleted a range.
The following code will recreate the problem.  Sheet1 has a single range defined as "MYRANGE" and there is some data in the following cells.

Sub Main
Dim oSheet as Object
DIM cBlock as New com.sun.star.table.CellRangeAddress
dim myRow as integer

oSheet = ThisComponent.sheets("Sheet1")
myRow=oSheet.getCellRangeByName("MYRANGE").RAngeAddress.EndRow


cBlock.Sheet=0
cBlock.StartColumn=0
cBlock.EndColumn=4
cBlock.StartRow=myRow+1
cBlock.EndRow=myRow+2
osheet.removeRange(cBlock, com.sun.star.sheet.CellDeleteMode.UP)

myRow=oSheet.getCellRangeByName("MYRANGE").RAngeAddress.EndRow ' << fails here

End Sub
Comment 2 Jeremy 2013-05-16 09:46:20 UTC
Still a problem in 4.0.3.3
Comment 3 QA Administrators 2015-04-19 03:20:48 UTC Comment hidden (obsolete)
Comment 4 Buovjaga 2015-06-15 12:45:09 UTC
Repro per comment 1.

Win 7 Pro 64-bit Version: 5.1.0.0.alpha1+
Build ID: 01a189abcd9a4ca472a74b3b2c000c9338fc2c91
TinderBox: Win-x86@39, Branch:master, Time: 2015-06-14_07:46:28
Locale: fi-FI (fi_FI)
Comment 5 Xisco Faulí 2017-06-12 10:54:09 UTC
Changing version back to the earliest version affected.
Comment 6 QA Administrators 2018-06-17 02:43:42 UTC Comment hidden (obsolete)
Comment 7 Buovjaga 2019-08-11 10:23:52 UTC
(In reply to Jeremy from comment #1)
> I am getting the same error on 4.0.2.2 and previous release.  I only get the
> error if I have deleted a range.
> The following code will recreate the problem.  Sheet1 has a single range
> defined as "MYRANGE" and there is some data in the following cells.
> 
> Sub Main
> Dim oSheet as Object
> DIM cBlock as New com.sun.star.table.CellRangeAddress
> dim myRow as integer
> 
> oSheet = ThisComponent.sheets("Sheet1")
> myRow=oSheet.getCellRangeByName("MYRANGE").RAngeAddress.EndRow
> 
> 
> cBlock.Sheet=0
> cBlock.StartColumn=0
> cBlock.EndColumn=4
> cBlock.StartRow=myRow+1
> cBlock.EndRow=myRow+2
> osheet.removeRange(cBlock, com.sun.star.sheet.CellDeleteMode.UP)
> 
> myRow=oSheet.getCellRangeByName("MYRANGE").RAngeAddress.EndRow ' << fails
> here
> 
> End Sub

I put numbers in A1-8 and defined A1-A4 as MYRANGE. I ran the macro and it did not fail. The macro removes two rows after the range.

Is there something I am missing?

Arch Linux 64-bit
Version: 6.4.0.0.alpha0+
Build ID: 37fc9f51a8de11d40632e8cda17ccf1fa4b1f503
CPU threads: 8; OS: Linux 5.2; UI render: default; VCL: gtk3; 
Locale: fi-FI (fi_FI.UTF-8); UI-Language: en-US
Calc: threaded
Built on 6 August 2019
Comment 8 QA Administrators 2021-09-18 03:26:54 UTC Comment hidden (obsolete)
Comment 9 Mike Kaganski 2021-12-19 12:30:25 UTC
Eike: wasn't there a change that introduced sheet-local ranges [1]? Could that change be related to this?

[1] https://wiki.documentfoundation.org/ReleaseNotes/3.4#Calc
Comment 10 QA Administrators 2023-12-20 03:12:55 UTC Comment hidden (obsolete)
Comment 11 Rafael Lima 2024-01-24 19:59:13 UTC
As of LO 7.6 the macro in Comment #1 works fine if the document has a named range "MYRANGE". The macro will fail if the named range does not exist.

So I'm closing this as WFM. Fell free to reopen if needed.

Tested with

Version: 7.6.4.1 (X86_64) / LibreOffice Community
Build ID: 60(Build:1)
CPU threads: 16; OS: Linux 6.5; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Ubuntu package version: 4:7.6.4-0ubuntu0.23.10.1
Calc: threaded
Comment 12 Oleg 2025-05-01 20:24:58 UTC
Below is code that explains the problem that occurs when deleting cells with the RemoveRange(RangeAddress, com.sun.star.sheet.CellDeleteMode.UP) method and then accessing the cells with the GetCellRangeByName() or GetCellRangeByPosition() or GetCellByPosition() method.

Response from system:
Basic runtime error.
An exception occurred
Type: com.sun.star.uno.RuntimeException
Message:.

Dim oSheet1 As Object
Dim oRange As Object
Dim oRange2 As Object

oSheet1 = ThisComponent.Sheets(0)

' Example 1:
oRange = oSheet1.GetCellRangeByName("A15")
oSheet1.RemoveRange(oRange.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)
' No BUG, ​​because the deletion is performed in column A.
oRange2 = oSheet1.GetCellRangeByName("A20")

' Example 2:
oRange = oSheet1.GetCellRangeByName("X1")
oSheet1.RemoveRange(oRange.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)
' No BUG, ​​because the deletion is performed in row 1.
oRange2 = oSheet1.GetCellRangeByName("A20")

' Example 3:
oRange = oSheet1.GetCellRangeByName("B21")
oSheet1.RemoveRange(oRange.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)
' No BUG, ​​because the range requested by the next GetCellRangeByName() command is located above the row in which the deletion occurred (20 < 21).
oRange2 = oSheet1.GetCellRangeByName("A20")

' Example 4:
oRange = oSheet1.GetCellRangeByName("B20")
oSheet1.RemoveRange(oRange.RangeAddress, com.sun.star.sheet.CellDeleteMode.UP)
' There is a BUG, ​​because:
' 1) deletion is not in column A;
' 2) deletion is not in row 1;
' 3) range requested by the next GetCellRangeByName() command is located in the same row in which the deletion occurred, or below it (20 = 20, 65 > 20).
oRange2 = oSheet1.GetCellRangeByName("A20")
' OR
oRange2 = oSheet1.GetCellRangeByName("F65")


Similar behavior with GetCellRangeByName("NamedRange").


Version: 25.2.2.2 (X86_64) / LibreOffice Community
Build ID: 7370d4be9e3cf6031a51beef54ff3bda878e3fac
CPU threads: 8; OS: Windows 10 X86_64 (10.0 build 19045); UI render: Skia/Vulkan; VCL: win
Locale: ru-RU (ru_RU); UI: ru-RU
Calc: CL threaded
Comment 13 Mike Kaganski 2025-05-02 06:27:19 UTC
(In reply to Oleg from comment #12)

Thanks Oleg, very informative. However, it needs clarification, if what you report is the same as this "LibO Calc Macro .getCellRangeByName with named range" (with emphasis on "named range"), or is it a separate issue. Your steps only mention "Similar behavior with GetCellRangeByName("NamedRange")", without a sample document with a pre-made range plus a code to test.
Comment 14 Mike Kaganski 2025-05-02 12:42:50 UTC
Let me address comment 12 in https://gerrit.libreoffice.org/c/core/+/184889.
Comment 15 Mike Kaganski 2025-05-02 14:39:21 UTC
Let me assume it fixed.
Comment 16 Commit Notification 2025-05-02 14:39:27 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a0403b6eb25a99f6225e0f77f4b14d78f4045c9e

tdf#47479: Do not modify range of sheet / column / row in notifications

It will be available in 25.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 17 Commit Notification 2025-05-02 18:20:06 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "libreoffice-25-2":

https://git.libreoffice.org/core/commit/1f3b872e8c4901ff36e07bb973e008f6849af21c

tdf#47479: Do not modify range of sheet / column / row in notifications

It will be available in 25.2.4.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 18 Oleg 2025-05-03 14:59:40 UTC
Created attachment 200643 [details]
BUG after calling RemoveRange(UP).
Comment 19 Oleg 2025-05-03 15:04:49 UTC
Comment on attachment 200643 [details]
BUG after calling RemoveRange(UP).

The error occurs regardless of the method of getting the cell or (un)named range. If the target of the get is an (un)named range, the error also occurs if the removed cell was inside this range.