1

我有一个电子表格,其中包含 A 列中的城市,州。我想从当天到 B 列中获取 3 天的沉淀类型/值。我开始使用 openweathermap API,我发现我缺乏 XML 经验正在扼杀我。以下是我的开始方式,但我无法走得更远。我为使用节点和元素所做的任何尝试都没有成功。任何帮助,将不胜感激。

这是一个示例 XML http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston+MA&mode=xml&units=metric&cnt=7的链接?

Option Explicit

Function CityForecast(City As String) As String
Dim StartingURL As String
Dim SecondaryURL As String
Dim FinalURL As String
Dim CorrectedCity As String
Dim objXML As Object
Dim objDOM As MSXML2.DOMDocument60

CorrectedCity = Replace(City, " ", "+")
CorrectedCity = Replace(City, ",", "+")

StartingURL = "http://api.openweathermap.org/data/2.5/forecast/daily?q="
SecondaryURL = "&mode=xml&units=metric&cnt=7"
FinalURL = StartingURL + CorrectedCity + SecondaryURL
Debug.Print FinalURL

Set objXML = CreateObject("MSXML2.XMLHTTP")
Set objDOM = New MSXML2.DOMDocument60

With objXML
    .Open "GET", FinalURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
    .send
    objDOM.LoadXML .responseText
End With


End Function

Function CorrectedDate(WxDate As String) As String
Dim yr As Integer
Dim dy As Integer
Dim mnth As Integer

yr = Left(WxDate, 4)
mnth = Right(Left(WxDate, 7), 2)
dy = Right(WxDate, 2)

CorrectedDate = mnth & "/" & dy & "/" & yr
End Function
4

1 回答 1

0

尝试这样的事情。我使用了 DOMDocument 而不是 DOMDocument60,这对我来说是一个错误。您可能不需要更改该变量,但我做到了。

此函数也被修改为接受变量预测,daysOut因此,如果您想要从今天(包括今天)开始的第三天的预测,请执行以下操作:

=CityForecast("Boston, MA", 3)

这是代码:

Option Explicit

    Function CityForecast(City As String, daysOut As Integer) As String
    Dim StartingURL As String
    Dim SecondaryURL As String
    Dim FinalURL As String
    Dim CorrectedCity As String
    Dim objXML As Object
    Dim objDOM As MSXML2.DOMDocument  '### Changed
    'new variables I added:
    Dim precip As IXMLDOMNode


    CorrectedCity = Replace(City, " ", "+")
    CorrectedCity = Replace(City, ",", "+")

    StartingURL = "http://api.openweathermap.org/data/2.5/forecast/daily?q="
    SecondaryURL = "&mode=xml&units=metric&cnt=7"
    FinalURL = StartingURL + CorrectedCity + SecondaryURL
    Debug.Print FinalURL

    Set objXML = CreateObject("MSXML2.XMLHTTP")
    Set objDOM = CreateObject("MSXML2.DOMDocument")   '### Changed

    With objXML
        .Open "GET", FinalURL, False
        .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
        .send
        objDOM.LoadXML .responseText
    End With

    'Use xpath to get the appropriate precipitation detail
    Set precip = objDOM.SelectSingleNode("/weatherdata/forecast/time[" & daysOut & "]/precipitation")


    If precip Is Nothing Then 
    ' in the event that the node doesn't exist
        CityForecast = "n/a" 
    Else
    ' otherwise, return the forecast attribute (0):
        If precip.Attributes.Length = 0 then
            CityForecast = 0
        Else
            CityForecast = precip.Attributes(0).nodeTypedValue
        End IF
    End If

    Set objXML = Nothing
    Set objDOM = Nothing

    End Function
于 2014-06-25T18:02:13.810 回答