Bug 159973 - queryPrecedents returns cells as if in same sheet
Summary: queryPrecedents returns cells as if in same sheet
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.6.4.1 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-02-29 20:11 UTC by Patrick Traill
Modified: 2024-03-01 12:40 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments
Sample file for testing (14.51 KB, application/vnd.oasis.opendocument.spreadsheet)
2024-02-29 22:45 UTC, Rafael Lima
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick Traill 2024-02-29 20:11:19 UTC
When I use queryPrecedents to get the precedents of a formula, the ScCellRangesObj returned behaves as though all precedents were in the same sheet as the cell itself.

It also seems to me that enumerating the precedent cells only returns the first one, but I am not 100% sure that I am doing that right; that would probably be different bug anyway.
(I tried enumerating them to see if that functionality was any better, but inspecting the object in the debugger suggests that it would not.)

I apologise if this has already been reported; searching for "queryPrecedents" seemed to cause the site to hang!

I am running this version:
Version: 7.6.4.1 (X86_64) / LibreOffice Community
Build ID: e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1
CPU threads: 8; OS: Linux 5.3; UI render: default; VCL: kf5 (cairo+xcb)
Locale: en-GB (en_GB.UTF-8); UI: en-GB
Calc: threaded

The following code displays the message:
Formula: =A2+A6+Bug_Prec_Loc_2.A1+A4
Precedents: AbsoluteName = $Bug_Prec_Loc_1.$A$1:$A$2,$Bug_Prec_Loc_1.$A$4,$Bug_Prec_Loc_1.$A$6
Precedent cell names:  $Bug_Prec_Loc_1.$A$1
Enumerated:  $Bug_Prec_Loc_1.$A$1

I think it should display something like:
Formula: =A2+A6+Bug_Prec_Loc_2.A1+A4
Precedents: AbsoluteName = $Bug_Prec_Loc_1.$A$1,$Bug_Prec_Loc_1.$A$4,$Bug_Prec_Loc_1.$A$6,$Bug_Prec_Loc_2.$A$2
Precedent cell names:  $Bug_Prec_Loc_1.$A$1 $Bug_Prec_Loc_1.$A$4 $Bug_Prec_Loc_1.$A$6 $Bug_Prec_Loc_2.$A$2
Enumerated:  $Bug_Prec_Loc_1.$A$1 $Bug_Prec_Loc_1.$A$4 $Bug_Prec_Loc_1.$A$6 $Bug_Prec_Loc_2.$A$2


Option Explicit		' Require variables to be defined

Option Compatible	' So ParamArray is supported

' Demonstrate a bug in obtaining precedents of a formula
'	It appears that the precedents for a formula obtained with queryPrecedents projected into the sheet of the cell containing the formula
Sub Bug_Precedents_Local ()
	Dim	LF		as	String	:	LF		= Chr (10)
	Dim	z_1_S	as	String	:	z_1_S	= "Bug_Prec_Loc_1"
	Dim	z_2_S	as	String	:	z_2_S	= "Bug_Prec_Loc_2"

	On Local Error Resume Next
	ThisComponent.Sheets.insertNewByName (z_1_S, 0)
	ThisComponent.Sheets.insertNewByName (z_2_S, 1)
	On Local Error Go To 0
	
	Dim	z_1	as	Object		:	z_1	= ThisComponent.Sheets.getByName (z_1_S)
	Dim	z_2	as	Object		:	z_2	= ThisComponent.Sheets.getByName (z_2_S)
	
	Dim	z_1_A1	as	Object	:	z_1_A1	= z_1.getCellByPosition (0, 0)
	
	z_1_A1.setFormula ("=A2+A6+'" + z_2.Name + "'.A1+A4")
	
	Dim	z_Precedents	as	Object	:	z_Precedents	= z_1_A1.queryPrecedents (False)
	Dim	z_Cells			as	Object	:	z_Cells			= z_Precedents.getCells ()
	
	Dim	z_Names			as	String	:	z_Names			= ""
	Dim z_Cell			as	Object	:	For Each z_Cell in z_Cells
		z_Names	= z_Names + " " + z_Cell.AbsoluteName
	Next

	z_Names	= z_Names + LF + "Enumerated: "

	Dim	z_Enumeration	as	Object	:	z_Enumeration	= z_Cells.createEnumeration ()
	While z_Enumeration.hasMoreElements ()
		z_Cell	= z_Enumeration.nextElement ()
		z_Names	= z_Names + " " + z_Cell.AbsoluteName
	Wend

	MsgBox ("Formula: " + z_1_A1.Formula + LF + "Precedents: AbsoluteName = " + z_Precedents.AbsoluteName + LF + "Precedent cell names: " + z_Names)
End Sub
Comment 1 Rafael Lima 2024-02-29 22:45:39 UTC
Created attachment 192877 [details]
Sample file for testing

I can confirm the weird behavior with queryPrecedents when the range is in a different sheet.

I created a sample file with a macro to make it easier to test.

Steps to reproduce
1) Open the test file
2) Select cell B7 in Sheet1 and click "Show Precedents"
3) Notice it will show precedents correctly, since they're in the same sheet
4) Click cell A15 from Sheet1 (which contains a formula that uses a range from Sheet2
5) Click the button "Show Precedents"
6) Notice that the returned range for the second parameter is not correct

Tested with

Version: 7.6.5.2 (X86_64) / LibreOffice Community
Build ID: 60(Build:2)
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.5-0ubuntu0.23.10.1
Calc: threaded

Also repro with

Version: 24.8.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: 52806e4c1e241d12419373cb0ed013356e12c4b8
CPU threads: 16; OS: Linux 6.5; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Calc: CL threaded
Comment 2 Rafael Lima 2024-02-29 22:47:29 UTC
(In reply to Patrick Traill from comment #0)
> It also seems to me that enumerating the precedent cells only returns the
> first one, but I am not 100% sure that I am doing that right; that would
> probably be different bug anyway.

You're enumerating correctly, but if the two ranges are adjacent to one another, it will return both together as a single range.

In my sample file, remove the gap column in Test 1 and run it... only a single range will be returned. Weird anyways, I would expect a different result.
Comment 3 Patrick Traill 2024-03-01 11:41:06 UTC
(In reply to Rafael Lima from comment #2)
> (In reply to Patrick Traill from comment #0)
> > It also seems to me that enumerating the precedent cells only returns the
> > first one, but I am not 100% sure that I am doing that right; that would
> > probably be different bug anyway.
> 
> You're enumerating correctly, but if the two ranges are adjacent to one
> another, it will return both together as a single range.
> 
> In my sample file, remove the gap column in Test 1 and run it... only a
> single range will be returned. Weird anyways, I would expect a different
> result.

I am not sure I understand you. Do you agree that enumerating the result of getCells should return all the cells, not just one per range?
https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XSheetCellRanges.html#getCells says it “creates an enumeration of all used cells”.
Comment 4 Patrick Traill 2024-03-01 12:40:39 UTC
It may be worth mentioning that the menu function Tools – Detective – Trace Precedents does show (with a diamond) that there are one or more precedents in a different sheet, presumably meaning that it does not use queryPrecedents (unless that has undocumented capabilities).
As reported in bug #63087, is is, however, not possible to use the diamond to access the other cells.