Set Map API Key in Script Editor

As I understand it, to track the use of our quota, we need to provide our API key for the Google application service in the service we plan to use.

In my case, I have a spreadsheet with a source and destination and a custom function to calculate the distance between them.

I ran into a quota issue due to a call .getDirections():

Error: The service is called too many times in one day: route. (line **).

Code example:

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

So I read that if I assign an API key for my project, I can see its use and how close I am to the free quota.

API ""/" Google". Google , API Google - API.

, API Google, API , API. , API Google , IP-, .

, , , API.

+5
5

, . , Google, , 14 2015 .

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(). false, UrlFetch ( ) null.

, API Google Maps Directions. , , , .

+6

1.) API . , . https://console.developers.google.com/apis/credentials?project=

2.) ( ) "" "" API . :

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)

+1

7/13/2017 API , API " Google" ( 1 2) Google. Google , fetch .

[ "" " Google".] [1]

[ Google , API Google .] [2]

[1]: 6iCs0.png[2]: kDw5k.png

0

JP Carlin! . JP . , , ( JP Carlin), . , -, -. ( "/"):

. Google , API- Google (, "API-") Google Sheets . , . 04.11.2008 Google API Google Google Sheets, .

/********************************************************************************
 * 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;
      }
0
source

Link to comment: Google Support informed me that using your Google Maps API key (for example, with an "API") with Google Sheets is not supported. The code below works, but is not supported. As of 11/04/2008, Google has an internal request to add support for the Google Maps API in Google Sheets, but there is no schedule to add this feature.

Anyupdate about this?

0
source

All Articles