Newbie here- trying to use this google sheets script for a new formula in excel (calculating straight line distances between zip codes), but from what I understand about coding (which is very little) it's in the wrong "language". Anyone able to help "translate"/convert this so I can use this in excel? Or point me in the right direction of some resources to learn how to do it? Thanks!


function DrivingMeters(origin, destination) {
var directions = Maps.newDirectionFinder()
// .setMode(Maps.DirectionFinder.Mode.TRANSIT)
.setOrigin(origin)
.setDestination(destination)
.getDirections();
if (directions && directions.error_message) throw directions.error_message
if (directions && directions.routes && directions.routes[0] && directions.routes[0].legs && directions.routes[0].legs[0] && directions.routes[0].legs[0].distance)
return directions.routes[0].legs[0].distance.value;
return "";
}

function DrivingMiles(origin, destination) {
return DrivingMeters(origin, destination)/1609.34;
}

function FlyingMeters(origin,destination) {
Utilities.sleep(1000)
var geoOrigin = Maps.newGeocoder().geocode(origin);
Utilities.sleep(1000)
var geoDestination = Maps.newGeocoder().geocode(destination);
return haversine({lat:geoOrigin.results[0].geometry.location.lat,lng:geoOrigin.results[0].geometry.location.lng},
{lat:geoDestination.results[0].geometry.location.lat,lng:geoDestination.results[0].geometry.location.lng});
}

function FlyingMiles(origin,destination) {
return FlyingMeters(origin,destination)/1609.34;
}

function haversine(latLng1, latLng2) {
var radius = 6371000;
var dLat = (latLng2.lat-latLng1.lat) * Math.PI/180;
var dLng = (latLng2.lng-latLng1.lng) * Math.PI/180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(latLng1.lat * Math.PI/180) * Math.cos(latLng2.lat * Math.PI/180) *
Math.sin(dLng/2) * Math.sin(dLng/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = radius * c;
return d;
}