2016-08-03 159 views
0

我有一些YAML文件,我需要使用MS Excel宏在Excel中填充这些数据。我能够读取YAML文件并尝试逐行读取并查找语义。但是这个过程变得越来越复杂。我正在寻找替代解决方案。YAML解析器的Excel VBA

Excel VBA是否有YAML解析器?如果是这样,你能建议少数?我需要这个Hash格式的YAML,这样我就可以使用Hash格式的哈希来访问YAML数据了?

感谢 杰文

+1

最有可能不是。如果你使用32位,那么通过使用scriptcontrol,你可以尝试利用JS-YAML库。更健壮和更灵活的方式将是C#Com DLL。但无论如何,没有任何准备就绪,所以你必须自己写。 – cyboashu

+0

如果是这样,我怎么写一个解析器?我应该使用什么工具来编写解析器?我应该用什么语言来创建它,以及如何导入到excel vba中? – Jeevan

+1

我没有任何YAML,但看到这个链接http://ashuvba.blogspot.com/2014/09/json-parser-in-vba-browsing-through-net-net.html?m=1,因为它使用scriptcontrol来创建json解析器在Vba – cyboashu

回答

0

我试图找到一个准备使用的解决方案,并没有发现。 我做了一些可用的东西。它不是纯粹的YAML解释器,但可以解析键值数据。 YAML文件的

功能VBA ParseYAML

Sub ParseYAML() 
Dim myFile As String, text As String, textline As String 
' open YAML file 
myFile = Application.GetOpenFilename() 
' verify if a file were open 
If Not myFile = "Falsch" Then 
    Open myFile For Input As #1 
    Dim dataArray 
    Dim c As Collection 
    Set c = New Collection 
    Line = 0 
    Do Until EOF(1) 
     Line Input #1, textline 
     oneline = Replace(textline, " ", "") 
     dataArray = Split(oneline, ":", 2) 
     sizeArray = UBound(dataArray, 1) - LBound(dataArray, 1) + 1 
     ' Verification Empty Lines and Split don't occur 
     If Not textline = "" And Not sizeArray = 0 Then 
      Data = dataArray(1) 
      Key = dataArray(0) 
      ' test if line don't start with - 
      If InStr(1, Key, "-") = 0 Then 
       c.Add Data, Key 
      End If 
      ' just for debug 
      Line = Line + 1 
      'text = text & textline 
     End If 
    Loop 
    Close #1 

    Range("D6").Value = c.Item("key1") 
    Range("D7").Value = c.Item("key2") 
    Range("C18").Value = c.Item("key3") 
    Set c = Nothing 
End If 
End Sub 

- SECTION1:
KEY1:DATA1
KEY2:DATA2
- 第2节:
KEY3: data3