Excel - 如何根据同一文本中的条件提取文本中的文本

时间:2016-10-10 15:55:24

标签: excel excel-formula

一直试图解决这个问题,但没有成功。 我试图从Json文件中提取已翻译的数据,并在不同的列中提取不同的翻译。重要的是要知道数据并不总是具有相同的序列。

json看起来像这样:

{“en”:“life”,“nl-BE”:“leven”,“de-DE”:“Leben”,“fr-BE”:“la vie”}

请参阅Excel中的示例我想要实现的目标。Excel example

Column A                                                                 Column B Column C Column D Column E
 Json                                                                    "en"    "nl-BE"  "de-DE"   "fr-BE"
{"en":"life","nl-BE":"leven","de-DE":"Leben","fr-BE":"la vie"}           life    leven    Leben   la vie
{"nl-BE":"Kinderen","de-DE":"Kinder","en":"Children","fr-BE":"Enfants"}  Children Kinderen Kinder Enfants

提前致谢。

2 个答案:

答案 0 :(得分:1)

使用此公式:

=SUBSTITUTE(SUBSTITUTE(TRIM(MID(SUBSTITUTE(SUBSTITUTE($A2,":",REPT(" ",99)),",",REPT(" ",99)),FIND(B$1,SUBSTITUTE(SUBSTITUTE($A2,":",REPT(" ",99)),",",REPT(" ",99)))+99,99)),"""",""),"}","")

将它放在B2副本上下。

enter image description here

答案 1 :(得分:1)

除了@ScottCraner的优秀答案,您还可以在VBA中编写UDF。在标准代码模块中放置:

Function JsonExtractor(json As String, key As String) As String
    Dim data As String
    Dim items As Variant
    Dim item As Variant

    data = Trim(json)
    data = Mid(data, 2, Len(data) - 2) 'strip off {,}
    items = Split(data, ",")
    For Each item In items
        If item Like key & ":*" Then
            item = Split(item, ":")(1)
            item = Mid(item, 2, Len(item) - 2) 'strip off ","
            JsonExtractor = item
            Exit Function
        End If
    Next item
    'raise error if not found:
    JsonExtractor = CVErr(xlErrValue)
End Function

然后将公式=JsonExtractor($A2,B$1)放入B2并复制:

enter image description here