OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Writer: TextTable Convert Rows to Columns

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
hld
Newbie
Newbie


Joined: 31 Aug 2010
Posts: 2

PostPosted: Tue Aug 31, 2010 10:00 am    Post subject: Writer: TextTable Convert Rows to Columns Reply with quote

hello,
we generate some ooo text documents with java and jodreports (a freemarker template solution for odf-files). for "special" tasks we can run some macros after the generating process. what's our problem:

the table model in odf/odt is row-based. we have a table like this:

a b c d
1 2 3 4
5 6 7 8

Is there any posibility to transfer this table to a "column"-based table (incl. copy cell style)?

a 1 5
b 2 6
c 3 7
d 4 8

This was my first try:
Code:

Sub Test
   Dim Doc As Object
   Dim Table As Object
   Dim Cursor As Object
   Dim Rows As Object
   Dim RowIndex As Integer
   Dim Cols As Object
   Dim ColIndex As Integer
   Dim CellName As String
   Dim Cell As Object
   Dim myCell1 As Object
   Dim myCell2 As Object
   Dim myText As String
   Dim TextTables As Object
   
   Doc = StarDesktop.CurrentComponent
   ThisComponent.createInstance("com.sun.star.text.TextTable")
   Dim oCursor As Object
   Dim oTable As Object
   Dim oText As Object
   oText = ThisComponent.Text
   oCursor = oText.createTextCursor()
   oCursor.gotoStart(FALSE)
   oTable = oDocument.createInstance("com.sun.star.text.TextTable")
   oTable.initialize(5,9)
   oText.insertTextContent(oCursor, oTable, FALSE)
   
   
   TextTables = ThisComponent.getTextTables()
   For I = 0 to TextTables.count - 1
   Table = TextTables(I)
   If Left(Table.name,3) = "alt" then
   Rows = Table.getRows
   Cols = Table.getColumns
   
   For RowIndex = 0 To Rows.getCount()-1
   For ColIndex = 0 To Cols.getCount()-1
   If RowIndex < ColIndex then
   myCell1 = Table.getCellByPosition(RowIndex,ColIndex)
   myCell2 = Table.getCellByPosition(ColIndex,RowIndex)
   myText = myCell1.String
   myCell1.String = myCell2.String
   myCell2.String = myText
   end If
   Next
   Next
   End If
   Next
End Sub



or do you know a complete different solution/have an other idea?


thx for your help,

Thies.
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Tue Aug 31, 2010 12:51 pm    Post subject: Reply with quote

I create a table with 2 columns and 3 rows and arbitrary content.
The following code makes use of the built-in and fast Calc function which transposes tabular data regardless of data type.
Unforturnately it takes a flat, 2-dimensional array as input.
I fetch the table's DataArray (nested array of row values) and convert it to a plain Basic array. The resulting array x() is a nested DataArray again.
Code:

sub transposeFirstWriterTable
tbl=thiscomponent.texttables.getbyindex(0)
a()=tbl.getdataarray()
DataArray2PlainArray(a())
srv= createUnoService("com.sun.star.sheet.FunctionAccess")
x=srv.callFunction("TRANSPOSE",array(a()))
nRows= ubound(x())+1
nCols= ubound(x(0))+1
tbl=getNewWriterTable(thiscomponent, nCols, nRows)
tbl.setDataArray(x())
end sub

function getNewWriterTable(doc,cols, rows)
   tbl = doc.createInstance("com.sun.star.text.TextTable")
   tbl.initialize(rows,cols)
   xrange = doc.getText
   xrange.insertTextContent(xrange,tbl,true)
   getNewWriterTable = tbl
end function

Sub DataArray2PlainArray(aRows())
Dim i%,j%,aCols(),aTmp()
redim aTmp(1 to uBound(aRows())+ 1,1 To uBound(aRows(0)) +1)
for i = 0 to uBound(aRows())
   aCols = aRows(i)
   for j = 0 to uBound(aCols())
      aTmp(i +1,j +1) = aCols(j)
   next
next
aRows() = aTmp()
End Sub

The above code toggles a test table between 2x3 and 3x2 layout keeping all data (numbers and text) as is.
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
hld
Newbie
Newbie


Joined: 31 Aug 2010
Posts: 2

PostPosted: Tue Aug 31, 2010 10:37 pm    Post subject: Reply with quote

thx. it works fine. now I try to transfer all the cell-styles...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group