Mind Mining Medium

Google Sheets: Get Neighborhood from Address or Query

If you need you get the neighborhood based an address or location name, it’s pretty easy using Google App Scripts and the HERE API. You’ll need to supply your own HERE API key. They have a freemium model that is pretty generious (250k transactions per month).

First, in your Google Spreadsheet, create an “Apps Script”. As of 2021, you can do this by going to Extentions > Apps Script. Ths will open a new Apps Script file.

Inside the Apps Script site, rename the project to getNeighborhood and paste the following code inside Code.gs.

const hereAPIKey = '<YOUR_HERE_API_KEY>';

function getNeighborhood(query) {
  query = query.trim();

  if (!query || !query.length) {
    throw new Error('<query> cannot be empty');
  }

  const encodedAddress = encodeURIComponent(query);
  const URL = `https://geocode.search.hereapi.com/v1/geocode?apiKey=${hereAPIKey}&q=${encodedAddress}`;
  const results = UrlFetchApp.fetch(URL);
  const data = JSON.parse(results);
  return data['items'][0]['address']['district'];
}

Be sure to save and click Debug. Confirm permissions once they pop up and you’re set.

Back inside your spreadsheet, you can use the new function like so:

=getNeighborhood("312 W Adams St, Chicago")
=getNeighborhood("Sears Tower, Chicago")
=getNeighborhood(A2)

Source: https://github.com/karbassi/apps-script-getNeighborhood