[Solved] Saving Writer document with table

The Application Programming Interface and the OASIS Open Document Format

[Solved] Saving Writer document with table

Postby gma » Fri Jan 24, 2014 8:51 am

I create document and insert a table with 1 row and 4 columns, and save it (via delphi).

Code: Select all   Expand viewCollapse view
oTable := oDoc.createInstance('com.sun.star.text.TextTable');
oTable.Initialize(1, 4);
oTable.setPropertyValue('HoriOrient', 7); // LEFT_AND_WIDTH
oTable.setPropertyValue('IsWidthRelative', True);
oTable.setPropertyValue('RelativeWidth', 90);
oDoc.getText.insertTextContent(oDoc.getText.CreateTextCursor, oTable, False);

oTable.getCellByPosition(0, 0).CreateTextCursor.setString(OleVariant('1'));
oTable.getCellByPosition(1, 0).CreateTextCursor.setString(OleVariant('2'));
oTable.getCellByPosition(2, 0).CreateTextCursor.setString(OleVariant('3'));
oTable.getCellByPosition(3, 0).CreateTextCursor.setString(OleVariant('4'));

// save document without additional parameters
oDoc.StoreAsURL(FileNameToURL('d:\temp\oo_save_test01.odt'), VarArrayCreate([0, -1], varVariant));


When I open result file in OpenOffice, I see a table with 1 row and 1 column.
I tried in OO 3.2 and 4.0.1.
What I'm doing wrong?
Attachments
oo_save_test01.odt
(7.98 KiB) Downloaded 127 times
Last edited by gma on Sun Jan 26, 2014 1:49 pm, edited 1 time in total.
I use OpenOffice 3.2, 4.0 and LibreOffice 4.2 on Windows 7
gma
 
Posts: 5
Joined: Thu Jan 23, 2014 7:26 am
Location: Russia

Re: Saving writer document with table

Postby FJCC » Fri Jan 24, 2014 5:25 pm

I tried your code in OO Basic, making as few changes as possible.
Code: Select all   Expand viewCollapse view
oDoc = ThisComponent
oTable = oDoc.createInstance("com.sun.star.text.TextTable")
oTable.Initialize(1, 4)
oTable.setPropertyValue("HoriOrient", 7) 'LEFT_AND_WIDTH
oTable.setPropertyValue("IsWidthRelative", True)
oTable.setPropertyValue("RelativeWidth", 90)
oDoc.getText.insertTextContent(oDoc.getText.CreateTextCursor, oTable, False)

oTable.getCellByPosition(0, 0).CreateTextCursor.setString("1")
oTable.getCellByPosition(1, 0).CreateTextCursor.setString("2")
oTable.getCellByPosition(2, 0).CreateTextCursor.setString("3")
oTable.getCellByPosition(3, 0).CreateTextCursor.setString("4")

It works fine. The only thing I notice is that the initialize method is not capitalized when I inspect oTable using MRI. That is, it should be
Code: Select all   Expand viewCollapse view
oTable.initialize(1, 4)

Basic doesn't care about such things but perhaps Delphi does. Of course, that should cause a error during execution, so it doesn't seem to be the problem.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7490
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Saving Writer document with table

Postby gma » Sat Jan 25, 2014 8:10 am

Did you save a document?
After this code, my table is displayed fine, and have 4 columns. I can print it and saw 4 columns at paper.
After saving, closing and opening of document - table have 1 column.

I watched xml content of doc, it have description of one column and one row with four cells with data ("1", "2", "3" and "4").

May be there is a mistake somewhere in initialization code
Code: Select all   Expand viewCollapse view
// without this string
oTable.setPropertyValue("HoriOrient", 7) 'LEFT_AND_WIDTH
// or this
oTable.setPropertyValue("HoriOrient", 3) 'LEFT
// I cannot change width of a table in future

// without this
oTable.setPropertyValue("IsWidthRelative", True)
oTable.setPropertyValue("RelativeWidth", xxx)
// table created with 34 sm width - two times more than a width of page
I use OpenOffice 3.2, 4.0 and LibreOffice 4.2 on Windows 7
gma
 
Posts: 5
Joined: Thu Jan 23, 2014 7:26 am
Location: Russia

Re: Saving Writer document with table

Postby gma » Sat Jan 25, 2014 8:45 am

If I omit table width setting,
Code: Select all   Expand viewCollapse view
oTable.Initialize(1, 4)
oDoc.getText.insertTextContent(oDoc.getText.CreateTextCursor, oTable, False)

result is ok.

In first case (with table width), xml content:
Code: Select all   Expand viewCollapse view
<table:table-column table:style-name="Таблица1.A"/>

In second case (without table widht):
Code: Select all   Expand viewCollapse view
<table:table-column table:style-name="Таблица1.A" table:number-columns-repeated="4"/>
I use OpenOffice 3.2, 4.0 and LibreOffice 4.2 on Windows 7
gma
 
Posts: 5
Joined: Thu Jan 23, 2014 7:26 am
Location: Russia

Re: Saving Writer document with table

Postby FJCC » Sat Jan 25, 2014 7:28 pm

You are correct that after closing and reopening the file my original code had the same problem as yours. This version seems to work better. Insert the table first and then adjust the width.
Code: Select all   Expand viewCollapse view
oDoc = ThisComponent
oTable = oDoc.createInstance("com.sun.star.text.TextTable")
oTable.Initialize(1, 4)

oDoc.getText.insertTextContent(oDoc.getText.CreateTextCursor, oTable, False)
oTable.setPropertyValue("HoriOrient", 7) 'LEFT_AND_WIDTH
oTable.setPropertyValue("RelativeWidth", 90)

oTable.getCellByPosition(0, 0).CreateTextCursor.setString("1")
oTable.getCellByPosition(1, 0).CreateTextCursor.setString("2")
oTable.getCellByPosition(2, 0).CreateTextCursor.setString("3")
oTable.getCellByPosition(3, 0).CreateTextCursor.setString("4")
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7490
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Saving Writer document with table

Postby gma » Sun Jan 26, 2014 1:48 pm

FJCC, thank you very much!
It looks like it works.
Earlier I tried to change order of operations and got some errors...
I use OpenOffice 3.2, 4.0 and LibreOffice 4.2 on Windows 7
gma
 
Posts: 5
Joined: Thu Jan 23, 2014 7:26 am
Location: Russia


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 2 guests