Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Office Spreadsheet (Calc) - Concatenate text cells with delimiters

I am using Open Office's spreadsheet program and am trying to concatenate several text cells together with delimeters. For example, suppose I have the cells below:

+--------+
| cell 1 |
+--------+
| cell 2 |
+--------+
| cell 3 |
+--------+
| cell 4 |
+--------+
| cell 5 |
+--------+

I would like to concatenate them with delimiters so that the result is in one cell like this one:

+----------------------------------------------+
| (cell 1),(cell 2),(cell 3),(cell 4),(cell 5) |
+----------------------------------------------+

My first thought was to try and make a macro or something, but I don't think open office supports those. Any ideas?

like image 901
Markus O'Reilly Avatar asked Dec 01 '09 12:12

Markus O'Reilly


People also ask

How do you CONCATENATE cells in LibreOffice Calc?

There is a simple function “CONCATENATE()” in LibreOffice. Click into the result cell (eg B2). Click on the “Function Wizard”=“fx”. Select the function “CONCATENATE()”.

How do I group Data in open office?

You can access the grouping with the menu entry Data > Group and Outline > Group or by pressing F12. It is important that you select the correct cell area. The grouping function is mainly determined by the type of values that have to be grouped.


2 Answers

Thanks a lot Markus for finding a solution to this.

Here are some slightly more detailed instructions for the benefit of OpenOffice Basic newbies like myself. This applies to version 3.1:

Tools -> Macros -> Organize Macros -> OpenOffice.org Basic...

Now select from the explorer tree where you want your function live, e.g. it can be in your own macro library (My Macros / Standard) or stored directly in the current spreadsheet.

Now enter a new Macro name and click New to open the OO.org Basic IDE. You'll see a REM statement and some stub Sub definitions. Delete all that and replace it with:

Function STRJOIN(range, Optional delimiter As String, Optional before As String, Optional after As String)
    Dim row, col As Integer
    Dim result, cell As String

    result = ""

    If IsMissing(delimiter) Then
        delimiter = ","
    End If
    If IsMissing(before) Then
        before = ""
    End If
    If IsMissing(after) Then
        after = ""
    End If

    If NOT IsMissing(range) Then
        If NOT IsArray(range) Then
            result = before & range & after
        Else
            For row = LBound(range, 1) To UBound(range, 1)
                For col = LBound(range, 2) To UBound(range, 2)
                    cell = range(row, col)
                    If cell <> 0 AND Len(Trim(cell)) <> 0 Then
                        If result <> "" Then
                            result = result & delimiter
                        End If
                        result = result & before & range(row, col) & after
                    End If
                Next
            Next
        End If
    End If

    STRJOIN = result
End Function

The above code has some slight improvements from Markus' original:

  • Doesn't start with a delimiter when the first cell in the range is empty.

  • Allows optional choice of the delimiter (defaults to ","), and the strings which go before and after each non-blank entry in the range (default to "").

  • I renamed it STRJOIN since "join" is the typical name of this function in several popular languages, such as Perl, Python, and Ruby.

  • Variables all lowercase

Now save the macro, go to the cell where you want the join to appear, and type:

  =STRJOIN(C3:C50)

replacing C3:C50 with the range of strings you want to join.

To customise the delimiter, instead use something like:

  =STRJOIN(C3:C50; " / ")

If you wanted to join a bunch of email addresses, you could use:

  =STRJOIN(C3:C50; ", "; "<"; ">")

and the result would be something like

<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>
like image 166
Adam Spiers Avatar answered Sep 20 '22 05:09

Adam Spiers


Well, after a lot more searching and experimenting, I found you can make your own functions in calc. This is a function I made that does what I want:

Function STRCONCAT(range)
    Dim Row, Col As Integer
    Dim Result As String
    Dim Temp As String

    Result = ""
    Temp = ""

    If NOT IsMissing(range) Then
        If NOT IsArray(range) Then
            Result = "(" & range & ")"
        Else
            For Row = LBound(range, 1) To UBound(range, 1)
                For Col = LBound(range, 2) To UBound(range, 2)
                    Temp = range(Row, Col)
                    Temp = Trim(Temp)
                    If range(Row, Col) <> 0 AND Len(Temp) <> 0 Then
                        If(NOT (Row = 1 AND Col = 1)) Then Result = Result & ", "
                        Result = Result & "(" & range(Row, Col) & ") "
                    End If
                Next
            Next
        End If
    End If

    STRCONCAT = Result
End Function
like image 27
Markus O'Reilly Avatar answered Sep 19 '22 05:09

Markus O'Reilly