[Solved] Create table with rows & columns of variable size

The Application Programming Interface and the OASIS Open Document Format

[Solved] Create table with rows & columns of variable size

Postby basilbc » Mon Oct 04, 2010 11:53 am

Hello to all,

I am new to OOo scripting. Previously i used to use "Record Macro" functionality to automate the below table generation.
I use it to document test cases. Since i am confortable with Python, i need some help to generate the table in the same
format as shown in image.

Currently i am using a sample program to generate to create a Writer Document and generate the table. I want to
write a pyhton script that will generate the table alone. Then i can assigne it to a keyboard shortcut, so that whenever
i want to generate the table i just need press that key, rest the script will take care. I am not able to find any samples
that tells me how to split a cell, how to change coloumn width or row height, font properties and cell background colour.

Kindly help me, in creating a script that will generate the table, so that i can have a start in creating other kinds
of tables after studying it.

The table fits in A4 sheet.
The font used in tables are Tahoma, size 8 and bold.
The width of the 1st cell in 1st row is 0.48"
The width of the 1st cell in 3rd row is 1.99"
The width of the 1st cell in 5th & 6th row is 0.30"
The colors are light grey, cyan and light orange

TP.jpg
Testing Procedure


Thanks. BASIL. :geek:
Last edited by Hagar Delest on Thu Oct 07, 2010 12:31 pm, edited 1 time in total.
Reason: tagged [Solved].
BASIL B.C., OOo 3, Windows Xp.
basilbc
 
Posts: 3
Joined: Mon Oct 04, 2010 11:23 am

Re: Help in creating table with rows & columns of variable s

Postby hanya » Wed Oct 06, 2010 2:11 pm

There is no one wanted to write whole code for you, you should provide piece of code with your post.

Code: Select all   Expand viewCollapse view
from com.sun.star.text import TableColumnSeparator
from com.sun.star.awt.FontWeight import BOLD as FW_BOLD

def table_test():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
   
    oTable = oDoc.createInstance("com.sun.star.text.TextTable")
    oTable.setName("NewTable")
    oTable.initialize(7, 2)
   
    oText.insertTextContent(oText.getEnd(), oTable, True)
   
    style_name = oTable.getCellByName("A2").createTextCursor().ParaStyleName
    oTable.getCellByName("A1").createTextCursor().ParaStyleName = style_name
   
    # to set formats using style is better
    oRange = oTable.getCellRangeByName("A1:B7")
    oRange.CharHeight = 8.0
    oRange.CharWeight = FW_BOLD
    oRange.CharFontName = "Tahoma"
   
    oCursor = oTable.createCursorByCellName("A1")
   
    oTable.getCellByName("A1").setString("TP-999")
    oTable.getCellRangeByName("A1:B1").BackColor = 12632256
   
    oCursor.gotoCellByName("A2", False)
    oCursor.goRight(1, True)
    oCursor.mergeRange()
   
    oTable.getCellByName("A2").setString("Changes")
    oTable.getCellByName("A2").BackColor = 16777164
   
    oCursor.gotoCellByName("A4", False)
    oCursor.goRight(1, True)
    oCursor.mergeRange()
    oTable.getCellByName("A4").setString("Test Cases")
    oTable.getCellByName("A4").BackColor = 12119807
   
    oTable.getCellByName("A5").setValue("999")
   
    oCursor.gotoCellByName("A6", False)
    oCursor.goRight(1, True)
    oCursor.mergeRange()
   
    oTable.getCellByName("A6").BackColor = 16749838
    oTable.getCellByName("A6").setString("Findings")
    oTable.getCellByName("A7").setValue("999")
   
    # set position of the row separator
    tcs = TableColumnSeparator(1500 * 10000 / 21000, True)
    oTable.TableColumnSeparators = (tcs, )
I do not know the way to set position of the separator of each row individually.

I am not able to find any samples
that tells me how to split a cell, how to change coloumn width or row height, font properties and cell background colour.
Search inside the forum and web, and read the DevGuide.

And if you choose Python for your answer, it might be difficult to get responses.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Help in creating table with rows & columns of variable s

Postby rudolfo » Wed Oct 06, 2010 4:29 pm

Let's look at it from a different point of view: What is your "sample program" that you are currently using to generate the tables? Why not building a Basic macro with:
Code: Select all   Expand viewCollapse view
Sub generateDocWithTable()
shell("... your command ...")
End Sub

I am sure there is something similar in python, but as you don't need the interpreter (you only use as a wrapper to let the OS start an application) Basic is better integrated into OOo.
A plain python macro accessing the OOo UNO object might be faster (depending on how your sample program is implemented). But as long as you don't have performance issues I would take the easy way.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Help in creating table with rows & columns of variable s

Postby basilbc » Thu Oct 07, 2010 4:13 am

Thank you for the help. That was perfect. I believe this will be enough to have a start.


These were my ideas with the document manipulation, and i believe i can work on
(1) generating an index table by grabbing the the "test case titles" from each
generated table (the content in the 2nd cell of 1st row). and inserting the index
table in page 2 of the document.
TP_Index.jpg
Index for testing procedures
TP_Index.jpg (10.33 KiB) Viewed 19703 times


2) grab the contents in findings (multiple or single row) with test case id "TP-999" and
copy them into a new document with the file name "BASILBCTRes_"<assignment alias>".doc
in a table as shown below (to me this looks like insane, and i should better use a software rather than
word processor)
TR_List.jpg



This was the sample python program i got from OOo pyUNO website
I was happy with the speed of the execution when conpared to my "record macro" method.
Is there any documentation for OOo source code similar to java documentation ?
After going through this program, i wanted to know
(1) how to split a sow into cells of desired width
(2) how to generate a new row and save the document

Code: Select all   Expand viewCollapse view
import uno

# a UNO struct later needed to create a document
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
from com.sun.star.awt import Size

from com.sun.star.lang import XMain

def insertTextIntoCell( table, cellName, text, color ):
    tableText = table.getCellByName( cellName )
    cursor = tableText.createTextCursor()
    cursor.setPropertyValue( "CharColor", color )
    tableText.setString( text )


def createTable():
    """creates a new writer document and inserts a table with some data (also known as the SWriter sample)"""
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

    # open a writer document
    doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )

    text = doc.Text
    cursor = text.createTextCursor()
    text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
    text.insertString( cursor, "Now we are in the second line\n" , 0 )

    # create a text table
    table = doc.createInstance( "com.sun.star.text.TextTable" )

    # with 4 rows and 4 columns
    table.initialize( 4,4)

    text.insertTextContent( cursor, table, 0 )
    rows = table.Rows

    table.setPropertyValue( "BackTransparent", uno.Bool(0) )
    table.setPropertyValue( "BackColor", 13421823 )
    row = rows.getByIndex(0)
    row.setPropertyValue( "BackTransparent", uno.Bool(0) )
    row.setPropertyValue( "BackColor", 6710932 )

    textColor = 16777215

    insertTextIntoCell( table, "A1", "FirstColumn", textColor )
    insertTextIntoCell( table, "B1", "SecondColumn", textColor )
    insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
    insertTextIntoCell( table, "D1", "SUM", textColor )

    values = ( (22.5,21.5,121.5),
              (5615.3,615.3,-615.3),
              (-2315.7,315.7,415.7) )
    table.getCellByName("A2").setValue(22.5)
    table.getCellByName("B2").setValue(5615.3)
    table.getCellByName("C2").setValue(-2315.7)
    table.getCellByName("D2").setFormula("sum <A2:C2>")

    table.getCellByName("A3").setValue(21.5)
    table.getCellByName("B3").setValue(615.3)
    table.getCellByName("C3").setValue(-315.7)
    table.getCellByName("D3").setFormula("sum <A3:C3>")

    table.getCellByName("A4").setValue(121.5)
    table.getCellByName("B4").setValue(-615.3)
    table.getCellByName("C4").setValue(415.7)
    table.getCellByName("D4").setFormula("sum <A4:C4>")


    cursor.setPropertyValue( "CharColor", 255 )
    cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )

    text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
    text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
    text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )

    textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
    textFrame.setSize( Size(15000,400))
    textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )

    text.insertTextContent( cursor, textFrame, 0 )

    textInTextFrame = textFrame.getText()
    cursorInTextFrame = textInTextFrame.createTextCursor()
    textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
    textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
    text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )

    cursor.setPropertyValue( "CharColor", 65536 )
    cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )

    text.insertString( cursor, " That's all for now !!" , 0 )

