Open Office Spreadsheet (Calc) - 使用分隔符连接文本单元格

发布于 2024-08-12 20:04:26 字数 480 浏览 2 评论 0原文

我正在使用 Open Office 的电子表格程序,并尝试将多个文本单元格与分隔符连接在一起。例如,假设我有下面的单元格:

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

我想将它们与分隔符连接起来,以便结果位于像这样的一个单元格中:

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

我的第一个想法是尝试制作一个宏或其他东西,但我不认为打开办公室支持这些。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-08-19 20:04:26

非常感谢马库斯找到了解决方案。

这里有一些稍微更详细的说明,以帮助像我这样的 OpenOffice Basic 新手。这适用于版本 3.1:

工具 ->宏->组织宏 -> OpenOffice.org Basic...

现在从资源管理器树中选择您想要运行函数的位置,
例如,它可以位于您自己的宏库中(我的宏/标准)或
直接存储在当前电子表格中。

现在输入新的宏名称并单击新建以打开 OO.org Basic IDE。你会看到 REM
语句和一些存根子定义。全部删除并替换
与 Markus 的原始代码相比

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

上面的代码

  • 有一些细微的改进:

    当范围中的第一个单元格为空时,不以分隔符开头。

  • 允许可选地选择分隔符(默认为“,”)和
    范围内每个非空白条目之前和之后的字符串
    (默认为“”)。

  • 我将其重命名为 STRJOIN,因为“join”是它的典型名称
    函数在几种流行语言中,例如 Perl、Python 和 Ruby。

  • 变量全部小写

现在保存宏,转到要显示连接的单元格,
并键入:

  =STRJOIN(C3:C50)

将 C3:C50 替换为您要连接的字符串范围。

要自定义分隔符,请使用以下内容:

  =STRJOIN(C3:C50; " / ")

如果您想加入一堆电子邮件地址,您可以使用:

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

结果将类似于

<[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>

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]>
流云如水 2024-08-19 20:04:26

好吧,经过大量的搜索和实验,我发现你可以在 calc 中创建自己的函数。这是我制作的一个函数,可以实现我想要的功能:

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

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
屋檐 2024-08-19 20:04:26

我经常享受更换和更换的轻松快捷。计算选项以及一般的快速处理和计算选项修改选项,当再次坐在转储文件列表或任何东西前面时。

我真的不明白为什么他们从一开始就不包含如此重要的功能。

它基于 Adam 的脚本,但具有将 CONCAT 从水平交换为垂直的扩展,同时仍保持分隔符的顺序。

Function CONCAT2D(Optional range,   Optional delx As String, Optional dely As String, _
                                    Optional xcell As String, Optional cellx As String, _
                                    Optional swop As Integer)
    Dim xy(1), xyi(1), s(1) As Integer
    Dim out, cell, del, dxy(1) As String

    'ReDim range(2, 1)           'Gen.RandomMatrix 4 Debugging
    'For i = LBound(range, 1) To UBound(range, 1)
    '   For j = LBound(range, 2) To UBound(range, 2)
    '       Randomize
    '       range(i,j) = Int((100 * Rnd) )
    '   Next
    'Next

    out  = ""
    If IsMissing(delx)  Then : delx  = ","      : End If
    If IsMissing(dely)  Then : dely  = delx()   : End If
    If IsMissing(xcell) Then : xcell = ""       : End If
    If IsMissing(cellx) Then : cellx = xcell()  : End If
    If IsMissing(swop)  Then : swop  = 0        : End If
    dxy(0) = delx() : dxy(1) = dely()
    xyi(0) = 1      : xyi(1) = 2
    If swop = 0     Then :  s(0) = 0 : s(1) = 1
                    Else    s(0) = 1 : s(1) = 0 : End If

    If NOT IsMissing(range) Then
        If   NOT IsArray(range) _
        Then :  out = xcell & range & cellx
        Else    del = delx
                For xy(s(0)) = LBound(range, xyi(s(0))) To UBound(range, xyi(s(0))
                For xy(s(1)) = LBound(range, xyi(s(1))) To UBound(range, xyi(s(1))
                    cell = range(xy(0), xy(1))
                    If cell <> 0 AND Len(Trim(cell)) <> 0 _
                    Then :  If out <> "" Then : out = out & del : End If
                            out = out & xcell & cell & cellx
                            del = dxy(s(0))
                    End If
                Next :      del = dxy(s(1))
                Next
        End If
    Else        out = "ERR"
    End If

    CONCAT2D = out
End Function

Ever so often I'd enjoy the ease and quickness of replace & calculation Options as well as in general the quick handling & modifying Options, when once again sitting in front of a dumped-file-lists or whatsoever.

I never understood why they didn't include such an essential function right from the start, really.

It's based on Adam's script, but with the extension to swap CONCAT from horizontal to vertical, while still keeping the delimiters in order.

Function CONCAT2D(Optional range,   Optional delx As String, Optional dely As String, _
                                    Optional xcell As String, Optional cellx As String, _
                                    Optional swop As Integer)
    Dim xy(1), xyi(1), s(1) As Integer
    Dim out, cell, del, dxy(1) As String

    'ReDim range(2, 1)           'Gen.RandomMatrix 4 Debugging
    'For i = LBound(range, 1) To UBound(range, 1)
    '   For j = LBound(range, 2) To UBound(range, 2)
    '       Randomize
    '       range(i,j) = Int((100 * Rnd) )
    '   Next
    'Next

    out  = ""
    If IsMissing(delx)  Then : delx  = ","      : End If
    If IsMissing(dely)  Then : dely  = delx()   : End If
    If IsMissing(xcell) Then : xcell = ""       : End If
    If IsMissing(cellx) Then : cellx = xcell()  : End If
    If IsMissing(swop)  Then : swop  = 0        : End If
    dxy(0) = delx() : dxy(1) = dely()
    xyi(0) = 1      : xyi(1) = 2
    If swop = 0     Then :  s(0) = 0 : s(1) = 1
                    Else    s(0) = 1 : s(1) = 0 : End If

    If NOT IsMissing(range) Then
        If   NOT IsArray(range) _
        Then :  out = xcell & range & cellx
        Else    del = delx
                For xy(s(0)) = LBound(range, xyi(s(0))) To UBound(range, xyi(s(0))
                For xy(s(1)) = LBound(range, xyi(s(1))) To UBound(range, xyi(s(1))
                    cell = range(xy(0), xy(1))
                    If cell <> 0 AND Len(Trim(cell)) <> 0 _
                    Then :  If out <> "" Then : out = out & del : End If
                            out = out & xcell & cell & cellx
                            del = dxy(s(0))
                    End If
                Next :      del = dxy(s(1))
                Next
        End If
    Else        out = "ERR"
    End If

    CONCAT2D = out
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文