Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用directions api在excel中获取距离(在VBA中使用JSON)_Json_Excel_Vba_Google Directions Api - Fatal编程技术网

如何使用directions api在excel中获取距离(在VBA中使用JSON)

如何使用directions api在excel中获取距离(在VBA中使用JSON),json,excel,vba,google-directions-api,Json,Excel,Vba,Google Directions Api,我试图将两点之间的距离输入excel文件。(基于荷兰邮政编码、城市或地址线) 我在网上找到了一些信息,并使用了我在网上找到的信息 小结: 我为directions API创建了一个API键 我从中导入了VBA-JSON 启用了脚本运行时、WINHTTP等引用 在excel中复制宏,如下所示 现在,当我在excel中使用公式时,我得到一个#值错误 旅行距离(D2;=设置!$B$2;设置!$B$1) D2是zipcode源代码“8311 PA”,设置!B2是“德瓦尔克韦克罗姆”联合航空公司的目

我试图将两点之间的距离输入excel文件。(基于荷兰邮政编码、城市或地址线)

我在网上找到了一些信息,并使用了我在网上找到的信息

小结:

  • 我为directions API创建了一个API键
  • 我从中导入了VBA-JSON
  • 启用了脚本运行时、WINHTTP等引用
  • 在excel中复制宏,如下所示
现在,当我在excel中使用公式时,我得到一个#值错误

  • 旅行距离(D2;=设置!$B$2;设置!$B$1)
    D2是zipcode源代码“8311 PA”,设置!B2是“德瓦尔克韦克罗姆”联合航空公司的目的地,格伦维尔,梅佩尔
    如果我更换D2并安装!B2加上“美国国会大厦”和“白宫”,公式返回4326 so 4,3公里的距离。
    所以我知道请求(API键)是有效的,我也知道JSON解析是有效的

使用荷兰语地址时会出现什么问题?我尝试过纯文本,如街道名称、城市名称、邮政编码等

谷歌距离api似乎不喜欢
ö
字符。我不知道zipcode,但可能也有问题。将
ö
更改为
o
并对另一个地址使用类似于荷兰阿姆斯特丹的
Amsterdam
的代码返回约123km的距离(使用不同的代码,但使用相同的api)。这有意义吗?谢谢你的提示,我尝试了不同的变化,但仍然以值错误告终。不知道为什么,如果我在我的webbrowser中输入搜索字符串,我可以看到它返回正确的方向和距离。你让它只使用
o
?如果没有,返回的错误消息是什么?它不适用于“o”。仅使用Meppel,荷兰和Lunteren,荷兰的结果相同。我得到的唯一错误是在excel中:#waarde,它是#Value。而不是excel中的错误消息。您需要在API调用返回的JSON中看到错误消息。您可以在即时窗口中访问它,方法是在解析JSON后放置一个断点,然后,如果我没记错的话,键入
?JSON(“error_message”)
或类似的内容(在locals窗口中展开JSON以查看确切的字典键)
Function TRAVELDISTANCE(origin, destination, apikey)

   Dim strUrl As String
   strUrl = "https://maps.googleapis.com/maps/api/directions/json?origin=" & origin & "&destination=" & destination & "&key=" & apikey
    
   Set httpReq = CreateObject("MSXML2.XMLHTTP")
   With httpReq
        .Open "GET", strUrl, False
        .Send
   End With
    
   Dim response As String
   response = httpReq.ResponseText
    
   Dim parsed As Dictionary
   Set parsed = JsonConverter.ParseJson(response)
   Dim meters As Integer
    
   Dim leg As Dictionary
    
    
   For Each leg In parsed("routes")(1)("legs")
       meters = meters + leg("distance")("value")
   Next leg
    
    
    
   TRAVELDISTANCE = meters

End Function