Bug 167420 - Different result of function ThisComponent.Sheets.moveByName(name, index)
Summary: Different result of function ThisComponent.Sheets.moveByName(name, index)
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
25.2.3.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: bibisected, bisected
Depends on:
Blocks:
 
Reported: 2025-07-08 10:03 UTC by Zdenek Borecky
Modified: 2025-07-08 11:32 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
sample file (9.70 KB, application/vnd.oasis.opendocument.spreadsheet)
2025-07-08 10:43 UTC, Xisco Faulí
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zdenek Borecky 2025-07-08 10:03:33 UTC
Description:
I have macros writted in versions 7 and older.
In connection with ticket  167178  I discover different behaviour of function ThisComponent.Sheets.moveByName(name, index)
in version 25.2.3.2 and 25.2.6.
I do't know if it is bug or new property...

Steps to Reproduce:
In table with sheets
Sheet0 Sheet1 Sheet3 Sheet4 lastsheet
run this code which insert new sheet a next move it to right order:

Sub Main
oLast = ThisComponent.Sheets.getByName("lastsheet")
ThisComponent.Sheets.insertNewByName("Sheet2", 0)
oSheet = ThisComponent.Sheets.getByName("Sheet2")
for i = oLast.RangeAddress.sheet - 1 to 1 step -1
  j = strComp(ThisComponent.Sheets.getByIndex(i).getName(),"Sheet2")
  if j = -1 then
    ThisComponent.Sheets.moveByName("Sheet2", i + 1)
    exit for
  end if
next i
End Sub

Actual Results:
Order of sheets in versions 25.2. is
Sheet0 Sheet1 Sheet3 Sheet2 Sheet4 lastsheet

Expected Results:
Order of sheets in versions 7.5.0.3. and olders, is:
Sheet0 Sheet1 Sheet2 Sheet3 Sheet4 lastsheet


Reproducible: Always


User Profile Reset: No

Additional Info:
I only have official versions 25.2. and later for testing and version 7.5.0.3 in which I once wrote the code.
I can't find out exactly from which version the behavior of the moveByName() function changed.
Thanks
Comment 1 Xisco Faulí 2025-07-08 10:43:41 UTC
Created attachment 201700 [details]
sample file
Comment 2 Xisco Faulí 2025-07-08 10:49:36 UTC
Regression introduced by:

commit e6d4af2a03f22de1493e00e497e081240bf8a5c4	[log]
author	Alin Andrei Abahnencei <alinandrei2004@gmail.com>	Thu Dec 12 16:03:22 2024 +0200
committer	Mike Kaganski <mike.kaganski@collabora.com>	Wed Dec 18 07:08:24 2024 +0100
tree c1aabac8d6ac0b697a5e27564885083cd12f5046
parent 943c079def4f55463581e1639b08c11f04863a04 [diff]

tdf#163319 Do not decrement destination position when moving a tab
Comment 3 Xisco Faulí 2025-07-08 10:50:26 UTC
@Mike, I thought you would be interested in this issue
Comment 4 Mike Kaganski 2025-07-08 10:56:17 UTC
(In reply to Xisco Faulí from comment #2)
> Regression introduced by:

Xisco: please don't set NEW and Regression, when the change has intentionally changed it in this way.

Zdenek: thank you for filing this, as we discussed in bug 167178. Now that the change is identified, it is clear that no, this is not a bug, but a bugfix, where previously it worked incorrectly.
Comment 5 Mike Kaganski 2025-07-08 11:00:14 UTC
(In reply to Zdenek Borecky from comment #0)

To clarify: previously, you had to do that "+ 1":

> Sub Main
> ...
>     ThisComponent.Sheets.moveByName("Sheet2", i + 1)

And that was exactly wrong. To move to position i, you had to make it i+i; and also you had to consider, if i is greater or smaller than the current index of the sheet you are moving. That is now fixed. When you want to move sheet to position i, you just use that i, and may be sure that

 ThisComponent.Sheets.moveByName("Sheet2", i)
 MsgBox ThisComponent.Sheets.getByName("Sheet2").RangeAddress.sheet

would show the value of i.
Comment 6 Zdenek Borecky 2025-07-08 11:32:11 UTC
Thanks you very much for quick reaction and explanation. I will fix my code.