Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The code below gets the distance between 2 different addresses and returns the distance in miles, kilometers or minutes. My understanding was that I could execute my app-script in a web app using google.script.run.googleMaps();

My question is how do I use input fields as my from and two columns to replicate the same result in a web app?

function googleMaps(start_address,end_address, return_type){

   var mapObj = Maps.newDirectionFinder();
   mapObj.setOrigin(start_address);
   mapObj.setDestination(end_address);
   var directions = mapObj.getDirections();

   var getTheLeg = directions["routes"][0]["legs"][0];

   var meters = getTheLeg["distance"]["value"];

  switch(return_type){
    case "miles" :
      return Math.floor(meters * 0.000621371);
      break;
    case "kilometers" :
      return meters / 1000;
      break;
    case "minutes" :
      var duration = getTheLeg["duration"]["value"];
      return duration / 60;
      break;
    default :
      return "Error: Wrong unit type";
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

Passing parameters to a WebApp

  • A WebApp must contian a function doGet(e) (or alternatively doPost(e)), which will be the first function to be run when the WebApp is called
  • When you deploy a WebApp, the WebApp URL will be something like this: https://script.google.com/a/XXXXX.eu/macros/s/XXXXXXXXXXXXXXXXXXX/exec

  • If you want to pass paramters to the url, you should do it like https://script.google.com/a/XXXXX.eu/macros/s/XXXXXXXXXXXXXXXXXXX/exec?start_address=AAA&end_address=BBB&return_type=CCC

  • Those parameters can be retrieved within the WebApp as
function doGet(e){
 var start_address = e.parameter.start_address;
 var end_address = e.parameter.end_address;
 var return_type = e.parameter.return_type;
 ...
}
  • Now, you can completely integrate your function within doGet() - you do not need google.script.run.googleMaps(); for it

  • If you do want to call your function with google.script.run, note that it is a method that calls an Apps Script function from within client-side (Javascript)
  • If you want to use it, it means that first you need to create a client-side with HtmlService
  • Second, you will need to pass the retrieved parameters from Apps Script to Javascript and back

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...