List of Writer Textsections in document order

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

List of Writer Textsections in document order

Post by JeJe »

Get a list of Writer textsections in the order of where they are in the document *

(as they appear in the Navigator, not what is retrieved by the api textsections list which isn't in document order)


Uses Lupp's method of getting the position from the viewcursor (as applied to annotation anchors here:
viewtopic.php?t=111343

Limited tested on OO only. In the past I have had issues with Writer not always updating the viewcursor position - so may occur here.

Code: Select all


	REM  *****  BASIC  *****
Option Explicit

Sub GetTextSectionInDocOrder
	dim cont,vc,ts, i as long,y as long,ub as long,j as long,k as long, found as boolean

	ts = thiscomponent.textsections
	ub=ts.count -1
	
	cont = thiscomponent.currentcontroller
	vc= cont.viewcursor

	dim ps(ub) as long,nms(ub) as string


	for i = 0 to ub
		cont.select ts(i).getanchor.getstart
		y = vc.position.y

			found = false			
		if i = 0 then
			ps(0) =y
			nms(0) = ts(0).getname	
		else
			for j= i -1 to 0 step -1
				k = j+1
				if  ps(j)>y  then
					ps(k) = ps(j)
					nms(k) =nms(j)
				else
					nms(k) = ts(i).getname
					ps(k)=y
					found = true
					exit for
				end if
			next
		
		
		if not found then
				ps(0) = y
				nms(0) = ts(i).getname
		end if
		
		end if
	next

	mri nms
	mri ps
'	mri thiscomponent.textsections.elementnames
End Sub


Edit: I've assumed a unique y position and haven't handled for sections in text frames with the same y positions.

* Edit: 14/3/24
CORRECTED ERROR IN THE SORTING ROUTINE
Last edited by JeJe on Fri Mar 15, 2024 1:04 am, edited 2 times in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: List of Writer Textsections in document order

Post by JeJe »

This is to order in both dimensions but that diverges from the list in the navigator in OO which doesn't do so. (again very limited testing)*

Code: Select all


	REM  *****  BASIC  *****
Option Explicit

Sub GetTextSectionInDocOrderIncHorizontal
	dim cont,vc,ts, i as long,y as long,ub as long,j as long,k as long, x as long, n as long,o as long
	dim foundx as boolean, foundy as boolean

	ts = thiscomponent.textsections
	ub=ts.count -1

	cont = thiscomponent.currentcontroller
	vc= cont.viewcursor

	dim ps(ub) as long,nms(ub) as string, psx(ub) as long


	for i = 0 to ub
		cont.select ts(i).getanchor.getstart
		y = vc.position.y
		x = vc.position.x

		if i = 0 then
			ps(0) =y
			psx(0)=x
			nms(0) = ts(0).getname

		else
			foundy = false

			for j= i -1 to 0 step -1
				k = j+1
				if  ps(j)>y  then
					ps(k) = ps(j)
					psx(k) = psx(j)
					nms(k) =nms(j)

				Elseif  ps(j)=y  then
					foundx = false

					for n= j to 0 step -1
						o = n+1
						if  ps(n)=y and psx(n)>x  then
							ps(o) = ps(n)
							nms(o) =nms(n)
							psx(o) = psx(n)
						else
							nms(o) = ts(i).getname
							ps(o)=y
							psx(o)=x
							foundx = true
							exit for
						end if
					next

					if not foundx then
						ps(0) = y
						psx(0) = x
						nms(0) = ts(i).getname

					end if

					foundy= true
					exit for

				else
					nms(k) = ts(i).getname
					ps(k)=y
					psx(k) =x
					foundy= true
					exit for
				end if

			next

			if not foundy then
				ps(0) = y
				psx(0) =x
				nms(0) = ts(i).getname
			end if

		end if
	next

	mri nms
	mri ps
	mri psx
	'	mri thiscomponent.textsections.elementnames
End Sub







*Edit: 14/3/24
CORRECTED ERROR IN THE SORTING ROUTINE
Last edited by JeJe on Fri Mar 15, 2024 1:05 am, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: List of Writer Textsections in document order

Post by JeJe »

The problem is, if a document is reopened then the textsections will be in document order, but when any textsections are added they get appended to the api list, not added in document order. Or if sections are moved their position is not changed in the API list either. And sections can't be found using the find dialog/methods. The Navigator is updated but we don't have ready access to that.

Edit
The list of headings the API provides us is in document order though, or available with search for styles - so a workaround is to always begin a section with a heading, or (which I prefer) have a heading followed by a section. If the heading text and section name are both the same then we have the sections in document order given us from the headings.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: List of Writer Textsections in document order

Post by JeJe »

CORRECTED ERROR IN THE SORTING ROUTINES

There may not be an exact match with the navigator list given by the view cursor position in the case of sections in frames.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply