Apache OpenOffice (AOO) Bugzilla – Issue 82420
TextSection objects not listed in order of their appearance
Last modified: 2017-05-20 11:20:08 UTC
The XTextSectionsSupplier interface's getTextSections() under certain circumstances returns sections in an incorrect order. Writer also displays the sections incorrectly (using Format->Sections) Use case to duplicate the bug: a) Create an empty writer document. (OOo 2.3 ) b) Insert a section, call it "Section1". c) After this section, add a few paragraph breaks by hitting enter. d) Insert a section, call it "Section2". e) Now go to the space with line breaks between "Section1" and "Section2", and insert another section called "Section3" there. f) Finally click Format->Sections. and it displays the order of the sections as : Section1 Section2 Section3 When it should be: Section1 Section3 Section2 The API also displays the order incorrectly: <code> Dim sections sections = thisComponent.getTextSections().ElementNames Dim i Dim outStr outStr = "" For i=LBound(sections) to UBound(sections) outStr = sections(i) + Chr(13)+Chr(10) + outStr Next i Msgbox outStr </code>
Currently the Format.Section dialog lists the sections in order of their insertion time. MRU->OS: would it generate high effort to implement a possibility to list the sectoins in order of their position?
The navigator shows the right order. For the API there is no need for a order because the sections can be nested but they appears only as a section of the document (not as a section of a section) at the same level in the elements list.
>The navigator shows the right order Yes, that is true, I think the navigator enumerates the content (via createEnumeration() ) while Format.Sections uses the XTextSectionsSupplier.... > For the API there is no need for a order because the sections can be nested but > they appears only as a section of the document (not as a section of a section) > at the same level in the elements list. the getChildSections() API of XTextSection returns a sequence of first level child sections in the wrong sequence (i.e. sequence in which they were added, and not the sequence they are in within the docment) .
By the fact, that *all* child sections are been returned, there can't be a wrong order. Use this code: REM ***** BASIC ***** Option Explicit Sub Main SectionTest End Sub Sub SectionTest Dim Sections As Object Dim Elements() As String Dim OutStr As String Dim i As Integer Dim j As Integer Sections = ThisComponent.getTextSections() Elements = Sections.ElementNames OutStr = "" For i = LBound( Elements() ) To UBound( Elements() ) OutStr = OutStr & Chr( 13 ) & Elements( i ) recursiveChilds( Sections.getByIndex( i ), 1, OutStr ) Next i Msgbox OutStr End Sub Sub recursiveChilds( Section As Object, iLevel As Integer, s As String ) Dim i As Integer Dim Childs() As Object Childs() = Section.getChildSections() For i = LBound( Childs() ) To UBound( Childs() ) s = s & Chr( 13 ) & String( 4 * iLevel, " " ) & "|-" & Childs( i ).getName() recursiveChilds( Childs( i ), iLevel + 1, s ) Next i End Sub
I am afraid you have misunderstood the problem. The issue is not whether you can iterate all sections (you can, as you have described). The issue is that the order of the sections returned by getTextSections() or getChildSections() does not reflect the order of sections as they appear in the document. Refer to the use case to duplicate the problem ast the start of the issue report.
I understand the problem, but you don't see the point (please use my code to test): ThisComponent.getTextSections() returs *all*(!) sections that are located in the document! Even all child sections. So how should be there any order? Also Section.getChildSections() returns *all*(!) child sections, not only the direct childs! Otherwise you must claim for that this methods returns *only* the first level sections and child sections respectively. But i think, this isn't a good idea ... Hope you see it clear now.
>ThisComponent.getTextSections() returs *all*(!) sections that are located in the >document! Even all child sections. So how should be there any order? If you have a nested section hierarchy like this in your document : root1 sec1.1 sec1.1.1 sec1.2 root2 sec2.1 sec2.1.1 sec2.2 it seems logical to me that the order would be the collapsed tree : root1 sec1.1 sec1.1.1 sec1.2 root2 sec2.1 sec2.1.1 sec2.2 >Also Section.getChildSections() returns *all*(!) child sections, not only the >direct childs! Hmm....this is'nt so, at least in OOo2.3.0 ... I always get only the immediate children (not grand children, or great-grand-children....) I was trying to build something like the navigator view for my application, for which getTextSections ().getByName() with getChildSections() is unusable because the order is the order of section creation....
Created attachment 49199 [details] Listing of nested sections
I can't believe that your OOo 2.3 produce a other output then my OOo 2.3. So please compare the output of your OOo with the output of my OOo stored in the frame of the sample file that i attached.
>Also Section.getChildSections() returns *all*(!) child sections, not only the >direct childs! Yes... I am getting the same output from your doument attachment. But clearly Section.getChildSections() is not returning *all* descendant sections (it returns only the immediate children).. otherwise you wouldnt be making a recursive call to getChildSections() would you? When i look at in the watch window getChildSections() clearly returns only the first level children. Honestly, i dont quite the thread of your argument, what does it have to do with the issue ?
Sorry, it was my mikstake. :-( With If IsNull( oSection.getParentSection() ) Then sString = sString & Chr( 13 ) & oSection.getName() ooomr_recursiveChilds( oSection, 1, sString ) End If you also get the right result at top level. So yes, my arguments don't affect to this issue furthermore. But please have a look at the following: If there are frames in the document and the frames contains sections - where they are located? Frames could be bound to a paragraph or a page. Where should they appear in your order? If there are sections in a document and there is also a frame with sections in it, in this manner, that the view of the sections overlap one another, the navigator shows the order of the view order, not the straight nested section order. (Look at the picture that i attach. The sections beginning with "(f?)" are located in the frame.) I don't know, how the API could reflect this behavior without scanning the view of the document. If you want to get this order and if you could ask the navigator about the sections, perhaps your problem could be solved. But i dont know if that is possible.
Created attachment 49251 [details] section order view in navigator, sections in a frame
I worked this around by using enumeration to get accurate section order, something like the following : Dim sect sect=thisComponent.getTextSections().getByName("root") Dim sectRange sectRange = sect.getAnchor() Dim sectText sectText = sectRange.getText() dim prevSection prevSection = "root" Dim sectEnum Dim orderStr orderStr = "" sectEnum = sectText.createEnumeration() while (sectEnum.hasMoreElements() ) dim nextElem nextElem = sectEnum.nextElement() if (Not isnull(nextElem.TextSection)) then if (not IsEmpty(nextElem.TextSection)) then dim tSect tSect = nextElem.TextSection.Name orderStr = orderStr + tSect + chr(13)+chr(10) end if end if wend msgbox orderStr this would work even for frames since enumeration relies on visible structure of the document. Its more tedious, and definitely slower but it works. You would just need to check for TextFrame objects in the main enumeration loop, and enumerate for sections within those.
Reset assigne to the default "issues@openoffice.apache.org".