| View previous topic :: View next topic |
| Author |
Message |
hld Newbie

Joined: 31 Aug 2010 Posts: 2
|
Posted: Tue Aug 31, 2010 10:00 am Post subject: Writer: TextTable Convert Rows to Columns |
|
|
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 |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Tue Aug 31, 2010 12:51 pm Post subject: |
|
|
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 http://forum.openoffice.org |
|
| Back to top |
|
 |
hld Newbie

Joined: 31 Aug 2010 Posts: 2
|
Posted: Tue Aug 31, 2010 10:37 pm Post subject: |
|
|
| thx. it works fine. now I try to transfer all the cell-styles... |
|
| Back to top |
|
 |
|
|
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
|