如何发出HTTP从Excel VBA的Mac GET(How do I issue an HTTP

2019-08-22 14:34发布

我需要发出一个HTTP查询字符串从Excel Web服务为您的Mac 2011年。我已经看到了答案使用QueryTables( 我如何使用VBA?发送一个HTTP POST请求到服务器从Excel ),但他们使用POST方法,而不是一个GET方法。 我也看到,它很容易从Windows机器,但我卡在Mac上。

任何建议,还是绝望?

Answer 1:

做进一步的研究,我碰到罗伯特骑士的评论是关于这个问题的VBA Shell函数在Office 2011的Mac和建造用他execShell函数调用卷曲的HTTPGET功能。 我测试过这与Excel运行Mac OS X 10.8.3(山狮)为Mac 2011下面是VBA代码在Mac上:

Option Explicit

' execShell() function courtesy of Robert Knight via StackOverflow
' https://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac

Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long

Function execShell(command As String, Optional ByRef exitCode As Long) As String
    Dim file As Long
    file = popen(command, "r")

    If file = 0 Then
        Exit Function
    End If

    While feof(file) = 0
        Dim chunk As String
        Dim read As Long
        chunk = Space(50)
        read = fread(chunk, 1, Len(chunk) - 1, file)
        If read > 0 Then
            chunk = Left$(chunk, read)
            execShell = execShell & chunk
        End If
    Wend

    exitCode = pclose(file)
End Function

Function HTTPGet(sUrl As String, sQuery As String) As String

    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long

    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    sResult = execShell(sCmd, lExitCode)

    ' ToDo check lExitCode

    HTTPGet = sResult

End Function    

要使用此功能,复制上面的代码,在Excel中打开VBA编辑器为Mac 2011如果你没有一个模块,单击插入 - >模块。 将代码粘贴到模块文件粘贴。 离开VBA编辑器(三叶草-Q)。

下面是一个使用天气预报Web服务的具体例子( http://openweathermap.org/wiki/API/JSON_API )

单元格A1将保留为城市的名称。

在A2单元格中输入URL字符串: http://api.openweathermap.org/data/2.1/forecast/city

单元格A3将构建查询串,输入: ="q=" & A1

在A4单元格,输入: =HTTPGet(A2, A3)

现在,请在单元格A1城市名称,例如London ,A4单元格会告诉你包含伦敦天气预报JSON响应。 更改A1从价值LondonMoscow - A4将改变为莫斯科JSON格式的预测。

很显然,使用VBA,你可以分析和重新格式化JSON数据,并将其放置在所需工作表在哪里。

性能或可伸缩性没有索赔,但对于简单的一次性访问从Excel Web服务for Mac 2011中,这似乎这样的伎俩和满足,我贴我原来的问题的需要。 因人而异 !



Answer 2:

从约翰·斯蒂芬斯上面的答案是太棒了(请给予好评吧!),但它不再在最近的Excel工作对我来说:MAC 2016年,有一个错误,需要在64位系统中使用要更新的代码。

采取一些技巧,从一个问题,我在相关的库中找到 ,我能够在约翰的脚本来调整数据类型在Excel中正常工作:MAC 2016

Option Explicit

' execShell() function courtesy of Robert Knight via StackOverflow
' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac

Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr

Function execShell(command As String, Optional ByRef exitCode As Long) As String
    Dim file As LongPtr
    file = popen(command, "r")

    If file = 0 Then
        Exit Function
    End If

    While feof(file) = 0
        Dim chunk As String
        Dim read As Long
        chunk = Space(50)
        read = fread(chunk, 1, Len(chunk) - 1, file)
        If read > 0 Then
            chunk = Left$(chunk, read)
            execShell = execShell & chunk
        End If
    Wend

    exitCode = pclose(file)
End Function

Function HTTPGet(sUrl As String, sQuery As String) As String

    Dim sCmd As String
    Dim sResult As String
    Dim lExitCode As Long

    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
    sResult = execShell(sCmd, lExitCode)

    ' ToDo check lExitCode

    HTTPGet = sResult

End Function


Answer 3:

另一种选择(相应地更新,如果你的卷发是不是位于/ opt / local / bin目录/卷曲):

VBA:

Public Function getUrlContents(url) As String
    Dim command As String
    command = "do shell script ""/path_to/getUrl.sh " + url + """"
    getUrlContents = VBA.MacScript(command)
End Function

/path_to/getUrl.sh:

#!/bin/sh

if [ -z "$1" ]
  then
    echo "missing url argument"
else
    /opt/local/bin/curl "$1"
fi

请注意,您必须确保getUrl.sh是可执行文件:

chmod u+x getUrl.sh


文章来源: How do I issue an HTTP GET from Excel VBA for Mac