Good evening,
I have a spreadsheet where I record my daily driving mileage when working. The column headings are: Date, Point A, Point B, Point C, Point D, Point E, Point F, Point G, and Trip Mileage.
Currently, I manually use Google Maps to determine driving distance between points. What I'd like to see happen instead is for a script to pull the points data from my spreadsheet, determine distance between the points and total distance of each trip (i.e. row), and insert the total in the last column.
I believe I have this essentially all set up except I can't figure out how to get the distance data from Google Maps. From reading similar question, I sense someone might tell me to refer to the Maps Distance Matrix API. I've referred to this. I'm a beginner programmer, but it appears to me to be oriented toward apps, not Google docs. Is this the case? Or can it be made to serve my project?
Thank you in advance for all of your wonderful advice, comments, suggestions, and encouragement!!!
BTW, here is my code currently:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mileageLogSheet = ss.getSheetByName("Mileage Log");
var dataRange = mileageLogSheet.getDataRange(); //Returns a Range corresponding to the dimensions in which data is present.
var numRows = dataRange.getNumRows();
var numCol = dataRange.getNumColumns(); //numCol = 9.
//Returns the range with the top left cell at the given coordinates (row, column) with the given number of rows and columns.
var rangeToCompute = mileageLogSheet.getRange(10, 2, numRows-9, numCol-1);
var address1,
address2;
for(var rowCoord = 1; rowCoord < numRows-9; rowCoord++){
for(var colCoord = 1; colCoord < 8; colCoord++){
if(rangeToCompute.getCell(rowCoord, colCoord).getValue() != ""){
address1 = rangeToCompute.getCell(rowCoord, colCoord).getValue();
address2 = rangeToCompute.getCell(rowCoord, colCoord+1).getValue();
var directions = Maps.newDirectionFinder()
.setOrigin(address1)
.setDestination(address2)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
//Nope, this doesn't work!:
var distanceValue = directions.routes[0].legs[0].distance.value;
var totalTripDistance;
}
}
}
}