【问题标题】:Save the email ID of the user filling the form保存填写表单的用户的电子邮件 ID
【发布时间】:2020-09-16 13:13:49
【问题描述】:

我有一个 Google 表单来收集在偏远地区工作的员工的信息

Emp No *
Punch *
Customer details / mode or travel

数据进入具有以下结构的 Google 电子表格

Timestamp   Emp No  Punch   Remark  Name    GeoCode GeoAddress  Email

我可以通过以下脚本捕获用户的 GPS 坐标。我制作了一个网络应用程序(任何人甚至匿名都可以运行)并要求用户单击链接。

我不能做什么:

我想保存填写表单的用户的电子邮件 ID(或员工编号)。但是电子邮件 ID 没有被捕获到表单中。如果我填写表格,则会捕获电子邮件 ID。对于其他用户,它不会被捕获。我不希望所有用户都验证脚本(以登录用户身份运行脚本)。它必须通过其他方式捕获。有可能吗?

如果 GPS 未被捕获(它为空),我想在 HTML 页面中显示不同的消息。怎么做?

代码.gs

function doGet() {
    return HtmlService.createHtmlOutputFromFile("Index");
}
//
function getLoc(value) {
  var destId = FormApp.getActiveForm().getDestinationId() ;
  var ss = SpreadsheetApp.openById(destId) ;
  var respSheet = ss.getSheetByName("Location");
  var numResponses = respSheet.getLastRow();
  var currentemail = Session.getActiveUser().getEmail();
  var c=value[0]; var d=value[1];
  var e=c + "," + d ;
  //respSheet.getRange(numResponses,6).setValue(e);
  //respSheet.getRange(numResponses,8).setValue(currentemail);
  var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
  var f= response.results[0].formatted_address;
  //respSheet.getRange(numResponses,7).setValue(f);
  respSheet.getRange(numResponses,6,1,3 ).setValues([[ e, f, currentemail ]]);
}
//

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>
<body>
<p>Please ensure your GPS is on to record your location. You can generate the report from website to check. Pl. close this window (version 3)</p>
</body>
</html>

【问题讨论】:

  • 请他们填写
  • 他们可以填写错误的员工编号。我想避免它。此外,我想记录谁在没有做任何事情(身份验证)的情况下填写了它。 PL。帮我解决第二个问题,这可能不是很难
  • 这是 gsuite 吗?您如何期望脚本凭空知道一些事情?如果他们不进行身份验证,脚本将如何知道有关执行脚本的用户的任何信息?关于第二个问题,如答案中所说,使用if...else客户端或服务器端withSuccessHandler。

标签: javascript authentication google-apps-script web-applications


【解决方案1】:

问题

我想保存填写表单的用户的电子邮件 ID(或员工编号)。但是电子邮件 ID 没有被捕获到表单中。如果我填写表格,则会捕获电子邮件 ID。对于其他用户,它不会被捕获。我不希望所有用户都验证脚本(以登录用户身份运行脚本)。它必须通过其他方式捕获。有可能吗?

在使用 Google Apps 脚本创建以自动获取用户电子邮件 ID 的网络应用程序上,您可以将网络应用程序设置为作为运行应用程序的用户执行,而不是像您一样执行,但如果不想使用此功能 那么您必须设置自己的身份验证过程。

问题

如果 GPS 未被捕获(它为空),我想在 HTML 页面中显示不同的消息。怎么做?

使用 JavaScript 条件表达式

function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      alert('Can\'t get the position');
    }
})() 

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);
 }
}

上面的代码使用alert,但你可以使用DOM。

资源

