我在Excel中有这样的数据:
Duration:
PT7M37S
PT9M30S
PT2M16S
PT4M22S
PT31S
PT15S
PT3M20S
PT58S
PT27M50S
但是,我想把它转换为457(7 * 60 + 37)秒。有没有简单的方法可以做到这一点,因为我有22,000个这样的样本?
答案 0 :(得分:3)
我有一个类似于斯科特的不完全考虑过的解决方案。我看到他发布了一个很好的配方解决方案。
这是一个UDF,也可以使用。它使用正则表达式来查找适当的分钟和秒,然后将它们组合起来。
Option Explicit
Function ExtrSec(S As String) As Long
Dim RE As Object, MC As Object
Const sM As String = "\d+(?=M)"
Const sS As String = "\d+(?=S)"
Dim L As Long
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.ignorecase = False
.Pattern = sM
If .test(S) = True Then
Set MC = .Execute(S)
L = MC(0) * 60
End If
.Pattern = sS
If .test(S) = True Then
Set MC = .Execute(S)
L = L + MC(0)
End If
End With
ExtrSec = L
End Function
正则表达式对此可能有些过分,您可以使用所有本机VBA函数完成所请求的内容:
Function ExtrSec2(ByVal value As String) As Variant
Dim M As Long, S As Long
Dim locM As Long, locS As Long
value = Mid(value, 3)
locM = InStr(value, "M")
locS = InStr(value, "S")
'We do not check specifically for the presence of PT
' at the beginning, but we could, if necessary
If locM + locS = 0 Then
ExtrSec2 = CVErr(xlErrValue)
Exit Function
End If
If locM > 0 Then
M = Val(value) * 60
value = Mid(value, locM + 1)
End If
S = Val(value)
ExtrSec2 = M + S
End Function
答案 1 :(得分:3)
这会创建和时间字符串,并将其乘以一天中的秒数。 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourApp.Views.YourPage"
x:Name="YourPageName">
<ContentPage.Content>
<StackLayout>
<ListView ItemsSource="{Binding OtherItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0,0" Padding="0,0" ColumnSpacing="0" RowSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="85*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding Source={x:Reference YourPageName}, Path=BindingContext.oRowHeight.Height}" />
</Grid.RowDefinitions>
<!-- Like the rest of XAML above -->
变为PT
,00:
变为M
,我们删除了:
。最终结果类似S
,Excel可以解析为实际时间
如果找不到00:07:37
,我们会在进行替换之前将M
放入字符串中,以确保我们将秒数放在它们所属的位置:
0M
答案 2 :(得分:1)
这是一个Excel公式,用于构建所需的公式:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"PT","="),"M","*60" & IF(RIGHT(A1,1)="S","+","")),"S","")
这将为您提供文本=7*60+37
,但现在的问题是如何将其作为公式执行。遗憾的是,除了使用仅在VBA中可用的EVALUATE()
函数之外,我不知道以何种方式执行文本作为公式。您可以添加模块并将此功能粘贴到其中:
Function Eval(ByVal value As String) As Integer
Application.Volatile
Eval = Evaluate(value)
End Function
现在,您可以使用它来执行公式:
=Eval(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"PT",""),"M","*60" & IF(RIGHT(A1,1)="S","+","")),"S",""))
或者,您可以在VBA中执行整个计算:
Function CalcSeconds(ByVal value As String) As Integer
value = Replace(value, "PT", "")
value = Replace(value, "M", "*60" + IIf(Right(value, 1) = "S", "+", ""))
CalcSeconds = Evaluate(Replace(value, "S", ""))
End Function
现在你的公式就是:
=CalcSeconds(A1)
编辑要添加小时计算:
Function CalcSeconds(ByVal value As String) As Long
Dim ending As String
ending = Right(value, 1)
value = Replace(value, "PT", "")
value = Replace(value, "H", "*3600" + IIf(ending = "M" Or ending = "S", "+", ""))
value = Replace(value, "M", "*60" + IIf(ending = "S", "+", ""))
CalcSeconds = Evaluate(Replace(value, "S", ""))
End Function
答案 3 :(得分:0)
假设您的样本位于单元格A1中:
=LEFT(RIGHT(A1,LEN(A1)-2),LEN(RIGHT(A1,LEN(A1)-2))-FIND("M",A1))*60+LEFT((RIGHT(A1,FIND("M",A1)-1)), LEN((RIGHT(A1,FIND("M",A1)-1)))-1)