Новости:

Интересные и полезные статьи по работе с Excel и VBA
можно найти в разделе ХИТРОСТИ

Главное меню

Вложенный массив для JSON

Автор Alexis7z, 23.12.2019, 10:44:39

« назад - далее »

Alexis7z

Привет.

Есть макрос из статьи https://habr.com/ru/post/467127/, который преобразует данные из Excel в JSON.

У меня возникли небольшие трудности с преобразованием вложенного массива данных.

Не могу понять как мне пометить секцию входных\выходных данных чтобы в JSON был вложенный массив с одним или несколькими элементами как на этом скриншоте жирным шрифтом?
https://hsto.org/getpro/habr/conversation/133/adb/d5b/133adbd5b8dbacc4faa09884e28232e7.jpg

I, I2 ... – Секция входных данных, поддерживает хранение моделей данных любой вложенности, цифра справа от I отвечает за уровень вложенности.

O, O2 ... — Секция выходных параметров, они будут сравнены с теми, что возвращает response.
Возможно данный макрос этого не поддерживает, я не знаком с программированием, подскажите как это сделать?

Код макроса:
[spoiler]
Sub collectDataFortransform()

For Each sh In ThisWorkbook.Sheets

    Worksheets(sh.Name).Activate

    Dim jsonFileObject As New FileSystemObject
    Dim jsonFileExport As TextStream
    Dim i, k As Integer
   
    Dim json As New Collection
    Dim jsonRequestsForTC As New Collection
   
    Dim lLastRow As Integer
    lLastRow = 0
   
    Dim lLastCollumn As Integer
    lLastCollumn = 0
    Dim TC_line As Integer
   
    lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
   
    Dim RequestNumber As Integer
    RequestNumber = 1
    Dim numberOfRequests
    numberOfRequests = 0
   
    Dim MassivR() As Integer
    ReDim MassivR(0)
   
    For i = 1 To lLastRow
        If Cells(i, 1) = "TC" Then
            TC_line = i
            lLastCollumn = Cells(i, Columns.Count).End(xlToLeft).Column
        End If
       
        If Cells(i, 1) = "R" Then
            numberOfRequests = numberOfRequests + 1
            If (UBound(MassivR) - LBound(MassivR) + 1 < numberOfRequests) Then
                ReDim Preserve MassivR(UBound(MassivR) - LBound(MassivR) + 1)
            End If
            MassivR(UBound(MassivR)) = i
        End If
    Next i
   
    For k = 3 To lLastCollumn
        Set jsonRequestsForTC = Nothing
        For i = 0 To (UBound(MassivR) - LBound(MassivR) - 1)
           jsonRequestsForTC.Add CollectRequest(MassivR(i), MassivR(i + 1), CInt(k))
        Next i
        jsonRequestsForTC.Add CollectRequest(MassivR(i), lLastRow, CInt(k))
        json.Add jsonRequestsForTC
    Next k
   
    Debug.Print (sh.Name)

    Set jsonFileExport = jsonFileObject.CreateTextFile(ThisWorkbook.Path + "\" + Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5) + sh.Name + ".json", True)
    jsonFileExport.WriteLine (JsonConverter.ConvertToJson(json, Whitespace:=3))

    Set json = Nothing
    Set jsonRequestsForTC = Nothing

Next

End Sub

Function CollectRequest(First As Integer, Last As Integer, TC As Integer) As Dictionary

Dim Key
Dim PrevKey
Dim ArrayIndex

Dim jsonDictionary As New Dictionary
Dim jsonDictionary_inner_check As New Dictionary

Dim DataArray() As String
ReDim DataArray(0, 0)

Dim numberOfTheTypes
Dim numberOfTheData
Dim numberOfTheData_inner
Dim arrI, arrK
Dim exist

numberOfTheTypes = 0
numberOfTheData = 0
numberOfTheData_inner = 0

If (Cells(Last, 1) = "R") Then
    Last = Last - 1
End If

For i = First To Last
    Key = Cells(i, 1)
    If (Key = "PI" Or Left(Key, 1) = "I" Or Left(Key, 1) = "O" Or Key = "PO" Or Key = "R" Or Key = "PC" Or Key = "H") Then
        If Left(Key, 1) <> Left(PrevKey, 1) Then
            numberOfTheData_inner = 0
        End If
       
        numberOfTheData_inner = numberOfTheData_inner + 1
       
        If ((Left(Key, 1) <> Left(PrevKey, 1)) And ((Left(Key, 1) = "I") Or (Left(Key, 1) = "O")) Or (Key <> PrevKey) And (Not ((Left(Key, 1) = "I") Or (Left(Key, 1) = "O")))) Then
            numberOfTheTypes = numberOfTheTypes + 1
        End If
       
        If numberOfTheData < numberOfTheData_inner Then
            numberOfTheData = numberOfTheData_inner
        End If
        PrevKey = Key
    End If
   
Next i

If numberOfTheData < numberOfTheData_inner Then
            numberOfTheData = numberOfTheData_inner
End If
           
numberOfTheData_inner = 0
       
If numberOfTheTypes = 0 Then
    numberOfTheTypes = 1
End If

If numberOfTheData = 0 Then
    numberOfTheData = 1
End If

ReDim DataArray(numberOfTheTypes - 1, numberOfTheData)

arrI = 0
arrK = 0

If First = Last Then
    DataArray(UBound(DataArray, 1) - LBound(DataArray, 1), 0) = Cells(First, 1)
    DataArray(UBound(DataArray, 1) - LBound(DataArray, 1), UBound(DataArray, 2) - LBound(DataArray, 2)) = First
Else
    For i = First To Last
        Key = Cells(i, 1)
        arrI = 0
        If (Key = "PI" Or Left(Key, 1) = "I" Or Left(Key, 1) = "O" Or Key = "PO" Or Key = "R" Or Key = "PC" Or Key = "H") Then
            exist = 0
            For z = LBound(DataArray, 1) To UBound(DataArray, 1)
                If DataArray(z, 0) = Left(Key, 1) Then
                 exist = 1
                End If
            Next z
           
            If exist = 0 Then
                For k = UBound(DataArray, 1) To LBound(DataArray, 1) Step -1
                    If DataArray(k, 0) = "" Then
                        arrI = k
                    End If
                Next k
                DataArray(arrI, 0) = Key
            End If
        End If
    Next i

    Dim rowNumber

    For i = First To Last
        Key = Cells(i, 1)
        arrI = 0
        arrK = 0
        If (Key = "PI" Or Left(Key, 1) = "I" Or Left(Key, 1) = "O" Or Key = "PO" Or Key = "R" Or Key = "PC" Or Key = "H") Then
            rowNumber = 0
               
            For z = LBound(DataArray, 1) To UBound(DataArray, 1)
                If (DataArray(z, 0) = Key) Or (DataArray(z, 0) = Left(Key, 1) And Left(Key, 1) = "I") Or (DataArray(z, 0) = Left(Key, 1) And Left(Key, 1) = "O") Then
                    rowNumber = z
                End If
            Next z
               
            For k = UBound(DataArray, 2) To LBound(DataArray, 2) Step -1
                If DataArray(rowNumber, k) = "" Then
                    arrK = k
                End If
            Next k
            DataArray(rowNumber, arrK) = i
        End If
    Next i
End If

For i = 0 To numberOfTheTypes - 1
    Dim DataToTransform() As String
    ReDim DataToTransform(numberOfTheData)
    For k = 0 To numberOfTheData
        DataToTransform(k) = DataArray(i, k)
    Next k
   
    If DataToTransform(i) = "R" Then
        jsonDictionary.Add "request_type", Cells(DataToTransform(i + 1), 2)
        jsonDictionary.Add "url", Cells(DataToTransform(i + 1), 3)
    Else
        Set jsonDictionary_inner_check = transforArray(DataToTransform, TC)
        If jsonDictionary_inner_check.Count <> 0 Then
            jsonDictionary.Add DataArray(i, 0), transforArray(DataToTransform, TC)
        End If
    End If
Next i

Set CollectRequest = jsonDictionary
Set jsonItems = Nothing
End Function

Function transforArray(DataArray() As String, TC As Integer) As Dictionary
Dim jsonDictionary_inner As New Dictionary

Dim i, z
Dim DataArray_inner() As String
Dim jsonDictionary_inner_check As Dictionary
ReDim DataArray_inner(UBound(DataArray, 1) - LBound(DataArray, 1))

If Left(DataArray(0), 1) <> "I" And Left(DataArray(0), 1) <> "O" And DataArray(0) <> "R" Then
    For i = 1 To (UBound(DataArray, 1) - LBound(DataArray, 1))
        If DataArray(i) <> "" Then
            If Cells(DataArray(i), TC) <> "" Then
                jsonDictionary_inner.Add Cells(DataArray(i), 2), Cells(DataArray(i), TC)
            End If
        End If
    Next i
End If

For i = 0 To UBound(DataArray, 1) - LBound(DataArray, 1)
    If DataArray(i) = "" Then
        ReDim Preserve DataArray(i - 1)
        Exit For
    End If
Next i

If Left(DataArray(0), 1) = "I" Or Left(DataArray(0), 1) = "O" Then
    For i = 1 To (UBound(DataArray, 1) - LBound(DataArray, 1))
        z = 1
        If i < UBound(DataArray, 1) - LBound(DataArray, 1) Then
            If (Cells(DataArray(i + 1), 1) <> Cells(DataArray(i), 1)) Then
                DataArray_inner(0) = Cells(DataArray(i), 1)
                For k = i + 1 To UBound(DataArray, 1) - LBound(DataArray, 1)
                        If Cells(DataArray(k), 1) <> "" And Cells(DataArray(k), 1) <> DataArray_inner(0) Then
                            DataArray_inner(z) = DataArray(k)
                            z = z + 1
                        Else
                            Exit For
                        End If
                Next k
                Set jsonDictionary_inner_check = transforArray(DataArray_inner, TC)
                If jsonDictionary_inner_check.Count <> 0 Then
                    jsonDictionary_inner.Add Cells(DataArray(i), 2), jsonDictionary_inner_check
                End If
               
                i = i + z - 1
                ReDim DataArray_inner(UBound(DataArray, 1) - LBound(DataArray, 1))
            Else
                If Cells(DataArray(i), TC) <> "" Then
                    jsonDictionary_inner.Add Cells(DataArray(i), 2), Cells(DataArray(i), TC)
                End If
            End If
        Else
            If Cells(DataArray(i), TC) <> "" Then
                jsonDictionary_inner.Add Cells(DataArray(i), 2), Cells(DataArray(i), TC)
            End If
        End If
    Next i
End If

Set transforArray = jsonDictionary_inner
Set jsonDictionary = Nothing
End Function





[/spoiler]

Яндекс.Метрика Рейтинг@Mail.ru