Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get geolocation on submit in Google Forms using Google Script

I have a Google Form where I want to get the user geolocation along with the inputs. Currently, I'm able to get it by making the user click on a url after he submits the answers. This is the code in Google Script that does it:

function doGet() {
    return HtmlService.createHtmlOutputFromFile("Index");
}


function getLoc(value) {
  var destId = FormApp.getActiveForm().getDestinationId() ;
  var ss = SpreadsheetApp.openById(destId) ;
  var respSheet = ss.getSheets()[0] ;
  var data = respSheet.getDataRange().getValues() ;
  var headers = data[0] ;
  var numColumns = headers.length ;
  var numResponses = data.length;
  var c=value[0]; var d=value[1];
  var e=c + "," + d ;
   if (respSheet.getRange(1, numColumns).getValue()=="GeoAddress") {    
       respSheet.getRange(numResponses,numColumns-2).setValue(Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm:ss"));
       respSheet.getRange(numResponses,numColumns-1).setValue(e);
       var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
       f= response.results[0].formatted_address;
       respSheet.getRange(numResponses,numColumns).setValue(f);
     }
   else if (respSheet.getRange(1,numColumns).getValue()!="GeoAddress") {
       respSheet.getRange(1,numColumns+1).setValue("GeoStamp");
       respSheet.getRange(1,numColumns+2).setValue("GeoCode");
       respSheet.getRange(1,numColumns+3).setValue("GeoAddress");
  }
}

And the Index.html file:

<!DOCTYPE html>
<html>
<script>
(function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
      }
})()      
function showPosition(position){
 var a= position.coords.latitude;
 var b= position.coords.longitude;
 var c=[a,b]
 getPos(c)
 function getPos(value){
 google.script.run.getLoc(value);
 }
}
</script>
</html>

Instead of making the user click on a url, I want the geolocation to be automatically inputed into the response sheet as the user submits the form. I'm able to make the getLoc function run on a submit trigger, however the html function doesn't run. I believe it might be because the doGet function is already a trigger, but it requires a browser page to be opened in order to run. If that's the case, the ideal solution would be to redirect the browser to the Google Script url after the user submits it, but I can't seem to find a way to do it. Is this the correct approach and how can I make it work?

Thanks in advance!

like image 717
prolifebel Avatar asked Nov 16 '22 21:11

prolifebel


1 Answers

Maybe this works: https://www.youtube.com/watch?v=J93uww0vMFY

Tutorial on Geotagging (Geo-stamp and Time-stamp) google forms. Add info on Latitude, Longitude, and Address (Street name and number, city, state, zip code, and country) of a device submitting google forms. Linking user’s location within google forms. Google forms integration with google maps.

Links to download the scripts are given below:

  1. Code.gs : https://www.youtube.com/redirect?v=J93uww0vMFY&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1D4vTzUGZAf3_ZDVMeW760i1KoZCn37un&redir_token=VQ2rLeQvyQea-mq9_kGhh_Kxihd8MTU5MTgxOTM2OEAxNTkxNzMyOTY4

  2. Index.html : https://www.youtube.com/redirect?v=J93uww0vMFY&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1mYZGYNrXNOxUD8DlDmRdBajy1nc3FKtw&redir_token=VQ2rLeQvyQea-mq9_kGhh_Kxihd8MTU5MTgxOTM2OEAxNTkxNzMyOTY4

like image 127
Wolkuz Avatar answered Jan 10 '23 11:01

Wolkuz