function calculateDistances() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var startRow = 6; // Starting row for addresses
  var endRow = 300; // Ending row for addresses
  var startColumn = 1; // Column A
  var endColumn = 2; // Column B
  var outputDistanceColumn = 4; // Column C
  var outputDurationColumn = 5; // Column D

  for (var i = startRow; i <= endRow; i++) {
    var origin = sheet.getRange(i, startColumn).getValue();
    var destination = sheet.getRange(i, endColumn).getValue();
    
    if (origin && destination) {
      var directions = Maps.newDirectionFinder()
        .setOrigin(origin)
        .setDestination(destination)
        .setMode(Maps.DirectionFinder.Mode.DRIVING)
        .getDirections();
      
      var distanceText = directions.routes[0].legs[0].distance.text;
      var durationText = directions.routes[0].legs[0].duration.text;
      
      var distance = parseFloat(distanceText.replace(/,/g, '').replace(' mi', ''));
      var duration = convertDurationToFractionalHours(durationText);
      
      sheet.getRange(i, outputDistanceColumn).setValue(distance.toFixed(2));
      sheet.getRange(i, outputDurationColumn).setValue(duration.toFixed(2));
    }
  }
}

function convertDurationToFractionalHours(durationText) {
  var hours = 0;
  var minutes = 0;
  var days = 0;
  
  var dayMatch = durationText.match(/(\d+)\s*day/);
  var hourMatch = durationText.match(/(\d+)\s*hour/);
  var minuteMatch = durationText.match(/(\d+)\s*min/);
  
  if (dayMatch) {
    days = parseInt(dayMatch[1]);
  }
  
  if (hourMatch) {
    hours = parseInt(hourMatch[1]);
  }
  
  if (minuteMatch) {
    minutes = parseInt(minuteMatch[1]);
  }
  
  var totalHours = (days * 24) + hours + (minutes / 60);
  return totalHours;
}