zondag 15 maart 2020

A Walk A Day Keeps The Doctor Away #GoogleAppsScript



function aWalkADayKeepsTheDoctorAway(){

 var file = DriveApp.getFilesByName("yyyyy").next(); //csv file with latitude/longitude points
 var fileBlob = file.getAs("text/csv");

 var fileData = Utilities.parseCsv(fileBlob.getDataAsString(), ",");
 var points = fileData.filter(function(row,index){return index>2}).map(function(row){return row[4].split(/geo:/)[1].split(",")}); 
  
 var directions = Maps.newDirectionFinder().setMode(Maps.DirectionFinder.Mode.WALKING).setOrigin(points[0]).setDestination(points[points.length-1]);
  for(var p=1;p<points.length;p++){
    directions.addWaypoint(points[p]);
  }
  
  var route = directions.getDirections().routes[0];
  var map = Maps.newStaticMap().addPath(route.overview_polyline.points);
 
  var distance  = route.legs.map(function(leg){return leg.duration.value}).reduce(function(accumulator,currentValue){return accumulator + currentValue;})/1000;
  
  var email = Session.getActiveUser().getEmail();
  MailApp.sendEmail
  (email,
  'A walk a day keeps the doctor away: ' + distance + " km",'Please open: ' + map.getMapUrl() + '&key=YOUR_API_KEY', 
    {
      htmlBody: '<br/><img src="cid:mapImage">',inlineImages: {mapImage: Utilities.newBlob(map.getMapImage(), 'image/png')                                                                }
    }
  );
  
}

Geen opmerkingen:

Een reactie posten