简体   繁体   English

提供API密钥,以避免来自Apps脚本中Maps Service的命中限制错误

[英]Supply API key to avoid Hit Limit error from Maps Service in Apps Script

I have a Google Sheet where we are fetching the driving distance between two Lat/Lng via the Maps Service . 我有一个Google表格,可以通过Maps Service获取两个纬度/经度之间的行驶距离。 The function below works, but the matrix is 4,500 cells, so I'm getting the "Hit Limit" error. 下面的函数有效,但是矩阵是4,500个单元格,因此出现“命中限制”错误。

How can I supply my paid account's API key here? 如何在此处提供我的付费帐户的API密钥?

Custom Function 自定义功能

function drivingMeters(origin, destination) {
  if (origin=='' || destination==''){return ''}
  var directions = Maps.newDirectionFinder()
  .setOrigin(origin)
  .setDestination(destination)
  .getDirections();
  return directions.routes[0].legs[0].distance.value ;
}

Example use: 使用示例:

A1: =drivingMeters($E10,G$9)

Where E10 = 42.771328,-91.902281
  and G9  = 42.490390,-91.1626620

Per documentation, you should initialize the Maps service with your authentication details prior to calling other methods: 根据文档,您应在调用其他方法之前使用身份验证详细信息初始化Maps服务

Your client ID and signing key can be obtained from the Google Enterprise Support Portal. 您的客户ID和签名密钥可以从Google Enterprise支持门户获取。 Set these values to null to go back to using the default quota allowances. 将这些值设置为null可返回使用默认配额限额。

I recommend storing these values in PropertiesService and using CacheService , to provide fast access. 我建议将这些值存储在PropertiesService并使用CacheService来提供快速访问。 Using this approach, rather than writing them in the body of your script project, means they will not be inadvertently copied by other editors, pushed to a shared code repository, or visible to other developers if your script is published as a library. 使用这种方法,而不是在脚本项目的主体中编写它们,意味着如果您的脚本以库的形式发布,它们将不会被其他编辑器无意复制,推送到共享代码存储库或其他开发人员可见。

Furthermore, I recommend rewriting your custom function to accept array inputs and return the appropriate array output - this will help speed up its execution. 此外,我建议重写自定义函数以接受数组输入并返回适当的数组输出-这将有助于加快其执行速度。 Google provides an example of this on the custom function page: https://developers.google.com/apps-script/guides/sheets/functions#optimization Google在自定义功能页面上提供了一个示例: https : //developers.google.com/apps-script/guides/sheets/functions#optimization

Example with use of props/cache: 使用道具/缓存的示例: 道具实例

function authenticateMaps_() {
  // Try to get values from cache:
  const cache = CacheService.getScriptCache();
  var props = cache.getAll(['mapsClientId', 'mapsSigningKey']);
  // If it wasn't there, read it from PropertiesService.
  if (!props || !props.mapsClientId || !props.mapsSigningKey) {
    const allProps = PropertiesService.getScriptProperties().getProperties();
    props = {
      'mapsClientId': allProps.mapsClientId,
      'mapsSigningKey': allProps.mapsSigningKey
    };
    // Cache these values for faster access (max 6hrs)
    cache.putAll(props, 21600);
  }
  // Apply these keys to the Maps Service. If they don't exist, this is the
  // same as being a default user (i.e. no paid quota).
  Maps.setAuthentication(props.mapsClientId, props.mapsSigningKey);
}
function deauthMaps_() {
  Maps.setAuthentication(null, null);
}

// Your called custom function. First tries without authentication,
// and then if an error occurs, assumes it was a quota limit error
// and retries. Other errors do exist (like no directions, etc)...
function DRIVINGMETERS(origin, dest) {
  if (!origin || !destination)
    return;
  try {
    return drivingMeters_(origin, dest);
  } catch (e) {
    console.error({
      message: "Error when computing directions: " + e.message,
      error: e
    });
    // One of the possible errors is a quota limit, so authenticate and retry:
    // (Business code should handle other errors instead of simply assuming this :) )
    authenticateMaps_();
    var result = drivingMeters_(origin, dest);
    deauthMaps_();
    return result;
  }
}

// Your implementation function.
function drivingMeters_(origin, dest) {
  var directions = Maps.newDirectionFinder()
  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果私钥未直接存储在应用程序脚本文件中,则服务帐户的Google Apps脚本OAuth2库错误 - Google Apps Script OAuth2 Library Error for Service Accounts if private Key is not stored in apps script file directly Google Apps脚本HTML服务和加载Google Maps JavaScript API V3 - Google apps script html service and loading the Google maps javascript api V3 Apps 脚本中的 Google Maps API 不断失败 - Google Maps API In Apps Script Keeps Failing 大查询 API 通过应用程序脚本 - 我在一个很大但在较小的表上工作的表上收到错误。 通过应用程序脚本有大小限制吗? - Big Query API via Apps script - I get an error on a table that is large but works on a smaller table. Is there a size limit via apps script? 避免 Google Apps 脚本中的 formatDate 错误 - Avoid formatDate error in Google Apps Script Google Apps脚本HTML服务键事件 - Google Apps Script HTML Service Key Events 尝试使用 Google Apps 脚本将图像上传到 Graph API 时,如何解决“异常:超出限制”错误? - How to fix “Exception: Limit Exceeded” error when trying to upload an image to Graph API using Google Apps Script? 在脚本编辑器中设置Maps API密钥 - Set Maps API key in Script Editor 在 Google 表格中使用 Apps 脚本解析来自 XML 的 Google 地图数据时出错 - Error Parsing Google Maps Data from XML using Apps Script in Google Sheets Google Apps 脚本:“无法访问的服务:镜像”错误 - Google Apps Script: “Unreachable Service: mirror” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM