Geocoding with Google Sheets and Google Apps Script¶
Revisiting Functions¶
Open up your Google Sheet from part 1.
Go to column for Location
and remember what column it is, for me it is C
:
Next, add two columns, one for lat
and another for lng
:
Now click on Extensions
Now click on Apps Script
When you first launch, you will see a blank myFunction() select it and get ready to paste over it:
Copy and paste the following code into the entire script:
- This defines the column as latitude to be
lat
- This defines the column as longitude to be
lng
cells[i][2]
the2
isnumber
that your address column is in minus 1!! You have to subtract 1 because JavaScript starts counting at0
!!! For example, column A is0
!- Make sure this column letter matches your
**latitude**
column!! - Make sure this column letter matches your
**longitude**
column!!
JavaScript arrays start at 0
Most programming have two types of indexing, 0
indexed or 1
indexed, which means the number that they start counting lists from. JavaScript is 0
indexed, meaning a list with 4 things starts from 0
and ends at 3
, like this: [0,1,2,3]
.
This is important when we call items from a list and want to get the right item from it!! For example, we have to get the fourth item in the example array like this, let theFourthItem = myArray[3]
.
Click on the Save Icon to save your script:
Click on the Run button to test the script:
You should get a prompt asking for Authorization
, click Review permissions:
Select your Google Account to continue:
Click on Advanced:
Click on Go to Untitled Project (unsafe)
Click on Allow
Do you trust yourself?
Essentially, anyone can write Google Scripts, so Google is making sure that the script is associated with your account before giving it access to this particular spreadsheet.
Click on Save:
If it ran successfully then you should now see latitude
and longitude
filled in the Google Sheet!
Geocoding on each submission with a trigger¶
Go back to the Google Scripts and click on the triggers icon
Click on Add Trigger:
Click on Select event type:
Change to On Form Submit so that everytime the the form gets submitted a new record gets latitude/longitude added too!
Click Save:
A pop-up should appear, but if you have a pop-up blocker like on FireFox, then you may have to click on Options
:
Then Allow this particular popup to appear.
Authorization again?
This is the same authorization as before, but it is for the Trigger
not the application!
Select your Google Account to continue:
Click on Advanced:
Click on Go to Untitled Project (unsafe)
Click on Allow
Click on Save:
Congratulations, now each time a form gets submitted you will be able to map the locations:
โฝIn-class Exercise #2 - Test your form!¶
Tasks
- Add 2-3 locations to your Google Form and see if the new locations work.
- Check to see if the locations are accurate or not!
- What type of locations do not show up?
Answer
Locations that do not show up are those that are blank or are not able to be located by the Google Geocoder.
Publishing your survey¶
Now that our data is able to be geocoded, the final step is to publish the spreadsheet so we can can bring it into our HTML file through JavaScript next week.
Go to File:
Click on Sharing:
Click on Publish to web:
Click on Webpage:
Choose CSV:
Click Publish:
If this warning pops-up click on OK:
Copy the URL in the address bar:
Paste it into your dataUrl variable like so:
const dataUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSNq8_prhrSwK3CnY2pPptqMyGvc23Ckc5MCuGMMKljW-dDy6yq6j7XAT4m6GG69CISbD6kfBF0-ypS/pub?output=csv"
Uncomment the loadData(url)
function to test if it’s working:
Since there is only a console.log()
in the loadData()
function, you have to open the browser’s console to check.
๐ Checkpoint¶
Your init.js
should look like the following:
If you see something in the console, then you move onto looping through the data and adding markers to the map!