首页 > 解决方案 > Google API 的 ImportXML / HTML 问题

问题描述

我正在努力从 importxml 到 googlesheet 获得任何结果。我以前得到的是反映距离的输出,但现在我得到一个错误“导入的内容是空的”。

 =importxml("http://maps.googleapis.com/maps/api/directions/xml?origin=" & B9 & "&destination=" & D9 & "&sensor=false&alternatives=false","//leg/distance/value")/1000*0.621371

有谁知道这里的解决方法?我也尝试过使用我的 api 密钥并遇到了类似的问题:

=IMPORTHTML("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=bay+shore,ny&destinations=New+York+City,NY&key=XXXXXXX", "table",3)

非常感谢任何帮助。

标签: google-mapsgoogle-sheetsgoogle-directions-api

解决方案


这只是一个初学者的尝试,因此欢迎改进,但是查看 geocodezip 发布的 URL,您可以将该 URL 传递给UrlFetchApp.fetchJSON.parsegetContentText()响应中使用。

您可以在工作表中用作用户定义的函数并通过控制台检查Logger.log(GETDISTANCE());

function GETDISTANCE() {
  
    var res= UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=bay+shore,ny&destinations=New+York+City,NY&key=");
    var content = res.getContentText();
    var json = JSON.parse(content);
    
    return json.rows[0]["elements"][0]["distance"].text;
}

您希望将 URL 和位置部分移动到传递给函数的参数中,以使其更加通用。

我通过检查 JSON 结构确定了用于提取里程文本的路径,如下所示:

在此处输入图像描述


推荐阅读