I'm using this tutorial (https://scrapediary.com/find-local-leads-with-google-places-api-and-sheets/) to scrape data from google places API into a google sheet. I copied the code exactly:
var output = [ ["Name", "Place ID", "Latitude", "Longitude", "Types"]]
var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=food&location=51.4977836,-0.1522502&radius=200&key=AIzaSyBtepY6mCTkHr3m4UCacxSkePkli5yEbCM";
var response = UrlFetchApp.fetch(url)
payload = JSON.parse(response)
for (var x = 0; x < payload['results'].length; x++){
var inner = [ payload['results'][x]['name'], payload['results'][x]['place'],payload['results'][x]['latitude'],payload['results'][x]['longitude'],payload['results'][x]['types']]
output.push(inner)}
}
and I'm trying to run it in google sheets like this:
=placeSearch("Golf Course","51.4977836","-0.1522502","20000","i_put_my_api_key_here")
and it shows "Loading" and then returns nothing. I've double checked that the url itself works by pasting it into the browser and it returns the results in JSON format. I feel like there's a problem with pushing the results to the sheet but I can't find it
There is no doubt that the code you copied is working. Upon testing the same exact code you posted to replicate the problems, I only added return
in the function to populate the cell.
See my exact code which worked and returned the data in sheets.
function placesAPI(keyword,latitude,longitude,radius,api_key,depth) {
var output = [ ["Name", "Place ID", "Latitude", "Longitude", "Types"]]
var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=food&location=51.4977836,-0.1522502&radius=200&key=AIzaSyBtepY6mCTkHr3m4UCacxSkePkli5yEbCM";
var response = UrlFetchApp.fetch(url)
payload = JSON.parse(response);
for (var x = 0; x < payload['results'].length; x++){
var inner = [ payload['results'][x]['name'], payload['results'][x]['place'],payload['results'][x]['latitude'],payload['results'][x]['longitude'],payload['results'][x]['types']]
output.push(inner)}
return(output); // added this code to put the value on the cell
}
In the function call, you need to use the api key in the url first to establish a connection. I have confirmed in my testing that if you used other api keys in the first function call, it will not return anything.
=placesAPI("Golf Course","51.4977836","-0.1522502","20000","AIzaSyBtepY6mCTkHr3m4UCacxSkePkli5yEbCM",20)
After that, it should return the same output below. Same with what we see when visiting the url
manually.