Excel VBA - scripting.dictionary и чтение из переменной (созданной) локации
У меня сложная, многоуровневая структура словаря, содержащая ответ JSON.
Я хочу иметь возможность гибко считывать данные из словаря в таблицу Excel на основе полей, определенных в справочной справочной таблице.
Например, если моя таблица поиска имеет
TARGET_TBL_FIELD DICT_LOCATION
Hostname data.dc.hostname
Main_Contact data.owner.contact1
Я хочу получить JSON, а затем построить таблицу с помощью.
row 1, cell1 = dict.("data")(1)("dc")("hostname")
row 1, cell2 = dict.("data")(1)("owner")("contact1")
row 2, cell1 = dict.("data")(2)("dc")("hostname")
row 2, cell2 = dict.("data")(2)("owner")("contact1")
И продолжайте, пока не останется больше элементов данных, dc и owner всегда будут представлять собой один набор данных.
Во всяком случае, я написал весь код, но я застрял.
Я использую таблицу поиска, чтобы создать строковую переменную Excel с путем к словарю.
For Each ghdLookupRow In ghdLookupTable.ListRows
ssFieldLabel = ghdLookupRow.Range(1, ghdLookupColumns("SS_TITLE").Index).Value
ssFieldSource = ghdLookupRow.Range(1, ghdLookupColumns("GHD_SOURCE").Index).Value
ssFlattenData = ghdLookupRow.Range(1, ghdLookupColumns("FLATTEN_DATA").Index).Value
If ghdRow = 1 Then
' Write header data into array
'qcDataArray(1, 1) = ssFieldLabel
ghdDataArray(c, 0) = ssFieldLabel
End If
' pass the field source string , dictionary object and row number into command to retrieve data
ghdFieldValue = getJsonField(ghdData, ghdRow, ssFieldSource, ssFlattenData)
' Store the data retrieved from the Json
ghdDataArray(c, ghdRow) = ghdFieldValue
'increment column variable
c = c + 1
Next
Где функция getJsonField
Function getJsonField(ghdData As Dictionary, ghdRow As Long, fieldPath As String, flattenData As Boolean) As String
Dim field As Variant, ghdPath As String
ghdPath = "ghdData(" & Chr(34) & "data" & Chr(34) & ")(" & ghdRow & ")"
For Each field In Split(fieldPath, ".", , vbTextCompare)
ghdPath = ghdPath & "(" & Chr(34) & field & Chr(34) & ")"
Next
'The ghdPath string has the correctly structured path for the dictionary location e.g "ghdData("data")(1)("dc")("hostname")"
' How do I "call" the string as if it was a written command
' so how do I retrieve the data?
' If I write the line specifically then it returns
getJsonField = ghdData("data")(1)("dc")("hostname")
'How do I 'evaluate' the string as a command?
getJsonField = ***How.To.Evaluate***(ghdPath)
End Function
Поэтому у меня есть строковая переменная, содержащая { dict.("Data")(1)("dc")("hostname") }.
Я ожидал, что смогу использовать эту переменную для извлечения данных из этого места.
Я не могу понять, как "выполнить" или "вызвать" содержимое переменной как путь к местоположению объекта сценария, чтобы извлечь данные и записать их в соответствующую ячейку.
Это возможно?