[Solved] Find predecessor paragraph for TextTable/Cell

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

[Solved] Find predecessor paragraph for TextTable/Cell

Post by brauart »

Hello,
I have a TextField within a TextTable/Cell. From there I would like to navigate (or get access) to the previous (in terms of textflow) text paragraph.
I tried several things.
From the cell I can get the TextTable through properties, but am stuck then.
Using cursors I did not succeed too.

Any help appreciated.

Thanks,
brauart
Last edited by Hagar Delest on Tue Aug 04, 2020 11:42 am, edited 1 time in total.
Reason: tagged solved
OpenOffice 4.1.7 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Find predecessor paragraph for TextTable/Cell

Post by JeJe »

table.anchor

For a textcursor in the text where the table is anchored

p = table.anchor
tc = p.text.createtextcursorbyrange(p)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Find predecessor paragraph for TextTable/Cell

Post by brauart »

@JeJe
it does not work. The line p.text.createtextcursorbyrange(p) gives an error. When I replace it as shown below tc is void and the selection is still in the table.
To test is a document with a paragrapah and a table is needed, where in some table cell a User Field "MyField" is inserted.

Code: Select all

Sub testSelection
	oMasters = ThisComponent.getTextFieldMasters()
	depField = oMasters.getByName("com.sun.star.text.fieldmaster.User.MyField").DependentTextFields(0)
	ThisComponent.CurrentController.select(depField.Anchor)
	
    myCursor = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(depField.Anchor)
    table = myCursor.TextTable
    
    p = table.anchor
	'tc = p.text.createtextcursorbyrange(p)
	tc = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(p)
	
	' here I would like to select the previous paragraph
	ThisComponent.CurrentController.select(tc)
End Sub
OpenOffice 4.1.7 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Find predecessor paragraph for TextTable/Cell

Post by JeJe »

Works for me on OO.

Code: Select all


Sub testSelection
  oMasters = ThisComponent.getTextFieldMasters()
  depField = oMasters.getByName("com.sun.star.text.fieldmaster.User.MyField").DependentTextFields(0)
 ThisComponent.CurrentController.select(depField.Anchor)
   
  myCursor = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(depField.Anchor)
 table = myCursor.TextTable
 
p = table.anchor

tc = p.text.createtextcursorbyrange(p)
tc.goleft(1,false)
tc.gotoStartOfParagraph(false)
tc.gotoEndOfParagraph(true)
   ThisComponent.CurrentController.select(tc)
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Find predecessor paragraph for TextTable/Cell

Post by brauart »

@JeJe
you are right, it does work - in AOO. Thank you.
I must admit I tried this with LibreOffice, and there p.text is void.
I did not expect to find differences in the data model AOO vs LO.
OpenOffice 4.1.7 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Find predecessor paragraph for TextTable/Cell

Post by JeJe »

Another method would be once you have the table name to enumerate the document's text paragraphs (if you know that's where its anchored) looking for supportsService("com.sun.star.text.TextTable")

From Useful Macro Information For OpenOffice By Andrew Pitonyak

Code: Select all

Sub EnumerateParagraphs
REM Author: Andrew Pitonyak
Dim oParEnum 'Enumerator used to enumerate the paragraphs
Dim oPar 'The enumerated paragraph
REM Enumerate the paragraphs.
REM Tables are enumerated along with paragraphs
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
REM This avoids the tables. Add an else statement if you want to
REM process the tables.
If oPar.supportsService("com.sun.star.text.Paragraph") Then
MsgBox oPar.getString(), 0, "I found a paragraph"
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
Print "I found a TextTable"
Else
Print "What did I find?"
End If
Loop
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Find predecessor paragraph for TextTable/Cell

Post by Lupp »

A cell may not have a preceding TextParagraph. The TextTable will - in most cases, but not always.

Since you also work with LibreOffice the VERY raw code below might help. It did during my tests even for a nested TextTable.
TextTable immediately following another one (without a paragraph in-between), or at the very beginning of the text were not tested.
I also would not expect the code to work in AOO. (Didn't test.)

Code: Select all

Function precedingParagraphToTheTextTableContainingTheSelctedTextField()
doc = ThisComponent
cCtrl = doc.CurrentController
sel = doc.CurrentSelection
tf = sel(0)
tTa = tf.TextTable
tTcur = tTa.createCursorByCellName("A1")
cCtrl.select(tTcur)
vc = cCtrl.ViewCursor
vc.goLeft(1, False)
h =  vc.TextParagraph
precedingParagraphToTheTextTableContainingTheSelctedTextField = h
cCtrl.select(sel)
End Function
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Find predecessor paragraph for TextTable/Cell

Post by JeJe »

If you're in the first cell and there is a previous text paragraph you could just use

Code: Select all

thiscomponent.currentcontroller.viewcursor.goup(1,false)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: [Solved] Find predecessor paragraph for TextTable/Cell

Post by brauart »

@JeJe and @Lupp
Thank you. Yes, switching to the viewCursor solved my problem.

Code: Select all

	myCursor = myTable.getCellByPosition(0,0).createTextCursor()
	myCursor.gotoStartOfParagraph(false)
	ThisComponent.CurrentController.select(myCursor)
	
	viewCursor = ThisComponent.CurrentController.viewcursor
	viewCursor.goLeft(1,false)
	myCursor = viewCursor.getText().createTextCursorByRange(viewCursor)
	myCursor.gotoEndOfParagraph(false)
	myCursor.gotoStartOfParagraph(true)
	ThisComponent.CurrentController.select(myCursor)
OpenOffice 4.1.7 on Windows 10
Post Reply