Page 1 of 1

Convert text to table code example

PostPosted: Mon Mar 31, 2014 7:55 pm
by Echoplex
Hello everyone.

I'm writing a c# program which uses OO and now I have some trouble using OO API. I need to convert the formatted text(tabs) into the table. After a lot of searching I found convertToTable function which receive few parameters that I didn't understand. Somebody, please post the code on C# (or Java) how to use this function, and if you may write down list of all properties (Cell, Row, Table).

Thanks.

Re: Convert text to table code example

PostPosted: Mon Mar 31, 2014 8:25 pm
by Villeroy
You can query all properties, methods and services from any UNO object. Install the MRI function and read the MRI tutorial on this forum.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 11:43 am
by Echoplex
Yes, that is not a problem. But I can't figure out how to use these parameters properly when I call convertToTable method. Here's how I try to call it:
Code: Select all   Expand viewCollapse view
XTextRange str;
str.setString(data); //data is string with formatted text
XTextConvert XTC;
XPropertySet CellProperties = (XProperty)XTC;
XPropertySet RowProperties = (XProperty)XTC;
XPropertySet TableProperties = (XProperty)XTC;
table = XTC.convertToTable(str, CellProperties, RowProperties, TableProperties); //table is XTextTable object

Error: Invalid arguments.

I tried a lot of ways but none of them is working. So I came here for help.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 12:20 pm
by B Marcelly
Hi,
Echoplex wrote:I need to convert the formatted text(tabs) into the table.

I am not sure what is your problem.
Is your original text a csv file ? (simple text file with fields separated by tabs)
Do you really want to obtain a table in a Writer document ?

If the answer is yes to both questions, I suggest you forget about convertToTable() and instead read your input file by program and fill the Writer table cell by cell.
Other solution : the dispatcher (Macro recorder) seems to work well.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 12:26 pm
by Villeroy
If it is about csv, connect a Base document and create a report (which is a Writer table).

Hint: Download and extract the attached zip from my spreadsheet topic [Example] Loading CSV into preformatted spreadsheets
Open the CSV_Database.odb
Click the Tables container, right-click Table1 (which is a representation of file Table1.csv) and choose "Report Wizzard".
Add all columns and finish. The resulting report is a Writer document with all data from Table1.csv dynamically linked.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 1:13 pm
by Echoplex
Hi everyone!

No, I have string with text, like:
Code: Select all   Expand viewCollapse view
111\t222\t333\n444\t555\t666

and this string convert to table.
For Example: https://wiki.openoffice.org/wiki/Docume ... a_table%3F

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 2:07 pm
by B Marcelly
As usual, without a real example from real data, it's a guess work.
From what you show, my previous answer is still valid.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 2:46 pm
by Echoplex
I have one program which generates a mass of text for filling .dot file. Parts of this text need to be converted into tables. That program can fill such file using MS Office API but I also need to make the same things with Open Office API. Filling tables cell by cell takes a lot of time. I need to use convertToTable method because it converts text separated by tabs into table. But problem is that I can't find any examples of use this method in documentation of OO API.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 5:34 pm
by Villeroy
Read the entire text string into a variable.
Analyse the column count (tabs.count +1) and the row count
Create a table with rows and columns as needed.
Split your text lines by tabs and fill the resulting arrays into the table.
What's so difficult about it? You are in a hurry and need the solution now. On the other hand did not even spend 10 hours with macro programming in OpenOffice. 10 hours time of learning about UNO is close to nothing but after 10 hours playing with other peoples code, object inspectors and documentation you would have enough knowledge to ask the right questions properly.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 8:37 pm
by Echoplex
Thank you that you're trying to help me but I found the method which I need (yes, convertToTable) and this method should do what I need without any hacks. The problem is that I have no clue how to properly call it, mostly how to make parameters received by this method. I read all the OO API manuals which I found, I read wiki, I read other people's code but I didn't find anything what can help me to solve the issue. That's why I wrote my question on this forum.

Re: Convert text to table code example

PostPosted: Wed Apr 02, 2014 9:11 pm
by Villeroy
All this macro programming with OpenOffice is the opposite of productivity and you may spend another day investigating the source code about valid names and types of property values for this particular method. Possibly you are the first person on the planet who wants to split tab-delimited text this way. If you have a program that does the trick with MS Word, why don't you want to use it? Reportedly, recent versions of MS Word support ODF. Let your VBA macro convert to table and save a .odt copy.
Next thing you can try is the macro recorder but I doubt that it comes up with any working code snippet.
Then you may write a macro to store a Word doc as plain text, run a database report on this file, save the report as .odt, close the report, replace the text file with the next shit, re-run the report, save the report, close the report and so on.

Re: Convert text to table code example

PostPosted: Sat Feb 20, 2021 3:36 am
by JeJe
(Albeit 7 years later)...I resurrected this topic in a new thread with MrProgrammer providing the solution, here:

viewtopic.php?f=20&t=104535&p=506723#p506723