使用 Google Script 在 Google 表单中提交时获取地理位置

pro*_*bel 5 html javascript google-apps-script google-forms

我有一个 Google 表单,我想在其中获取用户地理位置和输入。目前,我可以通过让用户在提交答案后单击 url 来获取它。这是 Google Script 中的代码:

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");
  }
}
Run Code Online (Sandbox Code Playgroud)

和 Index.html 文件:

<!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>
Run Code Online (Sandbox Code Playgroud)

我希望在用户提交表单时自动将地理位置输入到响应表中,而不是让用户单击 url。我可以让 getLoc 函数在提交触发器上运行,但是 html 函数不运行。我相信可能是因为 doGet 函数已经是一个触发器,但它需要打开浏览器页面才能运行。如果是这种情况,理想的解决方案是在用户提交后将浏览器重定向到 Google Script url,但我似乎找不到办法做到这一点。这是正确的方法吗?我怎样才能使它起作用?

提前致谢!

Wol*_*kuz 1

也许这有效:https://www.youtube.com/watch ?v=J93uww0vMFY

\n

有关地理标记(地理标记和时间戳)Google 表单的教程。\n添加有关提交 Google 表单的设备的纬度、经度和地址(街道名称和门牌号、城市、州、邮政编码和国家/地区)的信息。在 Google 表单中链接用户\xe2\x80\x99s 位置。\nGoogle 表单与 Google 地图集成。

\n

下载脚本的链接如下:

\n
    \n
  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_Kxihd8M TU5MTgxOTM2OEaxNTkxNzMyOTY4

    \n
  2. \n
  3. 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_Kxihd8MTU 5MTgxOTM2OEaxNTkxNzMyOTY4

    \n
  4. \n
\n

  • 您好,Wolkuz,感谢您的贡献!对于这个问题和未来的答案,您可能需要考虑一些建议: 1. 不要简单地在教程中链接,而是花时间在您的答案中进行总结。该链接可以作为您详尽答案的补充。2. 请使用编辑器格式化链接,以便看到的只是链接标题而不是整个链接。再次感谢并继续使用 stackoverflow! (2认同)
  • 知道了。我是这里的新人。 (2认同)