[Issue] Basic IS operator

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
chris-barry
Posts: 13
Joined: Sun Sep 20, 2015 4:29 pm

[Issue] Basic IS operator

Post by chris-barry »

Why does the expression:

Code: Select all

ThisComponent.sheets(0) is ThisComponent.sheets(0)
evaluate to False?
Last edited by MrProgrammer on Sat May 31, 2025 4:55 pm, edited 1 time in total.
Reason: Added [Issue] tag
OpenOffice 4.1.1, Linux (Gentoo)
User avatar
floris v
Volunteer
Posts: 4567
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: The "is" operator

Post by floris v »

See https://help.libreoffice.org/25.2/en-US ... ystem=UNIX for how the is operator works. This is very abstract stuff. My guess is that when you call ThisComponent.sheets(0), a pointer and object is created referencing that sheet. In your ThisComponent.sheets(0) is ThisComponent.sheets(0) that's done twice, resulting in two objects referencing the same sheet. But because they're different objects, the is operator returns False.
You should probably use one variable dim'd as object and one as variant/pointer as in the article in the link.
LibreOffice 24.2.7.2 on Ubuntu Linux
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
chris-barry
Posts: 13
Joined: Sun Sep 20, 2015 4:29 pm

Re: The "is" operator

Post by chris-barry »

I had already seen that article.
The initial sentence, "Tests if two Basic variables refer to the same object instance.", seems to imply, at least to me, that two variables, each declared as "object" and assigned to the same actual object should have returned True.

I wanted to use this to find the value of the index for a sheet by looping through sheets until I got a match. Of course I can compare CodeName instead, the extra cost of two variable name dereferences and a string comparison is trivial to the point of irrelevance. I was curious, not so much about the implementation details as about the reasons for the original design decision, since in its current form it is so limited that I cannot think of a situation in which I would find it useful.
OpenOffice 4.1.1, Linux (Gentoo)
chris-barry
Posts: 13
Joined: Sun Sep 20, 2015 4:29 pm

Re: The "is" operator

Post by chris-barry »

Immediately after posting my previous message I noticed a StackOverflow telling me how to solve my original problem (RangeAddress.sheet), so my use-case was invalid. Nonetheless, I still think that this is a legitimate question.
OpenOffice 4.1.1, Linux (Gentoo)
User avatar
floris v
Volunteer
Posts: 4567
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: The "is" operator

Post by floris v »

You should probably use a variant variable to loop through all sheets instead of an object variable. The variant will directly point at the sheet, while what you call the object is basically a pointer to an object variable that points at the sheet. You can have several object variables pointing at the same sheet, but they will still be different objects at different locations in memory.
LibreOffice 24.2.7.2 on Ubuntu Linux
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
chris-barry
Posts: 13
Joined: Sun Sep 20, 2015 4:29 pm

Re: The "is" operator

Post by chris-barry »

Is this what you mean?

Code: Select all

sub test_2()
	dim sht_1 as variant
	dim sht_2 as variant
	
	sht_1 = ThisComponent.sheets.getByIndex(1)
	sht_2 = ThisComponent.sheets.getByIndex(1)
	if sht_1 is sht_2 then
		MsgBox "True"
	else
		MsgBox "Not True " & sht_1.name & " " & sht_2.name
	end if
end sub
When I run this I get "Not True" followed by the same sheet name twice.
OpenOffice 4.1.1, Linux (Gentoo)
User avatar
floris v
Volunteer
Posts: 4567
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: The "is" operator

Post by floris v »

You should contact the developers about this, they don't visit this forum. They have a mailing list where you can subscribe: https://openoffice.apache.org/mailing-l ... ist-public, then you can ask your question there.
LibreOffice 24.2.7.2 on Ubuntu Linux
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
User avatar
Lupp
Volunteer
Posts: 3693
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: The "is" operator

Post by Lupp »

I never used the "IS operator" in decades and didn't even know that it existed. As far as I can see also Andrew Pitonyak doesn't mention it.
My way to test UNO objects for equality was the (Basic) Function EqualUNOObjects().
However I got the same problem. See my bug report https://bugs.documentfoundation.org/sho ... ?id=125421 .
Unfortunately I didn't understand the attempted answers.

But you find there also a link to an older thread in this forum where @Villeroy points out a solution.
viewtopic.php?f=20&t=98435

Alas! For SheetCell objects and SheetCellRange objects you have the same problem, and the solutions (workarounds using addresses) are very unhandy.

BTW: If you mention statements found in a different community, always give a link, please.
On Windows 10: LibreOffice 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
karolus
Volunteer
Posts: 1226
Joined: Sat Jul 02, 2011 9:47 am

Re: The "is" operator

Post by karolus »

Sheet-NAMES are unique, so why not simply compare

Code: Select all

…
if sht_1.Name = sh_2.Name then
…
end if
…
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 24.8… flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: The "is" operator

Post by JeJe »

The bug occurs at the sheets level - so will also occur with anything lower down such as an individual sheet.

Its trivial, so long as you're aware of it, as there are easy workarounds.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked