[Solved] Writer convertToTable

Creating a macro - Writing a Script - Using the API
Post Reply
JeJe
Volunteer
Posts: 2054
Joined: Wed Mar 09, 2016 2:40 pm

[Solved] Writer convertToTable

Post by JeJe »

This has been asked before a few years ago and didn't get resolved. Converting textrange to a table. The API for it is here (My searching couldn't find anything further):

https://www.openoffice.org/api/docs/com ... nvert.html

convertToTextFrame on that page works fine, where tc and tc2 are text ranges

Code: Select all

thiscomponent.text.convertToTextFrame(tc.start,tc2.end,array())
But I don't understand how to make the text ranges sequence for converttotable

convertToTable
XTextTable
convertToTable( [in] sequence< sequence< sequence< XTextRange > > > TableRanges,
[in] sequence< sequence< ::com::sun::star::beans::PropertyValues > > CellProperties,
[in] sequence< ::com::sun::star::beans::PropertyValues > RowProperties,
[in] ::com::sun::star::beans::PropertyValues TableProperties )
raises( ::com::sun::star::lang::IllegalArgumentException,
::com::sun::star::beans::UnknownPropertyException );

Description
.
Parameter TableRanges
contains the TextRange interfaces of the paragraphs, cells and rows of the table.

The innter sequence contains the start and end paragraphs of each table cell. The next sequence groups the cells to a row and the outer sequence groups the rows of the table.
Parameter CellProperties
contains the properties of each cell.
Parameter RowProperties
contains the properties of each table row.
Parameter TableProperties
contains the properties of the table.
Returns
the created table.
Anyone understand the bit in bold and what to put in the first array sequence? This gives a crash:

Code: Select all

'thiscomponent.text.convertToTable(array(tc.start,tc2.end),array(),array(),array())
Edit: attached my test document, which is a sample two words in the document text and this code to create the two ranges for which to test on.

Code: Select all

vc=thiscomponent.currentcontroller.viewcursor
tc = vc.text.createtextcursorbyrange(vc.text.start)
tc2 = vc.text.createtextcursorbyrange(vc.text.start)
tc.goright(6,true)
tc2.goright(6,false)
tc2.goright(6,true)

thiscomponent.text.convertToTextFrame(tc.start,tc2.end,array()) 'works fine
'thiscomponent.text.convertToTable(array(tc.start,tc2.end),array(),array(),array()) 'crash
'what to put?
NOTE THE BIT ABOUT OO CRASHES
Attachments
converttotable.odt
(10.03 KiB) Downloaded 115 times
Last edited by JeJe on Sat Feb 20, 2021 3:55 pm, edited 1 time in total.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
Posts: 2054
Joined: Wed Mar 09, 2016 2:40 pm

Re: Writer convertToTable

Post by JeJe »

[in] sequence< sequence< sequence< XTextRange > > > TableRanges,
Maybe its of the following form, an array of three array sequences?

Code: Select all


a=array(tc.start,tc.end)'what to put? "the start and end paragraphs of each table cell"
b=array(tc) 'what to put? "groups the cells to a row "
c=array(tc) 'what to put? "groups the rows of the table."
thiscomponent.text.convertToTable(array(a,b,c),array(),array(),array()) 
'gives illegal argument message

Edit:

or maybe the "groups the cells to a row " and "groups the rows of the table." bits refer to the second and third parameters - it so what to put in those?
Openoffice 4.1.6
Windows 8
User avatar
MrProgrammer
Moderator
Posts: 4408
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Writer convertToTable

Post by MrProgrammer »

JeJe wrote:Anyone understand the bit in bold and what to put in the first array sequence?
I have done very little programming with the API and have never worked with the table interface so you probably shouldn't listen to me however I believe the API reference for convertToTable means that the method takes four parameters all of which are arrays:

An array for the rows of the new table, with values of type T1
•• T1 is an array for the cells in each row, with values of type T2
••• T2 is an array for the paragraphs of each cell of the row, with values of type ::com::sun::star::text::TextRange

An array for the rows of the table, with values of type T3
•• T3 is an array for the cells in each row, with values of type ::com::sun::star::text::CellProperties

An array for the rows of the table, with values of type ::com::sun::star::text::TextTableRow

An array with values of type ::com::sun::star::beans::PropertyValue, with the allowed table properties in ::com::sun::star::text::TextTable

The table is created using the 4th parameter; the rows are created using the 3rd parameter, the cells of the row are created using the 2nd parameter; the cell content is created using the 1st parameter.

Since I work very little with macros, preferring to master the user interface, I may not have the terminology quite right above. If the convertToTable method proves to be too confusing, perhaps it is easier to create an empty table and then step through it to set the cell values. Or, one can build a block of text representing the table, tabs to separate cell text and paragraphs to separate rows. When the text is selected, the dispatcher can create the table. A recorded macro to execute Table → Convert → Text to table will show what parameters are needed. I'm sure this way uses a lot of defaults for the table, but those can be corrected by the macro after the table has been created by the dispatcher. The macro can set the cell content as desired since the Text to Table feature has limited function. For example, I don't believe it's possible to create a cell with multiple paragraphs.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800 on MacOS 10.14.6.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2054
Joined: Wed Mar 09, 2016 2:40 pm

Re: Writer convertToTable

Post by JeJe »

Brilliant thanks!!!

I've managed to create a table using the (almost) simplest case, one cell (it didn't like a single paragraph though so I needed two)

The document text is just these two paragraphs (edit: with a space after FISH.)

FISH
GOOSE

Code: Select all

Sub Main

vc=thiscomponent.currentcontroller.viewcursor
tc = vc.text.createtextcursorbyrange(vc.text.start)
tc2 = vc.text.createtextcursorbyrange(vc.text.start)
tc.goright(6,true)
tc2.goright(6,false)
tc2.goright(6,true)

a=array(array(array(tc,tc2)))
b=array(array())
c=array()
d=array()

thiscomponent.text.convertToTable(a,b,c,d)
End Sub
A table got created. Thank you!

Edit: I was using a method of creating and inserting a table first... and then in turn selecting the text I wanted for each cell... and inserting it into the table with gettransferable / settransferable. It worked, but this 'proper way' looks like it might be way better...
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
Posts: 2054
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writer convertToTable

Post by JeJe »

I was making the mistake of including the end paragraph character in my paragraph ranges - which causes strange results.

With that corrected, it seems to work fine and you can put multiple paragraphs in a cell.

In my above example I should have put .goright(5,true) instead of .goright(6,true) including just the word in the paragraph not the end paragraph mark.

This creates a two column, single row table where the first cell has paragraph tc1 in it and the second has paragraph ranges tc2 to tc5

Code: Select all

a=array(array(array(tc1,tc1),array(tc2,tc5))) '
b=array(array())
c=array()
d=array()
thiscomponent.text.convertToTable(a,b,c,d)
Thanks again for helping me crack this.

Edit:
One slight issue - the operation doesn't appear in the Undo list/can't be undone with undo. That seems to be all.
Openoffice 4.1.6
Windows 8
User avatar
MrProgrammer
Moderator
Posts: 4408
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] Writer convertToTable

Post by MrProgrammer »

JeJe wrote:One slight issue - the operation doesn't appear in the Undo list/can't be undone with undo.
Perhaps you can add it to the list with interface XUndoManager. I have never used this interface, perhaps the subject for a new topic.
Mr. Programmer
AOO 4.1.7 Build 9800 on MacOS 10.14.6.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2054
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writer convertToTable

Post by JeJe »

Thanks, yeah something might perhaps be possible with that - it might need an undo listener. The operation can be easily undone by the Convert table to text operation that's available in the Table menu anyway.

XUndoManager is really useful... if you have a macro that does 1000 successive operations on a document, you can call it a name and have that name on the undo list and undo all 1000 operations at once when called.
Openoffice 4.1.6
Windows 8
Post Reply