如何使用API-Key或OAuth2将Google表格代码链接到我的付费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);
}