2016-08-03 156 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