我正在创建的excel电子表格的一部分是一个包含8个不同位置的网格,以及从Google Maps distance Matrix API中提取的它们之间的距离。位置是从表中输入的,并将定期更改。
我目前使用的VBA代码是:
'Calculate Google Maps distance between two addresses
Public Function GetDistance(start As String, dest As String)
Dim firstVal As String, secondVal As String, lastVal As String
firstVal = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="
secondVal = "+UK&destinations="
lastVal = "+UK&mode=car&language=en&sensor=false"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & lastVal
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, """distance"" : {") = 0 Then GoTo ErrorHandl
Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """value"".*?([0-9]+)": regex.Global = False
Set matches = regex.Execute(objHTTP.responseText)
tmpVal = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))
GetDistance = CDbl(tmpVal)
Exit Function
ErrorHandl:
GetDistance = -1
End Function
然后,我使用简单的函数在电子表格中调用它:
=GetDistance($D$14,B15)
这个脚本运行良好,但它确实意味着每次加载电子表格和每次更改位置时,我都要执行56个API调用,因此我很快就达到了2500个API调用的限制。
有没有一种方法可以让函数只在特定的时间(例如,在点击按钮时)拉取数据,或者只需更少的API调用就可以获取相同的数据?
发布于 2016-02-29 23:43:27
通过添加一个按钮(只在按下时刷新)和一个保存到目前为止获得的所有值的集合,您应该能够减少调用的数量……
Option Explicit
Public gotRanges As New Collection 'the collection which holds all the data
Public needRef As Range 'the ranges which need to be recalculated
Public refMe As Boolean 'if true GetDistance will update if not in collection
Public Function GetDistance(start As String, dest As String)
Dim firstVal As String, secondVal As String, lastVal As String, URL As String, tmpVal As String
Dim runner As Variant, objHTTP, regex, matches
If gotRanges.Count > 0 Then
For Each runner In gotRanges
If runner(0) = start And runner(1) = dest Then
GetDistance = runner(2)
Exit Function
End If
Next
End If
If refMe Then
firstVal = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="
secondVal = "+UK&destinations="
lastVal = "+UK&mode=car&language=en&sensor=false"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & lastVal
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, """distance"" : {") = 0 Then GoTo ErrorHandl
Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """value"".*?([0-9]+)": regex.Global = False
Set matches = regex.Execute(objHTTP.responseText)
tmpVal = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))
GetDistance = CDbl(tmpVal)
gotRanges.Add Array(start, dest, GetDistance)
Exit Function
Else
If needRef Is Nothing Then
Set needRef = Application.Caller
Else
Set needRef = Union(needRef, Application.Caller)
End If
End If
ErrorHandl:
GetDistance = -1
End Function
Public Sub theButtonSub() 'call this to update the actual settings
Dim runner As Variant
refMe = True
If Not needRef Is Nothing Then
For Each runner In needRef
runner.Offset.Formula = runner.Formula
Next
End If
Set needRef = Nothing
refMe = False
End Sub
如果您将a、b和c (将加载6次)更改为c、a和b(如果您理解我的意思),则不会再次加载它们。
如果您还有问题,请直接问:)
https://stackoverflow.com/questions/35700537
复制相似问题