Spreadsheet Open Office (Calc) - células de texto Concatenar com delimitadores
-
22-07-2019 - |
Pergunta
Eu estou usando programa de planilha do Open Office e estou tentando concatenar várias células de texto juntamente com os delimitadores. Por exemplo, suponha que eu tenho as células abaixo:
+--------+
| cell 1 |
+--------+
| cell 2 |
+--------+
| cell 3 |
+--------+
| cell 4 |
+--------+
| cell 5 |
+--------+
Gostaria de concatenar-los com delimitadores de modo que o resultado está em uma célula como esta:
+----------------------------------------------+
| (cell 1),(cell 2),(cell 3),(cell 4),(cell 5) |
+----------------------------------------------+
O meu primeiro pensamento foi tentar fazer uma macro ou algo assim, mas eu não acho suportes Office Open aqueles. Alguma idéia?
Solução 2
Bem, depois de muito mais pesquisa e experimentação, eu achei que você pode fazer suas próprias funções no Calc. Esta é uma função que eu fiz que faz o que eu quero:
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
Outras dicas
Muito obrigado Markus para encontrar uma solução para isso.
Aqui estão algumas instruções um pouco mais detalhada para o benefício dos novatos OpenOffice básicos como eu. Isso se aplica a versão 3.1:
Ferramentas -> Macros -> Organizar Macros -> OpenOffice.org Básico ...
Agora, selecione a partir da árvore explorer onde você quer que sua função ao vivo, por exemplo. ele pode estar em sua própria biblioteca de macro (My Macros / padrão) ou armazenados diretamente na planilha atual.
Agora, introduza um novo nome de macro e clique em Novo para abrir a OO.org Básico IDE. Você verá uma REM declaração e algumas definições esboço Sub. Excluir tudo isso e substituir -lo com:
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
O código acima tem algumas ligeiras melhorias de Markus' original:
-
não começa com um delimitador quando a primeira célula no intervalo está vazio.
-
Permite escolha opcional do delimitador (o padrão é ""), ea cordas que vão antes e depois de cada entrada não em branco na gama (Padrão para "").
-
Eu rebatizou-STRJOIN uma vez que "juntar-se" é o nome típico desta função em vários idiomas populares, tais como Perl, Python e Ruby.
-
As variáveis ??em minúsculas
Agora salve a macro, ir para a célula onde deseja que a juntar-se a aparecer, e digite:
=STRJOIN(C3:C50)
substituindo C3:. C50 com a gama de cordas que você quer para se juntar
Para personalizar o delimitador, em vez usar algo como:
=STRJOIN(C3:C50; " / ")
Se você queria se juntar a um grupo de endereços de email, você poderia usar:
=STRJOIN(C3:C50; ", "; "<"; ">")
e o resultado seria algo como
<foo@bar.com>, <baz@qux.org>, <another@email.address>, <and@so.on>
De vez em quando eu desfrutar da facilidade e rapidez de substituir & Opções de cálculo, bem como, em geral, a rápida manipulação e modificação de Opções, quando mais uma vez sentado na frente de um despejado em arquivo-listas ou qualquer.
Eu nunca entendi por que não incluir uma tal função fundamental desde o início, realmente.
É baseado no roteiro de Adam, mas com a extensão para CONCAT
troca de horizontal para vertical, mantendo os delimitadores em ordem.
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