Calculate distance in Google spreadsheet | Winfred van Kuijk

Calculate distance in Google spreadsheet

By • Published: December 3, 2010 • Last updated: March 27, 2017 • Filed in: Software

Share
Seeing “internal error executing the custom function”? Appears to be an intermittent Google issue. Try again later.
Using Mapquest as provider and seeing errors? Most likely monthly quota of requests is used. See background tab for details of limitations.

Distance calculator v3.2This post started as a description of how to use Google’s API for distance calculations (with importXML), and there was a template as a proof of concept.

It has evolved into a distance calculator spreadsheet with custom functions using either Google’s or Mapquest’s API, with the ability to add results to a trip log.

Fill out From and To locations and the spreadsheet will calculate the distance and driving time based on the recommended route. You can copy the result to a trip log. Google account required.

Read the background to learn more about the options and the limitations.
For issues or requests, write a comment or contact me directly.


Is this spreadsheet saving you time/money?
Feel free to make a donation, to me or your favorite charity.

Instructions

Download the spreadsheet from the Google template directory.

December 2014: it is now possible to determine your current location. See release overview.
Got a minute? Do you mind rating the template?

253 Responses to “Calculate distance in Google spreadsheet”

  1. Naresh Kumar says:

    Dear Sir

    I would like to thank for you amazing work,But the distance calculator sheet has certain limitations which hampers our work,Please give a light on an issue which requires a fix.We are able to download only 50 directions. Which needs to be increased.

    Do let us know your feedback or any suggestion or alternative on the same is much appreciable.

    Regards
    Naresh Kumar

  2. Naresh Kumar says:

    if we make several route plan a day then show message
    “service invoked too many time of the day: route (line 485)”

    please give solution

  3. Blaine says:

    Winfred,

    Fantastic sheet. We would like to use it in a possible product. Can you email me so we can discuses what possibilities there are.

    Blaine

  4. Brenda says:

    Hi Winfred,
    Great template. I’m having a lot of trouble with the asTheCrowFlies function giving incorrect answers. I’ve tried both the original formula =if(and(C$21″”,C27″”),asTheCrowFlies(C$21,C27),””) with routeDistance replaced, and another from this page =asTheCrowFlies(C$21,C27,,,,true). routeDistance works fine in both, asTheCrowFlies returns a number that is consistently larger than the routeDistance (3,508 for Vancouver BC to New Orleans LA, whereas routeDistance is 2,750.
    Any suggestions?
    Thanks!

  5. avram says:

    Great job winfred!
    I already made a donation!

  6. Jake says:

    Winfred-

    This is an impressive spreadsheet. I wonder if you could help trouble shoot one I currently have? Its a spreadsheet that I am trying to do something similar and I am not having luck. My spreadsheet has 20 of my companies plant locations set in cells and then one variable cell. The idea is to set the shipping location in the variable cell and have the spreadsheet calculate the distance to each plant. I have been using the google built in api but I get the error – TypeError: Cannot read property “legs” from undefined. This error pops up at random times for random locations, as in it works 60% or so of the time and it will fail on locations and work on the same locations randomly.

    Do you know if there is a way I could use a portion of your mapquest api to accomplish this? I was going to attempt to set up a mapquest api but there site is shut down for free keys. Or do you know how this problem can be solved with the google api?

    Any help would be great and again nice spreadsheet. Wish I could put one that indepth together.

    • winfred says:

      Thanks Jake. You can use the spreadsheet, create a new sheet and put there the one variable and 20 locations. You could remove the other sheets, although I would recommend keeping the first one. E.g. use B1 as the variable cell, put the company plant locations in A2-A21. Then put the formula in B2-B21, e.g. in B2: =routeDistance($B$1,A2,,,,true) [the true to make sure miles are used].

      • Jake says:

        Thanks for the quick response. Is there anyway I could remove the small portion of code that I would need from the script and bring that function into my spreadsheet? I ask because I have a lot of other data and sheets in the spreadsheet that I am looking to add this to.

        • winfred says:

          Because it is set up to be used in many situations + configurations, it difficult to extract a small portion of the code. You could copy all the code though (go to script editor, create project and the script files and then copy all files). Otherwise use the contact form to reach out (for custom work a donation to me / your favorite cause is appreciated).

  7. Patrick says:

    How do I use the =asthecrowflies function with the multi-address tab?

    Everything I get is driving miles when I want short miles

    • winfred says:

      E.g. in cell D16 of multi-address (or any tab):
      =asTheCrowFlies(B6,C6) ==> results in 311 (km) -from Memphis, TN to Brentwood, TN- versus 334 km driving distance.

  8. TonyS says:

    Hi,

    Excellent work it’s a beautifully crafted spreadsheet. I am trying to create a simple mileage estimator for driving between locations in the UK, so that I input 2 locations and it autogenerates a mileage. How do I get it to default to, for example, Birmingham UK rather than Birmingham AL (I’ve set region to GB, is that enough?).

    Link here:
    https://docs.google.com/spreadsheets/d/1JV3ZL9ZGzBCHkYyKgmCa8-cOnkjXoY9Ip5WBTteGWog/edit?usp=sharing

    Please give me details for donating 🙂

    Many thanks

    • winfred says:

      Thanks! Setting region to GB should do it, but that does not work when using MapQuest as provider. You could always enter “Birmingham, UK” as
      Details for donating: see the PayPal link or send me direct message.

  9. MBS says:

    Thanks for the quick reply.

    Im assuming that when you say user, that is each separate Google account?

    Thanks

  10. MBS says:

    Hi, is it the individual sheet that has a limit of 2,500 requests per day?

    Is there a way around this? Maybe by creating several sheets.

    Thanks

  11. MBS says:

    Great work!

    Is there a way to choose any of the alternative routes and have these posted to the log sheet?

  12. Steve says:

    Is there a way to incorporate estimated drive time between multiple locations?

  13. SteveR says:

    Hi Winfred,

    As with many of the other comments here – great work, thank you!

    Is there any way of working out the travel time at different times of the day to account for traffic?

    Thanks

  14. Hans says:

    Hi Winfred,

    Nice functions and useful. I noticed unfortunately 2 issues:

    1. Almost every time when I set Google as a provider (or it has been set) and I’m opening the sheet the first time of the day, I see that the function has already been called too many times for that day. Not by me, since I couldn’t even start using it. I suppose this is shared by all users if the sheet and they must make many requests? If so, would it be possible to set your own key or something within this service, making the requests of a person unique?

    2. The quality of the Maprequest service is in many cases very poor (for the Netherlands). Even tough it catches the correct zip, then it still displays a wrong location. For example compare looking up ‘ Nijverdal’ with Google and Maprequest (situated in the East whereas Maprequest shows it in the South). The same applies to the city of ‘Hilversum’ (situated about the Middle of the Netherlands whereas Maprequest shows a village in the very North).

    Kind regards,
    Hans

    • winfred says:

      Thanks Hans.
      1. I see+hear this more often. Every user should have his/her own quota. Go to Tools > Script Editor. In the script editor go to Resources > Developers Console Project. This should show a popup with the project the script is currently associated with. Click that link. Under APIs & auth, click API’s and make sure the following are enabled:
      – Directions API
      – Geocoding API
      You can then select either, and click on Usage to see how much is being used. When I tested just now the usage stayed at 0, not clear why that is.
      I will keep an eye on: http://stackoverflow.com/questions/28948812/api-key-in-script-editor

      2. Agreed, Mapquest data is not always great. In what situation do you get the incorrect Hilversum?
      This is also because their different services can have different results (and pros and cons). Especially the nominatim vs the regular geocoding service.
      I plan to look at that for a future version.

      • Hans says:

        Hi Winfred,

        Thanks a lot!! While I verified the access to the Directions and Geocoding APIs, it appeared first when I clicked on the link for the project the script is associated with, that there has been an updated terms of service that I had to agree with, and a checkbox that I needed to check in order to agree with these updated terms. After doing so, I found out that these APIs were turned OFF for me. So then I simply switched them ON and then revisited the sheets with your distance calculator functions and yes it does work now!
        I am unsure whether this may or may not be applicable to other visitors but these steps are definitely worth checking!

        In regards to your question about Hilversum: when I enter this as “to” field in the “distance” tab (and for example choose ‘ Utrecht’ as “from” field, then the Mapquest data shows a village in North-Holland (in the neighbourhood of ‘ Alkmaar’ whereas expected would be slightly North above ‘Utrecht’). Honestly I must say that Google also plots ‘ Soest’ in Germany by only providing that name, but when further specifying the region, that is ‘ Soest, Utrecht’ then it does return the correct data. So I suppose both have their perks, be it that I still prefer Google. Many thanks for your background information!

        Thank you and kind regards,
        Hans

  15. Ron says:

    When using the mass address distance calculator how can I make it show the distance in more detail? I would like to see the exact driving distance not just rounded to the nearest mile.

    Thank you so much for this amazing program.

Leave a Reply to SteveR

« | Home | »

Loading...