【问题标题】:Populate Excel using an API使用 API 填充 Excel
【发布时间】:2021-05-22 20:20:13
【问题描述】:

我想查看我的游戏王卡片的卡片价格,刷新时我会在其中获得实时数据。
我一直在使用公共 API 获取价格数据:https://yugiohprices.docs.apiary.io/#

我手动输入了每张卡片的名称、稀有度和打印标签。我希望使用此 API 将价格数据填充到我的工作表中。

我的想法是我需要 API 来调用每个 print_tag 并将请求返回到三个价格列。 我是否需要在 VBA 中创建一个 for 循环来执行此操作,或者是否有更简单的方法使用 PowerQuery?

我目前拥有的 VBA 代码:

Option Explicit

Sub restAPICall()
Dim objRequest As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Dim json As Object

Set objRequest = CreateObject("MSXML2.XMLHTTP")
strUrl = "http://yugiohprices.com/api/price_for_print_tag/print_tag"
blnAsync = True

With objRequest
    .Open "GET", strUrl, blnAsync
    .SetRequestHeader "Conset-Type", "application/json"
    .Send
    
    While objRequest.readyState <> 4
        DoEvents
    Wend
    
    strResponse = .ResponseText
    
End With

【问题讨论】:

    标签: excel vba api powerquery


    【解决方案1】:

    您已成功从远程 API 查询数据!

    基本上你需要做的步骤:

    1. 循环遍历您的 link 列中的所有链接以及每个链接
    2. 获取数据(完成)
    3. 由于 API 以 JSON 格式返回数据,您需要转换 JSON 字符串。 GitHub VBA-JSON 上提供了一个可以帮助您解决此问题的知名库(将其转换为字典)
    4. 提取想要的字段(可能是price_data)并获取值
    5. 在excel表格的相应单元格中写下价格

    【讨论】:

      【解决方案2】:
      Option Explicit
      Sub restAPICall()
      Dim objRequest As Object
      Dim blnAsync As Boolean
      Dim strResponse As String, Prices As String
      Dim High As String, Low As String, Avg As String
      Dim strUrl As String, Print_Tag As String
      Dim PriceArray() As String
      Dim LastRow As Long, i As Long
      
      Set objRequest = CreateObject("MSXML2.XMLHTTP")
      strUrl = "http://yugiohprices.com/api/price_for_print_tag/"
      blnAsync = True
      
      'Get last row of column C (print tag)
      LastRow = Cells(Rows.Count, 3).End(xlUp).Row
      
      Application.ScreenUpdating = False
      
      For i = 2 To LastRow
      'Get print tag
      Print_Tag = Trim(Range("C" & i).Value)
      'Clear values
      strResponse = ""
      High = ""
      Low = ""
      Avg = ""
      
      'Make request with print_tag
      With objRequest
          .Open "GET", strUrl & Print_Tag, blnAsync
          .SetRequestHeader "Conset-Type", "application/json"
          .Send
      
          While objRequest.readyState <> 4
              DoEvents
          Wend
      
          strResponse = .ResponseText
      
      End With
      
      'Get prices
      strResponse = Replace(strResponse, ",", "")
      strResponse = Replace(strResponse, Chr(34) & "high" & Chr(34) & ":", "|")
      strResponse = Replace(strResponse, Chr(34) & "low" & Chr(34) & ":", "|")
      strResponse = Replace(strResponse, Chr(34) & "average" & Chr(34) & ":", "|")
      strResponse = Replace(strResponse, Chr(34) & "shift" & Chr(34) & ":", "|")
      
      PriceArray = Split(strResponse, "|")
      High = PriceArray(1)
      Low = PriceArray(2)
      Avg = PriceArray(3)
      
      'Put prices in column
      Range("F" & i).Value = Low
      Range("G" & i).Value = Avg
      Range("H" & i).Value = High
      
      Next
       
      Application.ScreenUpdating = False
      Set objRequest = Nothing
      End Sub
      

      这段代码对我有用。感谢 u/NegativeSpeech 在 reddit 上分享他的作品。

      【讨论】:

      • 另外,你不会在最后重新打开屏幕更新。
      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2017-02-12
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多