6

据我了解,为了跟踪我们的配额使用情况,我们需要在我们计划使用的服务上向 Google App Service 提供我们的 API 密钥。

在我的情况下,我有一个包含 Origin 和 Destination 的电子表格以及一个自定义函数来计算两者之间的距离。

我遇到了调用配额的问题.getDirections()

错误:一天内服务调用次数过多:路由。(线 **)。

代码示例:

 function getDirections_(origin, destination) {
  var directionFinder = Maps.newDirectionFinder();
  directionFinder.setOrigin(origin);
  directionFinder.setDestination(destination);
  var directions = directionFinder.getDirections();  
  return directions;
}

所以我读到,如果我将 API 密钥分配给我的项目,我应该能够看到使用情况以及我离免费配额有多近。

在脚本编辑器中,我确实启用了资源菜单/高级 Google 服务下的所有 API。然后我去了谷歌开发者控制台,在那里我没有看到任何关于我的自定义函数调用谷歌地图 API 或任何 API 使用的记录。

从逻辑上讲,我认为在我的脚本中我需要设置我的 google API 密钥,以便我的脚本开始以我的用户名调用 API 并计算我使用某些 API 的次数。我想现在我以匿名方式使用 Google Maps API,并且由于为整个公司分配了相同的 IP,因此我们用尽了允许的号码来调用此函数。

如果您知道将我的简单电子表格功能连接到我拥有的公共 API 访问密钥的方法,请回复。

谢谢你,保罗

4

4 回答 4

8

我也渴望找到这个答案很长时间了,很高兴地说我找到了。根据此页面的更新日期,Google 可能在 2015 年 10 月 14 日左右才提供此功能。

您可以利用 UrlFetchApp 添加您的 API 密钥。我在上面发布的链接应该有助于获取该密钥。

function directionsAPI(origin, destination) {
 var Your_API_KEY = "Put Your API Key Here";
 var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+
"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&alternatives="+Boolean(1)+"&key="+Your_API_KEY;

 var options={
  muteHttpExceptions:true,
  contentType: "application/json",
 };

 var response=UrlFetchApp.fetch(serviceUrl, options);
  if(response.getResponseCode() == 200) {
   var directions = JSON.parse(response.getContentText());
    if (directions !== null){
     return directions;
     }
    }
  return false;
 }

因此,浏览代码...首先输入您的 API 密钥。然后在 var serviceUrl 中选择您的参数。我已经加入了额外的参数(模式和替代)来展示如何添加它们。如果您不想要它们,请将它们删除。

使用 UrlFetch,您可以添加选项。我使用了 muteHttpExceptions,以便在响应代码指示失败时获取不会引发异常。这样我们就可以为函数选择返回类型,而不是抛出异常。我使用 JSON 作为内容类型,因此我们可以使用相同的格式来发送和检索请求。响应代码 200 表示成功,因此方向将解析并像 getDirections() 返回的对象一样运行。如果 UrlFetch 不成功(不同的响应代码)或对象为 null,则该函数将返回 false。

当您查看 Google Maps Directions API 时,您将能够在开发者控制台中实时查看查询。确保已启用计费,一旦超出配额,您将被收取费用。

于 2015-11-02T20:45:03.450 回答
1

1.) 我从控制台仪表板添加了一个 API 密钥。请记住选择您正在处理的正确项目。https://console.developers.google.com/apis/credentials?project=

2.)在我的项目(脚本编辑器)中,我使用 API 密钥和控制台中的客户端 ID 将身份验证设置为地图。我在下面包含了脚本:

function getDrivingDirections(startLoc, wayPoint, endLoc){
   var key = "Your_API_Key";
   var clientID = "Your_Client_ID";
   Maps.setAuthentication(clientID, key);
   var directions =  Maps.newDirectionFinder()
        .setOrigin(startLoc)
        .addWaypoint(wayPoint)
        .setDestination(endLoc)
        .setMode(Maps.DirectionFinder.Mode.DRIVING)
        .getDirections();
} return directions;

https://developers.google.com/apps-script/reference/maps/maps#setAuthentication(String,String)

于 2018-08-01T17:14:36.783 回答
-1

谢天谢地 JP 卡林!谢谢你上面的回答。JP 的回答也解释了他的代码。只是为了分享,没有代码解释(只需在上面查看 JP Carlin 的解释),下面是我的版本。您会看到我也有离开时间参数,以便我将获得特定日期时间的距离和驾驶分钟数。我还添加了对日志错误的调用(在“查看/日志”下查看):

注意:Google 支持人员告诉我,不支持将您的 Google 地图 API 密钥(例如,使用“Directions API”)与 Google 表格一起使用。下面的代码有效,但不受支持。截至 2018 年 11 月 4 日,谷歌有一个内部请求,要求在谷歌表格中添加对谷歌地图 API 的支持,但没有添加该功能的时间表。

/********************************************************************************
 * directionsAPI: get distance and time taking traffic into account, from Google Maps API
********************************************************************************/
function directionsAPI(origin, destination, customDate) {
        var Your_API_KEY = "<put your APK key here between the quotes>";
        var serviceUrl = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&departure_time="+customDate.getTime()+"&mode="+Maps.DirectionFinder.Mode.DRIVING+"&key="+Your_API_KEY;

        var options={
          muteHttpExceptions:true,
          contentType: "application/json",
         };

        var response=UrlFetchApp.fetch(serviceUrl, options);
          if(response.getResponseCode() == 200) {
           var directions = JSON.parse(response.getContentText());
            if (directions !== null){
             return directions;
             }
            }
      Logger.log("Error: " + response.getResponseCode() + " From: " + origin + ", To: " + destination + ", customDate: " + customDate + ", customDate.getTime(): " + customDate.getTime() );
      return false;
      }
于 2018-11-05T22:08:53.077 回答
-1

截至 2017 年 7 月 13 日,我能够通过在“高级 Google 服务”菜单(图像 1 和 2)和 Google 开发者控制台中启用 Sheets API 来使 API 正常工作。如果您使用相同的电子邮件地址登录 Google 表格,则不需要获取功能。

[在资源菜单中,选择高级 Google 服务。][1]

[在高级 Google 服务中,确保 Google 表格 API 已打开。][2]

[1]: [2]:

于 2017-07-13T13:00:20.667 回答