# How to make a code triggers distance in gscript

**URL:** <https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624>\
**Category:** Ask for Help\
**Created:** [January 3, 2022, 10:18am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624 "2022-01-03T10:18:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadiernad](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/nadiernad/32/31563_2.png) [@nadiernad](https://community.glideapps.com/u/nadiernad)\
**Post date:** [January 3, 2022, 10:18am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/1 "2022-01-03T10:18:32Z")

</div>

I have a script code to determine the distance between the origin and destination addresses, how do I add a trigger code to automatically fill in the distance column, can anyone help me? here is the code

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 meters \* 0.000621371;  
break;  
case “minutes”:  
// get duration in seconds  
var duration = getTheLeg[“duration”][“value”];  
//convert to minutes and return  
return duration / 60;  
break;  
case “hours”:  
// get duration in seconds  
var duration = getTheLeg[“duration”][“value”];  
//convert to hours and return  
return duration / 60 / 60;  
break;  
case “kilometers”:  
return meters / 1000;  
break;  
default:  
return “Error: Wrong Unit Type”;  
}

}

---

<div class="post-metadata">

**Author:** ![ShantanuIyengar](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/shantanuiyengar/32/35546_2.png) [@ShantanuIyengar](https://community.glideapps.com/u/ShantanuIyengar)\
**Post date:** [January 3, 2022, 10:46am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/2 "2022-01-03T10:46:15Z")

</div>

You can have a time base trigger on the gscript.  
It may check all the rows where origin and dest exists but distance does not exist.  
And then you can calculate the distance and write the data onto the sheet.  
A few things to keep in mind:  
The gscript triggers are time based and not based on edit. So those can take time to run and then update your sheet which may take a little bit of more time to sync with glide.  
I can help you with an easier method.  
Use an experimental column to calculate distance or use inbuilt distance column

---

<div class="post-metadata">

**Author:** ![nadiernad](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/nadiernad/32/31563_2.png) [@nadiernad](https://community.glideapps.com/u/nadiernad)\
**Post date:** [January 3, 2022, 10:52am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/3 "2022-01-03T10:52:20Z")

</div>

what code should I add here so that I can create a trigger on change, and so that it automatically goes directly to the distance column in the spreadsheet

---

<div class="post-metadata">

**Author:** ![ShantanuIyengar](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/shantanuiyengar/32/35546_2.png) [@ShantanuIyengar](https://community.glideapps.com/u/ShantanuIyengar)\
**Post date:** [January 3, 2022, 10:53am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/4 "2022-01-03T10:53:09Z")

</div>

I would have to look at your sheet for that!  
its not plug and play as its dependent on column positioning in your sheet!

---

<div class="post-metadata">

**Author:** ![ShantanuIyengar](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/shantanuiyengar/32/35546_2.png) [@ShantanuIyengar](https://community.glideapps.com/u/ShantanuIyengar)\
**Post date:** [January 3, 2022, 11:04am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/6 "2022-01-03T11:04:27Z")

</div>

I would suggest you share this personally rather than open to the community!

---

<div class="post-metadata">

**Author:** ![nadiernad](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/nadiernad/32/31563_2.png) [@nadiernad](https://community.glideapps.com/u/nadiernad)\
**Post date:** [January 3, 2022, 11:09am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/7 "2022-01-03T11:09:53Z")

</div>

do you already know the answer

---

<div class="post-metadata">

**Author:** ![ShantanuIyengar](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/shantanuiyengar/32/35546_2.png) [@ShantanuIyengar](https://community.glideapps.com/u/ShantanuIyengar)\
**Post date:** [January 3, 2022, 11:19am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/8 "2022-01-03T11:19:41Z")

</div>

I will post the script you need to add in the gscript shortly,

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 3, 2022, 11:23am UTC](https://community.glideapps.com/t/how-to-make-a-code-triggers-distance-in-gscript/36624/9 "2022-01-03T11:23:26Z")

</div>

Have you considered the Glide [Distance](https://docs.glideapps.com/all/topics/computed-columns/primary/distance-to-location) column?

If that’s not suitable for your use case, and you really need to use a script, then my advice would be to turn it into an [Experimental Code Column](https://docs.glideapps.com/all/topics/computed-columns/primary/experimental-code-column). This would have the following benefits:

- You would get instant results in your app, without having to wait for data to sync to/from your Google Sheet
- You wouldn’t need to mess around with triggers
- You could re-use it across multiple apps
- You wouldn’t be subject to the limits that come with the Distance Column
