How to add my key to this google app script?

我只是一个虾纸丫 提交于 2021-01-28 10:50:15

问题


I need to update this script to pass my key so that I don't go over the limit per day. How would I modify this script to pass my key?

(NOTE: google class information found here: https://developers.google.com/apps-script/reference/maps/geocoder)

    function geo2zip(a) {
      var response=Maps.newGeocoder()
        .reverseGeocode(lat(a),long(a));
      return response.results[0].formatted_address.split(',')[2].trim().split(' ')[1];
    }
    function lat(pointa) {
     var response = Maps.newGeocoder()
         .geocode(pointa);
      return response.results[0].geometry.location.lat
    }
    function long(pointa) {
      var response = Maps.newGeocoder()
         .geocode(pointa);
      return response.results[0].geometry.location.lng
    }

I have never used a google apps script before.

I have this script in place and am using "=geo2zip(cell)" to call the script from my google sheet to get the zip code for incomplete addresses. There are ~28k in my sheet. I have enabled the geocoding api in my google console and my billing info. and created my key but am not sure how to include my key in the script above.

I have also tried using the following to call the api using my key. It is working, but this has resulted in VERY slow responses. At the rate it is taking these to respond, it will take me 10 days to finish updating my 28k records.

CELL M4852--> "https://maps.googleapis.com/maps/api/geocode/xml?address=ADDRESS&key=MYKEY" NEXT CELL--> "=ImportXML(M4852,"/GeocodeResponse/result/formatted_address")"

The script responds much more quickly, so I would prefer to use that and pass my key. Please let me know if you can help.

UPDATE: I was able to resolve this using Alberto's suggestion below of adding the Maps.setAuthentication.


回答1:


I believe your issue has more to do with the Google API management than with the scripts themselves. There is a way to limit how much your key is used. According to the Maps Platform documentation:

Manage Your Cost of Use

To manage your cost of use of the Google Maps Platform APIs, you can set daily limits to all requests to any billable API.

To view or change daily billable limits for the Geocoding API, do the following:

Go to the Geocoding API Quotas page in the Google Cloud Platform Console. From the projects list, select a project. In the Requests section, on the Requests per day line, click the edit icon, then enter the preferred total billable daily quota, up to the limit (if any) specified by Google.

You can basically set how many requests you want to allow per day, which will avoid you going over your limit.

UPDATE

You can link the script to your account using the Maps.setAuthentication(clientId, signingKey) method, according to the docs, it:

Enables the use of an externally established Maps API for Business account, to leverage additional quota allowances. Your client ID and signing key can be obtained from the Google Enterprise Support Portal. Set these values to null to go back to using the default quota allowances.

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

Quotas URL: https://console.cloud.google.com/project/_/apiui/apiview/geocoding_backend/quotas?_ga=2.141719605.643331044.1560431279-1498828710.1560431279

Documentation URL: https://developers.google.com/maps/documentation/geocoding/usage-and-billing



来源:https://stackoverflow.com/questions/56580773/how-to-add-my-key-to-this-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!