Ask Your Question

Is there a way to add a row to an already created table?

asked 2020-03-17 05:20:25 +0100

khjf3 gravatar image


I am testing the SDK example files.

There is a code that creates a new table and puts values ​​in it. (

But what I want is to load an already created document and control the table there.

Is there a way to add rows and data to an already created table rather than a new one using java code?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2020-03-17 11:12:12 +0100

Lupp gravatar image

updated 2020-03-17 11:46:18 +0100

Whatever you want to do with a TextTable object in Writer by user code:
TextTable objects are a mess, and you have to expect problems of many kinds.
This starts with the fact that there are "complex tables" containing merged ranges and or split cells. They don't have a clear concept of rows and columns.
I also dont know an interface/service provided by the API for the insertion of rows/columns. The only way I can think of is to use the dispatcher simulating user interaction of the kind @edcdeee suggested. See provisional code below.

Sub insRowsAboveRangeIntoTextTable(Optional pDoc As Object, Optional pTextTable As Object, Optional pRangeName As String)
If IsMissing(pDoc) Then pDoc = ThisComponent

' <onlyfortesting>
If IsMissing(pTextTable) Then pTextTable = pDoc.TextTables(0)
If IsMissing(pRangeName) Then pRangeName = "A5:B7"  REM Case sensitive for TextTable!
' </onlyfortesting>

If InStr(pRgName, ":")=0 Then pRgName = pRgName &":"& pRgName REM Assuming pRgName was a cell name.
theRg = pTextTable.getCellRangeByName(pRangeName)

cCtrl = pDoc.CurrentController
dispatchPr = cCtrl.Frame
dispatchHe = createUnoService("")

oldSel = pDoc.CurrentSelection
dispatchHe.executeDispatch(dispatchPr, ".uno:InsertRowsBefore", "", 0, Array())
If hasFailed Then MsgBox("Attempted insertion of rows failed.")
End Sub

For simple ranges you then can manipulate the content of the cells one by one, or -concerning simple content- the DataArray using code like

simpleDA = simpleRg.getDataArray()
uR = Ubound(theDA)
uC = Ubound(theDA(0))
For r = 0 To uR
  For c = 0 To uC
    simpleDA(r)(c) = myNewValue()
  Next c
Next r

Note: Even simple content can have more than one paragraph (U+000D inserted) and hard linebreaks (U+000A). To get complex formatting you need to work on the single cells addressing them the XText way.

edit flag offensive delete link more


Thanks, the row is added well.

khjf3 gravatar imagekhjf3 ( 2020-03-18 01:19:35 +0100 )edit

answered 2020-03-17 10:19:44 +0100

edcdeee gravatar image

If you go to Table/Insert you can insert rows above and below.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-03-17 05:20:25 +0100

Seen: 57 times

Last updated: Mar 17 '20