如何在单元格中查找序列号,并用范围替换它们? 例如:
改变:
1,3,5,15,16,17,25,28,29,31 ......
为:
... 1,3,5,15-17,25,28-29,31
数字已经排序,即按升序排列。
感谢。
答案 0 :(得分:4)
我想看一个有趣的问题,不需要循环遍历序列(首先需要排序)检查顺序构建
此功能
ref.child("users").child(userID!).child("signal_received").observeSingleEvent(of: .value, with: { (snapshot) in
if(snapshot.value as! String == "true"){
self.view.makeToast("Signal received")
snapshot.ref.setValue("true")
}
}) { (error) in
}
将连续的行组合在一起循环不是必需的,更短的版本!
Union
答案 1 :(得分:2)
我想尝试使用 Microsoft365 的 LET()
作为捕获变量的方法的全公式解决方案。
以下解决方案仅将 3 个以上的连续数字视为数字范围,而不是两个。
B1
中的公式:
=LET(X,FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s"),Y,TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s[preceding::*[1]+1=.][following::*[1]-1=.]")),SUBSTITUTE(TEXTJOIN(",",,FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,IF(MMULT(--(X=Y),SEQUENCE(COUNTA(Y),,,0)),"-",X))&"</s></t>","//s[.*0=0 or (.='-' and preceding::*[1]*0=0)]")),",-,","-"))
答案 2 :(得分:0)
虽然基于给定范围/区域的答案很有意思,但它存在一些缺陷:
这是一个基于数组循环的基本方法。它可以处理长字符串。在我的测试中,它大约运行1/3。它还有不要求对输入进行排序的好处
Function NumOut2(strIn As String) As String
Dim arrIn() As String
Dim arrBuckets() As Long
Dim i As Long
Dim InRange As Boolean
Dim mn As Long, mx As Long
arrIn = Split(strIn, ", ")
mn = arrIn(0)
mx = arrIn(0)
For i = 1 To UBound(arrIn)
If arrIn(i) < mn Then
mn = arrIn(i)
ElseIf arrIn(i) > mx Then
mx = arrIn(i)
End If
Next
ReDim arrBuckets(mn To mx)
For i = 0 To UBound(arrIn)
arrBuckets(arrIn(i)) = arrIn(i)
Next
NumOut2 = LBound(arrBuckets)
InRange = False
For i = LBound(arrBuckets) + 1 To UBound(arrBuckets)
If arrBuckets(i) > 0 Then
If arrBuckets(i) = arrBuckets(i - 1) + 1 Then
If InRange Then
Else
InRange = True
NumOut2 = NumOut2 & "-"
End If
Else
If InRange Then
NumOut2 = NumOut2 & arrBuckets(i - 1) & ", " & arrBuckets(i)
Else
NumOut2 = NumOut2 & ", " & arrBuckets(i)
End If
End If
Else
If InRange Then
NumOut2 = NumOut2 & arrBuckets(i - 1)
End If
InRange = False
End If
Next
End Function