如何将 Google Cloud Platform 链接到 Google Sheet 脚本

Pat*_*mes 5 google-maps google-sheets google-apps-script google-sheets-api

如何使用 API-Key 或 OAuth2 将 Google Sheet 代码链接到我的付费 Cloud Platform 帐户。我有一个简单的应用程序脚本链接到一个简单的工作表,只要我在“免费”模式下使用它,它就可以完美地工作。但我无法让我的 Google Cloud Platform 帐户 API 与我的工作表一起使用。谁能帮我解决这个问题或告诉我我做错了什么?

工作表的作用:从连续的前三列中获取“发件人地址”、“收件人地址”和日期时间。然后我maps.newDirectionFinder()使用这三个输入参数进行调用。distance.value然后,我使用、duration.value和更新同一行中的其他三列duration_in_traffic.value

下面是我的代码。使用 " 时Maps.setAuthentication(<client-id>, <secret-key>)",我在调用的行中收到“不允许操作”错误Maps.newDirectionFinder()

注意:我启用的 API 包括:“Directions API”、“Google Sheets API”、“Distance Matrix API”、“Maps Javascript API”和“Roads API”。

代码(删除键):

/********************************************************************************
 * Library of Direction Finding and Route Optimization routines from Google Maps
 * Documentation of the Google Maps directionFinder functionality is here: 
 *       https://developers.google.com/apps-script/reference/maps/direction-finder
********************************************************************************/

// To do: 
// API Key (11/04/2018): <API-key>

/********************************************************************************
 * calcDistanceTable:
********************************************************************************/
function calcDistanceTable() {

  // var debug;
  var mysheet = SpreadsheetApp.getActiveSheet();
  var fromLocation = "test string";
  var toLocation = "test string";
  var lastRow = mysheet.getLastRow() + 1;
  var directions;
  var stringDate = "11/03/2018 22:00";//RR
  var customDate = new Date('Thu Nov 02 2018 17:29:34 GMT-0700 (PDT)');//RR 

  var row = 1; 

  //AuthenticateMaps();//RR

  // this next line doesn't work: there is no error message, but my GCP API Console shows no requests -- it seems to be ignored
  // https://maps.googleapis.com/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=<API-key>

  // this next line failed with an error:  TypeError: Attribute name "async" associated with an element type "script" must be followed by the ' = ' character. (line 32, file "getDistTime")
  // <script async defer src="https://maps.googleapis.com/maps/api/js?key=<API-key>&callback=initMap" type="text/javascript"></script>

  // this failed on the line where I call "directions = Maps.newDirectionFinder()" with this error message:  Action not allowed (line 50, file "Code") 
  // Maps.setAuthentication("<Client-ID>","<Secret-key>");


  do {    
    debug = mysheet.getRange(row, 1).getValues()[0][0];

    // check column 5 ('E') to see if this has been run already for this row
    if(mysheet.getRange(row, 5).getValues()[0][0] <= 0){ 

      fromLocation = mysheet.getRange(row, 1).getValues()[0][0];
      toLocation = mysheet.getRange(row, 2).getValues()[0][0];
      stringDate=mysheet.getRange(row, 4).getValues()[0][0];//RR
      customDate=new Date(stringDate);//RR
      Logger.log("row: " + row + " From: " + fromLocation + " To: " + toLocation);

     directions = Maps.newDirectionFinder()
        .setOrigin(fromLocation)
        .setDestination(toLocation)
        .setDepart(customDate)//RR
        .setMode(Maps.DirectionFinder.Mode.DRIVING)
        //.key=<API-key>      // this didn't work:  ReferenceError: "xxxxxxx" is not defined. (line xx, file "Code")
        //.key="<API-key>"  // this didn't work:  TypeError: Cannot find function getDirections in object <API-key>
        .getDirections();

      mysheet.getRange(row,6).setValue(directions.routes[0].legs[0].distance.value / 1000 * 0.621371);
      mysheet.getRange(row,7).setValue(directions.routes[0].legs[0].duration.value / 60);
      mysheet.getRange(row,8).setValue(directions.routes[0].legs[0].duration_in_traffic.value / 60);

      temp = directions;  // here to set a breakpoint to pause during debug 

    } 
    row++;
  } while (row < lastRow); 
}
Run Code Online (Sandbox Code Playgroud)