g_exportedScripts = createTable,



This is the macro i am using at the moment to automate table generation which is generated with the help of "Record Macro" function.
The macro works fine with minor inconvenience such as
(1) The width of the 1st cells of row 4 & 6 has to specifed manually during its generation. i.e the time when control reaches
to 1st cell a window will popup asking for cell width. Couple of months before i left a post in another OOo forum regarding how
to write a code for allocating cell width, and no reply yet.
(2) Eventhough. i record the table generation in a proper order, in the end the macro will generate a different one. So i need to copy the
macro of some of the actions and paste it to main macro later to generate a proper table.

Code: Select all   Expand viewCollapse view
sub My_Table5
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "TableName"
args1(0).Value = "TestCase"
args1(1).Name = "Columns"
args1(1).Value = 1
args1(2).Name = "Rows"
args1(2).Value = 7
args1(3).Name = "Flags"
args1(3).Value = 9

dispatcher.executeDispatch(document, ".uno:InsertTable", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(4) as new com.sun.star.beans.PropertyValue
args2(0).Name = "CharFontName.StyleName"
args2(0).Value = ""
args2(1).Name = "CharFontName.Pitch"
args2(1).Value = 2
args2(2).Name = "CharFontName.CharSet"
args2(2).Value = -1
args2(3).Name = "CharFontName.Family"
args2(3).Value = 5
args2(4).Name = "CharFontName.FamilyName"
args2(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "Text"
args3(0).Value = "Test Case"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())

rem ----------------------------------------------------------------------
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "BackgroundColor"
args4(0).Value = 15132390

dispatcher.executeDispatch(document, ".uno:BackgroundColor", "", 0, args4())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args6(4) as new com.sun.star.beans.PropertyValue
args6(0).Name = "CharFontName.StyleName"
args6(0).Value = ""
args6(1).Name = "CharFontName.Pitch"
args6(1).Value = 2
args6(2).Name = "CharFontName.CharSet"
args6(2).Value = -1
args6(3).Name = "CharFontName.Family"
args6(3).Value = 5
args6(4).Name = "CharFontName.FamilyName"
args6(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args6())

rem ----------------------------------------------------------------------
dim args7(0) as new com.sun.star.beans.PropertyValue
args7(0).Name = "Text"
args7(0).Value = "Changes"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())

rem ----------------------------------------------------------------------
dim args8(0) as new com.sun.star.beans.PropertyValue
args8(0).Name = "BackgroundColor"
args8(0).Value = 16777164

dispatcher.executeDispatch(document, ".uno:BackgroundColor", "", 0, args8())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args10(4) as new com.sun.star.beans.PropertyValue
args10(0).Name = "CharFontName.StyleName"
args10(0).Value = ""
args10(1).Name = "CharFontName.Pitch"
args10(1).Value = 2
args10(2).Name = "CharFontName.CharSet"
args10(2).Value = -1
args10(3).Name = "CharFontName.Family"
args10(3).Value = 5
args10(4).Name = "CharFontName.FamilyName"
args10(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args10())

rem ----------------------------------------------------------------------
dim args11(2) as new com.sun.star.beans.PropertyValue
args11(0).Name = "FontHeight.Height"
args11(0).Value = 8
args11(1).Name = "FontHeight.Prop"
args11(1).Value = 100
args11(2).Name = "FontHeight.Diff"
args11(2).Value = 0

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args11())

rem ----------------------------------------------------------------------
dim args12(2) as new com.sun.star.beans.PropertyValue
args12(0).Name = "Amount"
args12(0).Value = 2
args12(1).Name = "Horizontal"
args12(1).Value = false
args12(2).Name = "Proportional"
args12(2).Value = false

dispatcher.executeDispatch(document, ".uno:SplitCell", "", 0, args12())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SetColumnWidth", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args16(4) as new com.sun.star.beans.PropertyValue
args16(0).Name = "CharFontName.StyleName"
args16(0).Value = ""
args16(1).Name = "CharFontName.Pitch"
args16(1).Value = 2
args16(2).Name = "CharFontName.CharSet"
args16(2).Value = -1
args16(3).Name = "CharFontName.Family"
args16(3).Value = 5
args16(4).Name = "CharFontName.FamilyName"
args16(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args16())

rem ----------------------------------------------------------------------
dim args17(0) as new com.sun.star.beans.PropertyValue
args17(0).Name = "Text"
args17(0).Value = "Procud"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args17())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())

rem ----------------------------------------------------------------------
dim args20(0) as new com.sun.star.beans.PropertyValue
args20(0).Name = "Text"
args20(0).Value = "edures"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args20())

rem ----------------------------------------------------------------------
dim args21(0) as new com.sun.star.beans.PropertyValue
args21(0).Name = "BackgroundColor"
args21(0).Value = 13434879

dispatcher.executeDispatch(document, ".uno:BackgroundColor", "", 0, args21())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args23(4) as new com.sun.star.beans.PropertyValue
args23(0).Name = "CharFontName.StyleName"
args23(0).Value = ""
args23(1).Name = "CharFontName.Pitch"
args23(1).Value = 2
args23(2).Name = "CharFontName.CharSet"
args23(2).Value = -1
args23(3).Name = "CharFontName.Family"
args23(3).Value = 5
args23(4).Name = "CharFontName.FamilyName"
args23(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args23())

rem ----------------------------------------------------------------------
dim args24(2) as new com.sun.star.beans.PropertyValue
args24(0).Name = "FontHeight.Height"
args24(0).Value = 8
args24(1).Name = "FontHeight.Prop"
args24(1).Value = 100
args24(2).Name = "FontHeight.Diff"
args24(2).Value = 0

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args24())

rem ----------------------------------------------------------------------
dim args25(2) as new com.sun.star.beans.PropertyValue
args25(0).Name = "Amount"
args25(0).Value = 2
args25(1).Name = "Horizontal"
args25(1).Value = false
args25(2).Name = "Proportional"
args25(2).Value = false

dispatcher.executeDispatch(document, ".uno:SplitCell", "", 0, args25())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:RulerBorders", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args29(4) as new com.sun.star.beans.PropertyValue
args29(0).Name = "CharFontName.StyleName"
args29(0).Value = ""
args29(1).Name = "CharFontName.Pitch"
args29(1).Value = 2
args29(2).Name = "CharFontName.CharSet"
args29(2).Value = -1
args29(3).Name = "CharFontName.Family"
args29(3).Value = 5
args29(4).Name = "CharFontName.FamilyName"
args29(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args29())

rem ----------------------------------------------------------------------
dim args30(0) as new com.sun.star.beans.PropertyValue
args30(0).Name = "Text"
args30(0).Value = "Conclusions"

dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args30())

rem ----------------------------------------------------------------------
dim args31(0) as new com.sun.star.beans.PropertyValue
args31(0).Name = "BackgroundColor"
args31(0).Value = 16764057

dispatcher.executeDispatch(document, ".uno:BackgroundColor", "", 0, args31())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

rem ----------------------------------------------------------------------
dim args33(4) as new com.sun.star.beans.PropertyValue
args33(0).Name = "CharFontName.StyleName"
args33(0).Value = ""
args33(1).Name = "CharFontName.Pitch"
args33(1).Value = 2
args33(2).Name = "CharFontName.CharSet"
args33(2).Value = -1
args33(3).Name = "CharFontName.Family"
args33(3).Value = 5
args33(4).Name = "CharFontName.FamilyName"
args33(4).Value = "Tahoma"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args33())

rem ----------------------------------------------------------------------
dim args34(2) as new com.sun.star.beans.PropertyValue
args34(0).Name = "FontHeight.Height"
args34(0).Value = 8
args34(1).Name = "FontHeight.Prop"
args34(1).Value = 100
args34(2).Name = "FontHeight.Diff"
args34(2).Value = 0

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args34())

rem ----------------------------------------------------------------------
dim args35(2) as new com.sun.star.beans.PropertyValue
args35(0).Name = "Amount"
args35(0).Value = 2
args35(1).Name = "Horizontal"
args35(1).Value = false
args35(2).Name = "Proportional"
args35(2).Value = false

dispatcher.executeDispatch(document, ".uno:SplitCell", "", 0, args35())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:RulerBorders", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array())

end sub
BASIL B.C., OOo 3, Windows Xp.
basilbc
 
Posts: 3
Joined: Mon Oct 04, 2010 11:23 am

Re: Help in creating table with rows & columns of variable s

Postby hanya » Thu Oct 07, 2010 1:15 pm

basilbc wrote:(1) how to split a sow into cells of desired width

You have to specify their width as ratio relative with the table width. If TableColumnRelativeSum is 10000 and Position of the first element of TableColumnSeparators is 2000, the first cell fills 20% of the row. Calculate their width according to your table width.

basilbc wrote:(2) how to generate a new row and save the document

Code: Select all   Expand viewCollapse view
  oTable = oDoc.getTextTables().getByName(sTableName)
  oRows = oTable.getRows()
  oRows.insertByIndex(2, 1)


basilbc wrote:Is there any documentation for OOo source code similar to java documentation ?
There is no complete description about OOo API. You could read DevGuide with IDL Reference:
http://wiki.services.openoffice.org/wik ... pers_Guide
http://api.openoffice.org/docs/common/r ... le-ix.html
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: [Solved] Create table with rows & columns of variable si

Postby basilbc » Wed Oct 13, 2010 6:03 am

Thanks to all for the help. I have modified the original source code inorder to satisfy "variable cell width".
The script will create 20 cells in each column. The cells are then merged from different location, in such a way
that it will achieve the desired cell width.

It would be more convenient, if there was a function specific to adjusing desired cell width (python) such as
"oTable.getCellByName('A1').setCellWidth(10)"

Code: Select all   Expand viewCollapse view
from com.sun.star.text import TableColumnSeparator
from com.sun.star.awt.FontWeight import BOLD as FW_BOLD

def table_test():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
   
    oTable = oDoc.createInstance("com.sun.star.text.TextTable")
    oTable.setName("NewTable")
    oTable.initialize(7, 20)   
   
    oText.insertTextContent(oText.getEnd(), oTable, True)
   
    style_name = oTable.getCellByName("A1").createTextCursor().ParaStyleName
    oTable.getCellByName("A1").createTextCursor().ParaStyleName = style_name
   
    # to set formats using style is better
    oRange = oTable.getCellRangeByName("A1:T7")
    oRange.CharHeight = 8.0
    oRange.CharWeight = FW_BOLD
    oRange.CharFontName = "Tahoma"
   
    oCursor = oTable.createCursorByCellName("A1")
   
    # First column
    oTable.getCellByName("A1").setString("TP-999")
    oTable.getCellRangeByName("A1:T1").BackColor = 12632256
    oCursor.gotoCellByName("C1", False)
    oCursor.goRight(17, True)     
    oCursor.mergeRange()
    oCursor.gotoCellByName("A1", False)
    oCursor.goRight(1, True)     
    oCursor.mergeRange()
   
    # Second Column, fourth column & sixth column
    oTable.getCellByName("A2").setString("Changes")
    oTable.getCellByName("A2").BackColor = 16777164
    oCursor.gotoCellByName("A2", False)
    oCursor.goRight(19, True)   
    oCursor.mergeRange()       
   
    oTable.getCellByName("A4").setString("Test Cases")
    oTable.getCellByName("A4").BackColor = 12119807
    oCursor.gotoCellByName("A4", False)
    oCursor.goRight(19, True)   
    oCursor.mergeRange()   
   
    oTable.getCellByName("A6").setString("Findings")
    oTable.getCellByName("A6").BackColor = 16764057
    oCursor.gotoCellByName("A6", False)
    oCursor.goRight(19, True)   
    oCursor.mergeRange()   
   
    # Column five and column seven (numbers)
    oTable.getCellByName("A5").setValue("999")
    oCursor.gotoCellByName("B5", False)
    oCursor.goRight(18, True)
    oCursor.mergeRange()
   
    oTable.getCellByName("A7").setValue("999")
    oCursor.gotoCellByName("B7", False)
    oCursor.goRight(18, True)
    oCursor.mergeRange()
   
    # Column three
    oCursor.gotoCellByName("F3", False)
    oCursor.goRight(14, True) 
    oCursor.mergeRange()
    oCursor.gotoCellByName("A3", False)
    oCursor.goRight(4, True) 
    oCursor.mergeRange()
BASIL B.C., OOo 3, Windows Xp.
basilbc
 
Posts: 3
Joined: Mon Oct 04, 2010 11:23 am

Re: [Solved] Create table with rows & columns of variable si

Postby _savage » Tue Feb 07, 2017 12:53 pm

Very helpful, thanks!

One question from my side: is it possible to create a table without initialize() and add rows/columns dynamically? If I understand the documentation correctly, then I must define the dimension of the table before I can use it. In that context, what determines the dimensions of the table if the table has spanning/splitting cells?

For example:

Code: Select all   Expand viewCollapse view
+----+----+----+-----+
|    |    |    |     |  (4 columns)
+----+----+----+-----+
|    |               |  (2 columns)
+----+---+---+---+---+
|    |   |   |   |   |  (5 columns)
+----+---+---+---+---+

Is this a table of 3 rows with 2, or 4, or 5 columns at initialize()?
Mac 10.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 167
Joined: Sun Apr 21, 2013 12:55 am

Re: [Solved] Create table with rows & columns of variable si

Postby karolus » Tue Feb 07, 2017 6:20 pm

Hallo

Is this a table of 3 rows with 2, or 4, or 5 columns at initialize()?


Yes, it is a table with 2, 4 or 5 columns at initialize, but propably it starts with any other number of Columns!
AOO4, Libreoffice - 5.1 … 5.3.2.2 on Linux Mint17
User avatar
karolus
Volunteer
 
Posts: 851
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Create table with rows & columns of variable si

Postby _savage » Tue Feb 07, 2017 10:20 pm

karolus wrote:Yes, it is a table with 2, 4 or 5 columns at initialize, but probably it starts with any other number of Columns!

What would be the parameters to initialize() then? And are you suggesting to dynamically insert/delete columns and rows as necessary?
Mac 10.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 167
Joined: Sun Apr 21, 2013 12:55 am

Re: [Solved] Create table with rows & columns of variable si

Postby karolus » Wed Feb 08, 2017 1:11 am

I don't know about all the things you want to do in the doc, but why not insert as unique layouted pieces one after other…eg:

Code: Select all   Expand viewCollapse view
def table_example():
    doc = XSCRIPTCONTEXT.getDocument()
    text = doc.Text

    for i in range
(1,4):
        table = doc.createInstance( "com.sun.star.text.TextTable" )
        table.initialize(i,i)
        text.insertTextContent(text.EndtableTrue)
        # do here some more stuff with every Table
AOO4, Libreoffice - 5.1 … 5.3.2.2 on Linux Mint17
User avatar
karolus
Volunteer
 
Posts: 851
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Create table with rows & columns of variable si

Postby _savage » Wed Feb 08, 2017 1:21 am

karolus wrote:I don't know about all the things you want to do in the doc, but why not insert as unique layouted pieces one after other…

Do you propose to create three tables of one row each, one with 4 columns, one with 2, one with 5, and then to place them below one another as to make the whole thing look like a single table? I think that solution would be more of a visual fake... ;)

Personally, I'd be more interested in a single table with irregular layout.
Mac 10.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 167
Joined: Sun Apr 21, 2013 12:55 am


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 0 guests