将 URL 参数传递给 HTML 表单 Google Web 应用程序

Pass URL Parameters to HTML Form Google Web App

我已经解决了与此相关的大部分问题,但还没有找到对我有帮助的解决方案(我已经一一应用了)。我有一个 HTML 表单,我正在通过 google 作为 Web 应用程序发布。我需要用该参数预填充一个输入框。

code.gs

function doGet() {
return HtmlService
        .createTemplateFromFile('html')
        .evaluate();      
          }

html.html

<!DOCTYPE html>
<html>
<head>
</head>
<style>
</style>
<body>
<form>
  <h1><center>Verification</center></h1>
  Form ID:<input type="number" name="formid" id="formid" class="formid">
</form>
</body>
</html>

正如我所说,我在类似问题中尝试了很多建议,但似乎找不到一个可行的建议。 ID 是我可以输入我的 url + ?formid= 并将其填充到输入中。我怎样才能做到这一点?

谢谢!

使用Jquery:

<script>
$(function(){
  google.script.run
  .withSuccessHandler(function(v){
    $('#formid').val(v);
   })
   .getTheValueOfV();
});
</scrip>

常规JavaScript

<script>
window.onload=function(){
  google.script.run
  .withSuccessHandler(function(v){
     document.getElementById('formId').value=v;
   })
  .getTheValueOfV();
};
</script>

Code.gs:

function getTheValueOfV() {
  var ss=SpreadsheetApp.getActive();
  return ss.getRangeByName("TheRangeWhereYouFindtheValueofv").getValue();
}

您可以选择将脚本放在头部或正文中。如果你使用 JQuery 你需要在头部有这样的东西。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  • 当使用url + ?formid=###的URL访问Web Apps时,您想将###的值放入文本框。
    • URL 就像 https://script.google.com/macros/s/###/exec?formid=###

如果我的理解是正确的,这个答案怎么样?请将此视为几个答案之一。

修改点:

  • 在此修改中,我使用 google.script.url.getLocation() 来检索查询参数。

修改后的脚本:

请修改html.html如下。

<!DOCTYPE html>
<html>
<head>
</head>
<style>
</style>
<body>
<form>
  <h1><center>Verification</center></h1>
  Form ID:<input type="number" name="formid" id="formid" class="formid">
</form>
<script>
  google.script.url.getLocation(function(location) {
    document.getElementById("formid").value = location.parameters.formid[0];
  });
</script>
</body>
</html>
  • 通过上述修改,当您使用https://script.google.com/macros/s/###/exec?formid=12345访问Web Apps时,12345被放入文本框。

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。