Google 使用 url 查询进行表单验证

Google Form validation with url query

抱歉我措辞不准确,但我对 HTML/js/app 脚本不是很熟悉。

我们每个月都会与大量客户合作,并与他们一起组织活动。我们的每一位客户都有一个唯一的代码,这是参加我们活动所必需的。

我们严重依赖 Google 表格来让客户预订他们的位置。在表格上,我们要求他们提供代码,但我们没有任何类型的验证。

最近我们获得了访问数据库 API 的权限,该数据库可用于 URL 查询(例如 www.mydatabase.com/q=CODE)。如果“代码”有效,则返回非空 JSON 值。

我正在寻找一种将 google 表单 validation feature 与 URL 查询一起使用的方法。 我想在我们的表格中有一个初始部分,我们的客户需要在其中输入他们的代码。如果有效,则向他们显示表格的其余部分,否则他们将无法继续。

我四处查看并获得了 google 支持,但我找不到使用查询进行验证的方法。 我错过了什么吗?

如果没有办法这样做,那么最容易和最直接的路径是什么?

我们正在使用 google 表单,因为它很方便,而且我是我组中唯一的程序员,所以我希望让我的同事尽可能简单,而不需要他们编写任何代码. 理想的是在 google 表单上有一个一键式插件,自动生成部分(或 HTML 页面)。

编辑:更具体的问题

替代解决方法[已更新]

  • 正如 Cooper 在评论中提到的那样,Google 表单不可能在用户之前 运行 网站 check/query是否被定向到实际的 Google 表单,取决于检查。
  • 检查后,URL 验证仅检查输入的文本值是否采用正确的 URL 格式,例如https://example.com & 看来您无能为力了。

也许您可以尝试使用 Web app, Apps Script functions & HTML redirection

组合使用的更新解决方法

它是如何工作的?

You'll need to create a web-app URL using Apps Script that will run first the input.html web-page. The web-page will ask user to input the CODE to complete your database query via your URL www.mydatabase.com/q=CODE. Then, the script will decide if the complete database URL with inputted CODE returns a non-empty JSON value, user gets routed to the Google Form. If not, nothing will happen & user will get a browser alert that says Code is not valid.

注意:我还想提醒您,这只是一种解决方法并且它有一个限制,因为用户仍然可以复制实际的 Google 表单 link,因为这只是 URL.

的重定向

脚本代码

function checkSite(c){
  try{
    var response = UrlFetchApp.fetch("www.mydatabase.com/q="+c); //to complete your database query. Just edit this with your actual URL
    var data = JSON.parse(response.getContentText());
    if(Object.keys(data).length > 0){
      return true; //if URL returns JSON value
    }
    return false; //if URL returns empty JSON value
  }catch(e){
    return false;
  }
}

function passCode(code){
  return checkSite(code);
}

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('input');
}

input.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script> 
      function passData(){
        var code = document.getElementById("inputCode").value; //<-- PASS THIS DATA TO GOOGLE SCRIPT (The .gs file)
        google.script.run.withSuccessHandler(onSuccess).passCode(code);
      }

      function onSuccess(result) {
        route(result);
      }

      function route(result) {
        if(result == true){
          alert("Routing to Google Form");
          window.location.href="https://YOUR_GOOGLE_FORMS_LINK";  
        }
        alert("Code is invalid");
      }

      </script>
  </head>
  <body>
    <div align="center">
        <label for="code">Input Code:</label>
        <input type="text" id="inputCode" name="inputCode"><br><br>
        <button onclick='passData()' id="btnContinue">Continue</button>
    </div>
  </body>
</html>

示例结果

  • 下面是 input.html 网页的样子

  • 如果 URL 查询 returns 上的 CODE 是一个非空 JSON 值,用户会收到一个浏览器警告,显示 Routing to Google Form,然后用户最终被路由到 Google 表单

  • 否则,用户会收到一个浏览器警告,提示 Code is invalid 并且它永远不会将用户路由到 Google 表单