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!
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:
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
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
Related
I am trying to complete a form created in google app script web app via URL with the intention to display the field but be inactive and a UUID for form submit.
In web app testing the form is completed by URL eg: https://script.google.com/macros/s/AKfycbwam4eZHPrs2ooVQLzJZgMcFb9gfXC2OPSq8f7Xkp9z/dev?fn=bob filling "bob" into field "fn".
However to remove googles "This application was created by another user, not by Google.Terms of Service" we put the web app in and iframe or link in google sites. This throws a spanner in the works.
Any advise will be much appreciated
function doGet() {
return HtmlService.createTemplateFromFile("page").evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function userClicked(userInfo){
const url = "https://docs.google.com/spreadsheets/d/1-7D4bXhm1Yx0ekRUmrwr8fTsdQ0dLtMI4gYG-THPUdk/edit#gid=0";
const ss = SpreadsheetApp.openByUrl(url);
const ws = ss.getSheetByName("data");
ws.appendRow([userInfo.firstName,userInfo.lastName,userInfo.app,new Date()])
// Logger.log(name + " Clicked The Button");
}
function include(filename){
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
});
document.getElementById("btn").addEventListener("click",doStuff);
function doStuff(){
const userInfo = {};
userInfo.firstName = document.getElementById("fn").value;
userInfo.lastName = document.getElementById("ln").value;
userInfo.app = document.getElementById("app").value;
google.script.run.userClicked(userInfo);
document.getElementById("fn").value = "";
document.getElementById("ln").value = "";
var myApp = document.getElementById("app");
myApp.selectedIndex = 0;
M.FormSelect.init(myApp);
}
</script>
<script>
google.script.url.getLocation(function(location){
document.getElementById('fn').value = location.parameters.fn[0]
document.getElementById('ln').value = location.parameters.ln[0]
})
</script>
Unfortunately there is no way to pass a query string parameter into a Google Apps Script web app embedded in Google Sites but this might be done by using client-side JavaScript on a "regular" web page.
Note: Use the /exec URL, not the /dev one.
<body onload="loadIframe();">
<div id="content">
<iframe width="100%" height="100%" frameborder="0"></iframe>
</div>
<script>
function loadIframe(){
const url = "put_here_your_web_app_url" + window.location.search;
document.querySelector('iframe').src = url;
}
</script>
</body>
Related
Google Apps Script Gadget URL parameters are ignored when embedded in a Google Site
google site: pass parameters to embedded script
Google Apps script get Parent URL to iFrame in Javascript
I'm trying to pass var 'id' from the page 'a.html' to 'b.html'. The var content comes from 'code.gs' as below:
code.gs
function data(){
var id = 1;
return id;
}
Next, I get this var and I show it in 'a.html':
a.html
<?
var id = data();
?>
<h1><?= id ?></h1>
Go to B.html
By clicking 'Go to B.html', the system directs the user to there. I need to bring the same value of var 'id' from the page 'a.html' to 'b.html'.
Ps: searching for a little, I saw that there's a kind to send this var by the command 'localStorage', but it's not working for me. :(
Can anybody help me?
Use localstorage
a.html
localStorage.setItem('id',1)
b.html
var id = localStorage.getItem('id')
the other way is to put it in a js file and import it in both html
Storing & Retrieving html data on the server
Client Side JavaScript:
<script>
function saveId(v) {
google.script.run.saveKeyValue({key:'id',value:v});
}
function getId() {
google.script.run
.withSuccessHandler(function(v){
alert('The value is ' + v );
})
.getKeyValue('id');
}
</script>
Server Side Google Apps Script:
function saveKeyValue(obj) {
PropertiesService.getScriptProperties().setProperty(obj.key,obj.value);
}
function getKeyValue(key) {
return PropertiesService.getScriptProperties().getProperty(key);
}
You could also replace PropertiesService with CacheService.
Client To Server Communications
Properties Service
I lurk this site all the time, but I think I have exhausted my options to find the info that I need.
I just want a button to trigger acrobat to run the browseForDoc() function, take the cPath the user selects and use that as the path for the saveAs() function. It seems like it should be easier than it has been. That is a folder-level script for adobe acrobat.
I get an error of "missing ; before statement 14". Besides that, it doesn't work. Please let me know what I am doing wrong.
//get windows user name
var getLoginName = app.trustedFunction(
function(){
//Get and return the user's login name
app.beginPriv();
return identity.loginName;
app.EndPriv();
}
);
var fileExt = ".pdf";
var filePath = function(cFS, cPath, bPromptToOverwrite){
this.cFS:cFS;
this.cPath=cPath;
this.bPromptToOverwrite=bPromptToOverwrite;
}
//Open save dialog,return user input values and saveas
var mySaveDialog = app.trustedFunction(
function browse(cPath,oDoc){
app.beginPriv();
app.browseForDoc(){bSave: true, cFilenameInit: oDoc, cFSInit: ""};
return this.cPath;
var myPath = new filePath{cPath: browse.cPath};
this.saveAs(myPath,myFile+fileExt);
app.endPriv();
}
);
I need to add some URL parameters in the Send Email button in Email related list in Case detail page.
I have added a custom detail page button with execute javascript. Below is the button code.
var loc;
var uRoleId = UserInfo.getUserRoleId();
if(
"{uRoleId}" == "General Permissions Team"){
loc = "/ui/core/email/author/EmailAuthor?p2_lkid=0031500001e729m&rtype=003&p3_lkid=5001500000TkUwp&retURL=%2F5001500000TkUwp&p26=xyz#gmail.com";
}
else{
loc = "/ui/core/email/author/EmailAuthor?p2_lkid=0031500001e729m&rtype=003&p3_lkid=5001500000TkUwp&retURL=%2F5001500000TkUwp&p26=test#gmail.com";
}
window.top.location.href = loc;
Now when I click on the button , I get a javascript error that user info is not defined.
Can you help me point out whats the issue with the button code.
UserInfo is not available in JavaScript. Instead you need to use {!$UserRole.Name} to get the role.
Try this:
var loc;
var uRoleName = "{!$UserRole.Name}";
if( uRoleName == "General Permissions Team"){
loc = "/ui/core/email/author/EmailAuthor?p2_lkid=0031500001e729m&rtype=003&p3_lkid=5001500000TkUwp&retURL=%2F5001500000TkUwp&p26=xyz#gmail.com";
}
else {
loc = "/ui/core/email/author/EmailAuthor?p2_lkid=0031500001e729m&rtype=003&p3_lkid=5001500000TkUwp&retURL=%2F5001500000TkUwp&p26=test#gmail.com";
}
window.top.location.href = loc;
I am using google API to detect client location however it always shows to me "Your Location Was Not Detected By Google Loader".
I need to find country code of the client location however it is not working for me.
<script src="http://www.google.com/jsapi" language="javascript"></script>
<script language="javascript">
if (google.loader.ClientLocation != null) {
document.write("Your Location Is: " + google.loader.ClientLocation.address.country_code);
} else {
document.write("Your Location Was Not Detected By Google Loader");
}
</script>
ClientLocation object returns null when Google cannot geolocate your IP address. This could happen because of many reasons, one being the API cannot resolve your IP address.
Maybe you can try other solutions such as
http://html5demos.com/geo
or ask your server to do this.
Try this:
<script type="text/javascript">
if(google.loader.ClientLocation) {
var latitude = google.loader.ClientLocation.latitude;
var longtitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var region = google.loader.ClientLocation.address.region;
var country = google.loader.ClientLocation.address.country;
var countrycode = google.loader.ClientLocation.address.country_code;
} else {
// ClientLocation not found or not populated
// so perform error handling
}
</script>
Change
if (google.loader.ClientLocation != null) {
to
if (google.loader.ClientLocation){
Hope, this helps.