【讨论】:

  • 对不起@Ruben,如果 GPS 关闭,用户不会收到警报
  • 我合并了编辑。现在警报没有发生(如果 GPS 关闭)并且没有记录位置(即使 GPS 开启)。
  • 请。请参阅我上面 9 月 16 日的回答——一些用户报告说他们的出拳没有被记录下来。我进行了测试,我可以看到出拳没有被记录下来。我想在打孔后显示工作表中的“最后一条记录”。这将作为对用户的肯定确认,即打孔已记录在工作表中。我做不到。 @Rubén 你能帮帮我吗?
  • HTML 表单对大多数用户来说都可以正常工作。但是,它不适用于某些用户。我要求用户清除历史记录并注销,然后尝试。尽管如此,一些用户仍无法记录位置。该脚本也没有在其他浏览器(如 UCm 歌剧、火狐等)中创建记录(通过单击 web 应用程序)。
【解决方案2】:

我能够在没有任何谷歌表单(仅 HTML)的情况下制作完整的解决方案,并且还设法显示警报消息。仍然无法“登录”。

代码.gs

它运行表单并将所需列中的答案保存到谷歌表中。 它比谷歌表单运行得更快,并且只需单击一次“提交”。 由于通过“追加行”进行保存,因此避免了在我之前的方法中发生的数据混乱(行之间)。


/* @Include JavaScript and CSS Files */
function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
  .getContent();
}

/* @Process Form */
function processForm(formObject) {
  var url = "https://docs.google.com/spreadsheets/d/...../edit#gid=52499297";
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Location");
  var response = Maps.newGeocoder().reverseGeocode(formObject.lat, formObject.long);
  var address= response.results[0].formatted_address;
  ws.appendRow(
    [
      new Date(),
      formObject.empno,
      formObject.punch,
      formObject.rem,
      "",
      formObject.lat+","+formObject.long,
      address
    ]
  );
}



索引.html

这里有问题。

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <?!= include('JavaScript'); ?>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-6">
                <form id="myForm" onsubmit="handleFormSubmit(this);">
                    <p class="h4 mb-4 text-left">Record Attendance and Location</p>

                    <div class="form-group">
                        <label for="empno">Emp No - <a href="https://docs.google.com/spreadsheets/d/1yhcUpfhvyPcWyDkwglJRJwMn7VnBYEHaAjz959JC0wk/edit#gid=0">Click to see list</a></label>
                        <input type="number" class="form-control" id="empno" name="empno"  min="1" max="9999999"  required>
                    </div>

                    <div class="form-group">
                        <label for="punch">Punch (Select one)</label>
                        <select class="form-control" id="punch" name="punch" required>
                            <option selected disabled hidden style='display: none' value=''></option>
                            <option value="In">In</option>
                            <option value="Out">Out</option>
                            <option value="Started">Started</option>
                            <option value="Reached">Reached</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="rem">Remark</label>
                        <input type="text" class="form-control" id="rem" name="rem">
                    </div>


                    <div class="form-group">
                        <input type="hidden" class="form-control" id="lat" name="lat">
                        <input type="hidden" class="form-control" id="long" name="long">
                    </div>

                    <button type="submit" class="btn btn-primary btn-block">Submit</button>
                </form>

                <div id="output"></div>
            </div>
        </div>
    </div>
</body>

</html>


JavaScript.html

这会处理答案

<script>
    function showPosition() {
        navigator.geolocation.getCurrentPosition(showMap);
    }

    function showMap(position) {
        // Get location data
        var lat = position.coords.latitude;
        var geo1 = document.getElementById("lat");
        geo1.value = lat;
        var long = position.coords.longitude;
        var geo2 = document.getElementById("long");
        geo2.value = long;
    }

    // Prevent forms from submitting.
    function preventFormSubmit() {
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
            forms[i].addEventListener('submit', function(event) {
                event.preventDefault();
            });
        }
    }
    window.addEventListener('load', preventFormSubmit);
    window.addEventListener('load', showPosition);

    function handleFormSubmit(formObject) {
        google.script.run.processForm(formObject);
        document.getElementById("myForm").reset();
        alert('Data saved successfully');
        
    }
</script>

【讨论】:

    猜你喜欢
    • 2012-10-17
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2016-03-22
    • 2011-11-08
    • 2014-08-05
    相关资源
    最近更新 更多