Advertisement
Johniny

Function to get config from txt

Mar 10th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/declaring-arrays#declare-a-dynamic-array
  2. 'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/redim-statement -- can be used with UBound to set array upper bound!
  3.  
  4. '#
  5. '---------------------------------------------------------------------------------------------------------------------------
  6. '#
  7.  
  8. Sub somethingConfig()
  9. Dim output As Variant
  10. Dim myPath As String
  11. myPath = "C:\Users\Johny\Desktop\tréning\VBA\advanced_config.txt"
  12.  
  13. output = FetchConfig("fuck", myPath, "nice")
  14. If IsArray(output) Then
  15.     MsgBox "Test: " & Join(output, (vbNewLine & "test: "))
  16. Else
  17.     MsgBox "Test: " & output
  18. End If
  19.  
  20. End Sub
  21.  
  22. '#
  23. '---------------------------------------------------------------------------------------------------------------------------
  24. '#
  25.  
  26. 'https://stackoverflow.com/questions/2781689/how-to-return-a-result-from-a-vba-function
  27. Public Function FetchConfig(filterKey As String, Optional configPath As String, Optional ifNothingFound As String, Optional forceUpdateFromFile As Boolean, Optional needHelp As Boolean) As Variant
  28. 'filterKey -> ktore hodnoty (podla textu pred #) chces
  29. 'configPath -> kde sa nachadza subor s nastaveniami (a ako sa vola) //prednastavene chcem dat na ThisWorkbookFolder & "\config.txt"
  30. 'ifNothingDound -> co ma napisat ak nenajde ani jednu hodnotu podla FilterKey
  31. 'forceUpdateFromFile -/-> stiahne vsetky data nanovo i ked uz boli raz pocas makra stiahnute, prednastavene na False
  32. 'needHelp -/-> napise MsgBox s tymito komentarmi a vypne funkciu (a idealne i cele makro)
  33.  
  34. Dim configWhole As String
  35. Dim configArr As Variant
  36. Dim tempDivide As Variant
  37. Dim tempStr As String
  38. Dim configValue() As Variant 'Key-Value pairs (google it)
  39. Dim configKey As Variant
  40. Dim configOutput As Variant
  41. Dim i As Long 'i, j a k su len na pocitanie (opakovani a ine pomocne vypocty)
  42. Dim j As Long
  43. Dim k As Long
  44. i = 0
  45. j = 0
  46. k = 0
  47.  
  48. 'basic cesta ak nevyplnene
  49. If ((configPath = "") Or IsEmpty(configPath) Or (configPath = "0")) Then
  50.     configPath = "C:\Users\Johny\Desktop\tréning\VBA\advanced_config.txt"
  51. End If
  52.  
  53. 'musim pridat: If IsEmpty(globalConfigArr) Or forceUpdateFromFile = TRUE Then ... dole open ... Else ... pouzi hodnoty z globalConfigArr //musim samozrejme pridat globalConfigArr
  54. 'pridat kontrolu ci nie je globalConfigArr prazdny a ak ano, daj MsgBox o chybe a nacitaj nanovo zo suboru
  55. 'netusim ako funguje Open+Input+Close :(, viac info na https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/line-inputstatement
  56. Open configPath For Input As #1
  57. configWhole = Input$(LOF(1), 1)
  58. Close #1
  59. configArr = Split(configWhole, vbNewLine) 'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/split-function
  60.  
  61. 'musim najprv z nich vytvorit array
  62. ReDim configKey(0 To UBound(configArr))
  63. ReDim configValue(0 To UBound(configArr))
  64.  
  65. 'teraz napisat Do-While kde Spit configArr pomocou # do tempDivide
  66. 'nasledne configKey(i) = tempDivide(1) a configValue(i) = tempDivide(2)
  67. Do While (i < (UBound(configArr) + 1) And i < 500)
  68.     tempStr = configArr(i)
  69.     tempDivide = Split(tempStr, "#", 2)
  70.     Debug.Print tempDivide(0) & " " & tempDivide(1)
  71.     configKey(i) = tempDivide(0)
  72.     configValue(i) = tempDivide(1)
  73.     Debug.Print configKey(i) & " " & configValue(i)
  74.     i = i + 1
  75. Loop
  76.  
  77. 'potrebna priprava k mojmu "filtru"
  78. i = 0
  79. j = 0
  80.  
  81. 'len ak aspon jeden Key splna FilterKey
  82. 'If Not IsEmpty(Filter(configKey, filterKey)) Then --staré
  83. If Len(Join(Filter(configKey, filterKey))) > 0 Then 'musí mat aspon nejaky znak
  84.    'mozno by sa sem hodil Debug.Print?
  85.    ReDim configOutput(0 To (UBound(Filter(configKey, filterKey)))) As Variant
  86. Else
  87.     'ReDim configOutput() As String --neviem preco nejde, mozno nie je potrebne
  88.    configOutput = ifNothingFound
  89. End If
  90.  
  91. 'improvizovany "filter", ktory vyberie len tie hodnoty, ktore chcem a da ich zaradom do array
  92. Do While (i < (UBound(configKey) + 1) And i < (UBound(configValue) + 1))
  93.     If configKey(i) = filterKey And configValue(i) <> "" Then
  94.         ReDim Preserve configOutput(0 To j) 'bude sa prisposobovat velkosti zatial najdenych poloziek
  95.        configOutput(j) = configValue(i)
  96.         j = j + 1
  97.     End If
  98.     i = i + 1
  99. Loop
  100.  
  101. 'Musel som nejak zabranit chybe v Debug-Printe ked Output obsahuje String a nie array :(
  102. If Len(Join(Filter(configKey, filterKey))) > 0 Then
  103.     Debug.Print "Fn: " & Join(configOutput, vbNewLine) & "End Fn"
  104. Else
  105.     Debug.Print "Fn: " & configOutput
  106. End If
  107.  
  108. FetchConfig = configOutput
  109.  
  110. End Function
  111.  
  112. '#
  113. '---------------------------------------------------------------------------------------------------------------------------
  114. '#
  115.  
  116. 'source of inspiration
  117. Sub ReadFromFile()
  118.  
  119. Dim configFile As String
  120. Dim configWhole As String
  121. Dim configArr As Variant 'nebude to fungovat so String https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/data-type-summary
  122.  
  123. configFile = Application.ActiveWorkbook.Path & "\config.txt" 'Application.ActiveWorkbook.Path vráti cestu do zložky aktívneho workbooku
  124.    'alternativa Application.ThisWorkbook.Path vráti cestu ku zložke v ktorej je súbor s týmto makrom
  125. Open configFile For Input As #1 'netusim ako funguje Open+Input+Close :(, viac info na https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/line-inputstatement
  126. configWhole = Input$(LOF(1), 1)
  127. Close #1
  128. configArr = Split(configWhole, vbNewLine) 'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/split-function
  129.  
  130. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement