简体   繁体   English

URL 表单预填

[英]URL form pre-fill

I am using the following code to create a custom HTML form (deployed as a web app) with Google Apps Script:我正在使用以下代码使用 Google Apps 脚本创建自定义 HTML 表单(部署为网络应用程序):

function getData(a)
{
  var ts = Utilities.formatDate(new Date(), "GMT-6", "yyyy-MM-dd' 'HH:mm:ss");
  a.push(ts);
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Login').appendRow(a);
  return true;
}

function doGet()
{
  var html = HtmlService.createHtmlOutputFromFile('index');
  return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

index.html索引.html

  <div id="data">
    <br />Text 1<input type="text" size="15" id="txt1" />
    <br />Text 2<input type="text" size="15" id="txt2" />
    <br />Text 3<input type="text" size="15" id="txt3" />
    <br />Text 4<input type="text" size="15" id="txt4" />
    <br /><input type="button" value="submit" id="btn1" />
  </div>
  <div id="resp" style="display:none;">
    <h1>Response</h1>
    <p>Your data has been received.</p>
  </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $(function() {
        $('#btn1').click(validate);
        $('#txt4').val('');
        $('#txt3').val('');
        $('#txt2').val('');
        $('#txt1').val('')
      });

      function setResponse(a)
      {
        if(a)
        {
          $('#data').css('display','none');
          $('#resp').css('display','block');
        }
      }

      function validate()
      {
        var txt1 = document.getElementById('txt1').value || ' ';
        var txt2 = document.getElementById('txt2').value || ' ';
        var txt3 = document.getElementById('txt3').value || ' ';
        var txt4 = document.getElementById('txt4').value || ' ';
        var a = [txt1,txt2,txt3,txt4];
        if(txt1 && txt2 && txt3 && txt4)
        {
          google.script.run
            .withSuccessHandler(setResponse)
            .getData(a);
            return true;
        }
        else
        {
          alert('All fields must be completed.');
        }
      }

      function loadTxt(from,to)
      {
          document.getElementById(to).value = document.getElementById(from).value;
      }

     console.log('My Code');
   </script>

The code works as expected and is collecting responses in the spreadsheet however I would like to pre-fill some fields using the URL.该代码按预期工作,并正在电子表格中收集响应,但是我想使用 URL 预先填写一些字段。 I know you can do this with the regular Google Forms system.我知道您可以使用常规的 Google 表单系统来做到这一点。

Is this possible with the web app URLs? Web 应用程序 URL 可以做到这一点吗?

You can do this by sending url query parameters: 您可以通过发送url查询参数来做到这一点:

https://script.google.com/macros/[ID]?txt1=value1&txt2=value2

Then, onload , retrieve the query parameters and update it in your form. 然后, onload ,检索查询参数并在您的表单中对其进行更新。

Sample Index.html snippet: 示例Index.html代码段:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>
    $(function() {
      google.script.url.getLocation(function(location) {
        var locObj = location.parameter;
        $('#btn1').click(validate);
        Object.keys(locObj).forEach((key) => {$("#"+key).val(locObj[key])})
      });
    });

References: 参考文献:

Can I refresh this question lacking understanding how the script answer above refers to form fields "txt1" and "txt2" getting completed and refer to detail found in Form Field Does Not Complete By URL In Iframe or Google Site - but does as independent web app我是否可以刷新这个问题,但不了解上面的脚本答案是如何完成完成的表单字段“txt1”和“txt2”,并参考在 Iframe 或 Google 站点中的“表单字段未按 URL 完成”中找到的详细信息 - 但作为独立的网络应用程序执行

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM