Merging two columns of table cells but preserve rows

Hello. I hope you are in good health.

I’m using LibreOffice 7.6.2.1. Using a Macbook Air with OS 14.x.x.

This is about LibreOffice Writer only.

I have a document in which there is a three-column table with many rows. I would like to merge the cells in columns 2 and 3 but maintain the rows.

With my current knowledge, I will have to proceed row by row, select the column 2 and 3 cells in that row, and doing “Merge cells”, repeating this for each row.

Is there a way to automate this?

Thank you.

I don’t know what it’s called, just learned this myself an hour or so ago…

If you want D1 to read “A1 B1 C1,” then in D1 try this: =A1 & " " & B1 & " " & C1
The " " is just putting a space between them.

I appreciate your help. I need to edit the original question to specify LibreOffice Writer.

Hallo

def merge_BC_Row_wise():
    doc = XSCRIPTCONTEXT.getDocument()
    table = doc.TextTables[0] #the first Table
    for cellname in table.CellNames:
        if cellname.startswith("B"):
            cursor = table.createCursorByCellName( cellname )
            cursor.goRight(1, True)
            cursor.mergeRange()

of course its python, propably you’ll need apso.oxt from here
the version for hardliners:

def merge_BC_Row_wise():
    doc = XSCRIPTCONTEXT.getDocument()
    table = doc.TextTables[0]
    for name in table.CellNames[1::3]: # takes any third cell starting from second
        cursor = table.createCursorByCellName(name)
        cursor.goRight(1, True)
        cursor.mergeRange()

I appreciate your response.

Do I need to change values in the lines of code? Example: do I need to type a document name in the parentheses in “doc = XSCRIPTCONTEXT.getDocument()”?

I really don’t know what to do with this code.

I tried to run it as written, as a macro. I got the warning “BASIC syntax error.
Unexpected symbol: 0.”

Can you guide me to instructions that are clear to a novice?

Thank you.

No it points already to the currently active Document.

Thanks again.
I did download and install the apso.oxt thing. The apso.oxt console is what I used a couple times.
With either script, when I pasted it into the console and pressed “return”, I got

def merge_BC_Row_wise():
doc = XSCRIPTCONTEXT.getDocument()
table = doc.TextTables[0] #the first Table
for cellname in table.CellNames:
if cellname.startswith(“B”):
cursor = table.createCursorByCellName( cellname )
cursor.goRight(1, True)
cursor.mergeRange()
File “”, line 1
cursor.mergeRange()
^
IndentationError: unexpected indent

I messed around with the indent of the last line, but it made no difference. ¿Qué pasa?
Again, thank you.

In the console copy&paste and entereach of the following lines seperatly:

doc = XSCRIPTCONTEXT.getDocument()
table = doc.TextTables[0] #the first Table
for cellname in table.CellNames:
    if cellname.startswith("B"):
        cursor = table.createCursorByCellName( cellname )
        cursor.goRight(1, True)
        cursor.mergeRange()

exactly as you see with the indentation, until the console looks exactly:


now put the Cursor behind the last line and hit enter twice

1 Like

The simplest way to do this is to use the Table to text tool and then the Text to table tool.

1 Insert a new column directly after the first column.

2 Paste a character that does not appear in your table in every cell in the new column. (In my test, I used @).

3 Select Convert > Table to text. In the Separate Text At panel, select other and enter a space — i.e. hit the space bar once, then OK. This will create list of your data.

4 Select the list. Select Convert > Text to table. In the Separate Text At pane, select other and type in your selected character (@).

This will produce the desire result. Column 1 will contain the data from your original column 1 and column 2 will contain the data from the original column 2 separated by a space from the data from column 3.

This process will also introduce a couple of additional spaces in each row. If you need to remove them you can use the Find and Replace tool

In the Find entry, type the following

\s+(\r?(\n|$))

Note: the character between the lowercase n and the dollar sign, $, is a vertical bar or pipe symbol |.

On my keyboard, I need to shift the back-slash key to use it. I checked and found its position differs on other keyboards, so you may have to search for this symbol.

In the Replace entry type, $1.

Click Replace All and Close.

I hope this helps

Thank you. This is interesting.
Do you know a way I can automate pasting the selected symbol (in your example, the @ symbol) into each cell of the new column?

To paste the @ symbol into ever cell of the column, copy the symbol into the clip board.
Hover the mouse of the the top of the column until a black down arrow appears. Left click to select the entire column. Hold down the control key and press letter V. The symbol will be pasted into every cell in the column.

1 Like

I made a mistake when I stated my problem. It is not a 3-column table; it is actually a 4-column table.
I have two tables, and they are identical in number of columns and all format characteristics.

It’s the oddest thing! In one table, I applied your protocol and it worked perfectly.
In the other table, it just won’t work! I end up with the correct number of columns, but almost all the data are crammed into the first column.

I’m working on this… Must be some hidden difference between the tables?

You better had uploaded your tables instead to mark your question as solved - which is not really true.
.
:frowning_face:

Check in stage 3 that you have a space in the Convert table to text in the Separate at entry. As you can’t see it, the space character is easily missed.

Thank you.
I double-checked that I was inserting a space character into the “Other” box. Still having the same problem.
I need to mess around with it a while…
Thanks.