【发布时间】:2016-04-21 19:57:47
【问题描述】:
我在网络上有 XML 数据。这些是游戏内物品的价格和价格数据:
https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34
调用函数以获取 SINGLE 值(例如最高买入订单):
A B
1 ItemID buy/max
2 34 =ImportXML("https:// ... &typeid=34", "//buy/max")
3 35
函数如下:
Function ImportXML(url As String, query As String)
Dim document As MSXML2.DOMDocument60
Dim http As New MSXML2.XMLHTTP60
http.Open "GET", url, False
http.send
Set document = http.responseXML
ImportXML = document.SelectSingleNode(query).nodeTypedValue
End Function
一个值就可以了。如果我有 500 个项目,那么获取数据会非常长。 要填充多个单元格(在这种情况下只有两个),我们需要通过添加 &typeid=35 来修改输入链接:
https://api.eve-central.com/api/marketstat?regionlimit=10000002&typeid=34&typeid=35
通过该链接,我可以在 MsgBox 中获取两个值。但我需要处理这些数据,所以我需要在单元格 B3 中使用它。 现在代码如下所示:
Function ImportXML(url As String, query As String)
Dim document As MSXML2.DOMDocument60
Dim http As New MSXML2.XMLHTTP60
http.Open "GET", url, False
http.send
Set document = http.responseXML
Dim R As Integer
Dim C As Integer
R = ActiveCell.Row '<- these variables are for changing'
C = ActiveCell.Column '<- output cell'
Set xmlNodeList = document.SelectNodes(query)
For Each xmlNode In xmlNodeList
Cells(R, C) = xmlNode.nodeTypedValue '<- THIS LINE CAUSE AN ERROR'
'MsgBox xmlNode.nodeTypedValue <- if that line is active I got correct result'
R = R + 1 'but in MsgBox'
Next xmlNode
End Function
当我尝试将查询中的数据写入单元格时,出现了问题。 我想要实现的是Google 表格网络服务 中的与 ImportXML 函数类似的行为。 我没有 Excel 2013,所以我不能使用 WEBSERVICE 函数或 FILTERXML 和 XPath。 我从来没有在 VBA 中编写过代码。 如果你能把最终代码放在这里,我将不胜感激。
【问题讨论】:
标签: xml excel excel-2007 xml-import vba