Using Excel, you can create a trip calculator that uses web services to provide dynamic information that is up to date. I created this post after reading a post with a similar theme from Glen Gilchrist. In the example below, you’ll see that you can create the trip calculator using Excel functions and without needing to write any code.
Note: The illustrations below will work with Excel versions 2003 – 2010 by installing the Excel PowerUps Premium Suite. If you have Excel 2013, you can create this trip calculator without the add-in — just remove the “pwr” prefix from the function names in the examples below. Excel 2013 already has built in WEBSERVICE and FILTERXML functions!
Here’s the gist of how we’ll put this together
For the calculator, we’ll set up 3 user input cells.
- Your starting point
- Your destination
- Your average gas mileage
We’ll make two webservice calls.
- Call to Google’s Distance Matrix API (https://developers.google.com/maps/documentation/distancematrix/)
- Call to FuelEconomy.gov’s fuel prices API (http://www.fueleconomy.gov/feg/ws/index.shtml#fuelprices)
We’ll parse the XML to pull out Drive Time, Total Miles, and Average Price/Gallon.
The image below shows what a finished worksheet might look like. I used the pwrFORMULATEXT function in column D to help illustrate the actual function calls being used.
Let’s get started
Below I’ll give step-by-step notes on how I set up the functional part of the worksheet and note the Excel 2013 equivalent functions. I’m going to jump around on the sheet so that hopefully the data you’re gathering makes sense as you build up the sheet.
- In cell A2, enter the label “Enter Starting Place”. Add a box and/or shading to cell B2. This is where you’ll enter your starting point, which can be in the form of City, State or even a full address.
- In cell A3, enter the label “Enter Destination”. Add a box and/or shading to cell B3. This is where you’ll enter your ending point. This is where you’ll enter your ending point, which can be in the form of City, State or even a full address.
- In cell A15, enter the following formula. This will call the Google maps API using the pwrWEBSERVICE function and pass the start and end points using the pwrURLENCODE functions.
-
pwrWEBSERVICE("http://maps.googleapis.com/maps/api/distancematrix/xml?origins="&pwrURLENCODE(B2)&"&destinations="&pwrURLENCODE(B3)&"&mode=driving&language=en-us&sensor=false&units=imperial")
- In Excel 2013 use the following instead.
-
WEBSERVICE("http://maps.googleapis.com/maps/api/distancematrix/xml?origins="&ENCODEURL(B2)&"&destinations="&ENCODEURL(B3)&"&mode=driving&language=en-us&sensor=false&units=imperial")
-
-
- In cell A16, enter the following formula. This will call the FuelEconomy.gov’s fuel prices API.
-
pwrWEBSERVICE("http://www.fueleconomy.gov/ws/rest/fuelprices")
- In Excel 2013 use the following instead.
-
WEBSERVICE("http://www.fueleconomy.gov/ws/rest/fuelprices")
-
-
- In cell A5, enter the label “Drive Time”.
- In cell B5, enter the following formula to get the drive time from the XML returned from the web service.
-
pwrFILTERXML(A15,"/DistanceMatrixResponse/row/element/duration/text")
- In Excel 2013 use the following instead.
-
FILTERXML(A15,"/DistanceMatrixResponse/row/element/duration/text")
-
-
- In cell A7, enter the label “Total Miles”.
- In cell B7, enter the following formula to get the total distance in miles between the start and end points. The value returned also is being converted from meters to miles.
-
pwrCONVERTDISTANCE(pwrFILTERXML(A15,"/DistanceMatrixResponse/row/element/distance/value"),"m","mi")
- In Excel 2013 use the following instead.
-
CONVERT(FILTERXML(A15,"/DistanceMatrixResponse/row/element/distance/value"),"m","mi")
-
-
- In cell A8, enter the label “Enter your average fuel economy”. Add a box and/or shading to cell B8. This is where you’ll enter your average fuel mileage.
- In cell A9, enter the label “Average price/gallon”.
- In call B9, enter the following formula to get the average price/gallon from the XML returned from the web service. The data is returned from this web service as text so you’ll also need to use the VALUE function to convert the text to a numeric value.
-
VALUE(pwrFILTERXML(A16,"/fuelPrices/regular"))
- In Excel 2013 use the following instead.
-
VALUE(FILTERXML(A16,"/fuelPrices/regular"))
-
-
- In cell A11, enter the label “Trip Fuel Cost”.
- In cell B11, enter the following formula.
-
B9*(B7/B8)
-
Watch it below
Give it a try for yourself
At this point you should be able to enter a starting point, ending point, and your average MPG and the worksheet will give you the distance, drive time, and fuel costs for the trip. Have fun and try it out!
There you go.
This does not seem to work with Excel 2013, is there something that needs to enabled, like some setting?
Did you modify the formulas and remove the “pwr” prefix on the function names? Alternatively, you can just install the Excel PowerUps Premium Suite add-in and not have to remove the function name prefixes.
https://officepowerups.com/downloads/excel-powerups-premium-suite/
The fuel economy webservice line is working but the google maps one i not working any chance of an update??
Seems to work. As a test, I tried http://maps.googleapis.com/maps/api/distancematrix/xml?origins=flagstaff&destinations=denver&mode=driving&language=en-us&sensor=false&units=imperial and this link returned XML.
First off, great site and resource…
My initial WEBSERVICE call returns a #NAME error in the spreadsheet cell. Yet the same command works fine with the manually entered “Flagstaff and Denver” as above. I suspect Google has changed something related to the URLENCODE functions and the cell references.
There was a typo in the Excel 2013-based formula that caused the #NAME error. This has been fixed. Sorry!
The example works well. Where do the headers go when some sort of authentication is required?
The WEBSERVICE (and pwrWEBSERVICE) function doesn’t have any particular support for authentication.
Is this Excel function also subject to the request limits from Google? What if I have a large set of origin and destination pairs (say 1000 pairs) to get the durations and distances? Thanks.
Hi,
Yes, this function would be subject to limits of any service you pointed to.
Do you need an api key from google to do this?
In this example I’m not using an API key. Maybe for higher volumes?
hi is there a limit to distance i tried using cross country zipcodes and got error values. either that or it says a cross country trip from new york to la is one day which is not true. anyone ever encounter this?
That’s just how Google calculates the elapsed time. I suppose it assumes zero traffic and wide open highways.