I'm working on filling in data, including address, url, phone number, etc. for a list of businesses I have in a Google Sheet.
I have been on this site and others looking at scripts to call the Google Places API. I have figured out (I think) that I need to first get the Place_ID, and then I can call the API again to get all the details. I have copied and pasted a script off another answer on this forum, but it is hanging on line 8.
return placeId.candidates[0].place_id;
in this line, it appears that the code that I copied is referencing "candidates", which I don't know what this is.
I'd like this to write the Place_id to a cell in the row with the other known data. For example: A2:T2 is all the known data. In U2 I placed =locid(G2)
where G2 is the full address. I'd like (I think) U2 to get the place_id and then subsequent columns to receive the other requested data.
function locId(text) {
var API_KEY = 'AIzaSyDAI35g3ocior056QvNrjgY_lLs02Jkyg4';
var baseUrl = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json';
var queryUrl = baseUrl + '?input=' + text + '&inputtype=textquery&key=' + API_KEY;
var response = UrlFetchApp.fetch(queryUrl);
var json = response.getContentText();
var placeId = JSON.parse(json);
return placeId.candidates[0].place_id;
Logger.log(placeId)
}
function GET_DETAILS(id) {
var API_KEY = 'AIzaSyDAI35g3ocior056QvNrjgY_lLs02Jkyg4';
var fields = 'name,rating,formatted_phone_number,formatted_address,photo';
var baseUrl = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=';
var queryUrl = baseUrl + id + '&fields=' + fields + '&key='+ API_KEY;
if (id == '') {
return 'Give me a Google Places URL...';
}
var response = UrlFetchApp.fetch(queryUrl);
var json = response.getContentText();
var place = JSON.parse(json).result;
return [[ place.name,
place.formatted_phone_number,
place.rating,
place.formatted_address,
place.photo
]];
}
The error I'm getting is:
TypeError: Cannot read property "place_id" from undefined. (line 8).
I'm assuming the undefined
here is the 'candidates'
I'm not sure how this code should be structured to achieve what I am looking for. I'm sure this is simple, as being a total hack noob, I have only a little understanding of what I am looking at. I appreciate your assistance in helping me understand how this works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.