Excel VBA JSON POST循环

时间:2018-08-26 16:57:32

标签: json excel excel-vba post

我要完成的工作是遍历特定的表列,并为每个可见行返回该单元格的值。该单元格包含一个根据其他列中的表的值创建JSON的公式。这是我要遍历的列的示例:

Json Output

在此循环中,我将使用每个单元格值并发布JSON,但它需要遍历该列中的每个单元格并进行发布。这是我用来发布没有循环的单个单元格的VBA代码:

Sub FulcrumUpload()

Dim xhr As Object, thisRequest As String, JSONvalue As String

Set xhr = CreateObject("Microsoft.XMLHTTP")
thisRequest = "https://api.fulcrumapp.com/api/v2/records.json"

xhr.Open "POST", thisRequest, False
xhr.setRequestHeader "Accept", "application/json"
xhr.setRequestHeader "Content-type", "application/json"
xhr.setRequestHeader "X-ApiToken", "11111111111111"

xhr.Send Sheets("Fulcrum Upload").Range("V3").value

End Sub

1 个答案:

答案 0 :(得分:1)

假设从V3开始,已过滤的数据在V列中,也许像这样:

Public Sub FulcrumUpload()
    Dim xhr As Object, thisRequest As String, rng As Range

    Set xhr = CreateObject("Microsoft.XMLHTTP")
    thisRequest = "https://api.fulcrumapp.com/api/v2/records.json"

    With ThisWorkbook.Worksheets("Fulcrum Upload")
        For Each rng In .Range("V3:V" & .Cells(.Rows.Count, "V").End(xlUp).Row).SpecialCells(xlVisible)
            If Not IsEmpty(rng) Then
                With xhr
                    .Open "POST", thisRequest, False
                    .setRequestHeader "Accept", "application/json"
                    .setRequestHeader "Content-type", "application/json"
                    .setRequestHeader "X-ApiToken", "11111111111111"
                    .send rng.Value
                End With
            End If
        Next
    End With
End Sub