谷歌脚本、html用户表单、从html用户表单获取数据

时间:2021-02-14 22:32:30

标签: html forms google-apps-script

我想从 html 表单中获取文本,但我没有找到如何恢复数据。这在旧版本的表单中运行良好,并且当我由于 UI 方法的弃用而对其重新编程时)。但现在它不再起作用了。我对其重新编程,结果代码显示了 html 表单,但我无法恢复数据。 当我点击确定表单没有反应(当我点击退出时,表单关闭)。

小米码是

代码 GS

var authorName=''; 
    
function onOpen() {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('IM@ICHOS Menu')
          .addItem('Identifiy User', 'showDialog')
          .addSeparator()
          .addSubMenu(ui.createMenu('Verification')
              .addItem('Show User', 'showUser'))
          .addToUi();
    }
function showDialog() {
      try {
             var ss = SpreadsheetApp.getActiveSpreadsheet();
             html = HtmlService.createHtmlOutputFromFile('InputAuthor');
             SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
                .showModalDialog(html, 'Input author name');
            ss.toast('Author ->'+authorName);
          } 
      catch (event) {
               Browser.msgBox("Error report of [Monitored dialogues]/showDialog: " + event.message);
          }
    }

    //getValuesFromForm
function getValuesFromForm(form){
      try {
        authorName = form.authorName;
        return;
    } 
        catch (event) {
        Browser.msgBox("Error report of [Software and Data Status]/getValuesFromForm: " + event.message);
      }
    }

InputAuthor.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <b>Identifies Author</b><br /><br />
    Please, input your eMail(without domain) to allow proper identification of changes authorship.<br />(message from ICHOS/IM Team)<br />
    <br />

      <b>Input User</b>: <input id="authorName" name="authorName" type="text" />
      <input onclick="formSubmit()"                       type="button" value="OK"   />
      <input onclick="google.script.host.close()"         type="button" value="Exit" />
    </form>

    <script type="text/javascript">

      function formSubmit() {
      google.script.run
          .getValuesFromForm(document.getElementById('InputAuthor'));
          .withFailureHandler(myFailureFunction)
          .withSuccessHandler(mySuccessFunction)
      }
      
      function mySuccessFunction() {
      alert("Done!");
      google.script.host.close();
    }

      function myFailureFunction(argError) {
      alert("There was an error: " + argError.message);
      google.script.host.close();
    }


    function getValuesFromForm(form){
      try {
        authorName = form.authorName;
        return;
    } 
      catch (event) {
        Browser.msgBox("Error report of [Software and Data Status]/getValuesFromForm: " + event.message);
      }
    }

1 个答案:

答案 0 :(得分:0)

试着把你的 html 改成这样:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form>
      <input name="authorName" type="text" />
      <input onclick="formSubmit(this parentNode);" type="button" value="OK"   />
      <input onclick="google.script.host.close();" type="button" value="Exit" /><!--this only works for dialogs not webapps -->
    </form>

    <script type="text/javascript">

      function formSubmit(obj) {
      google.script.run
          .getValuesFromForm(obj));
          .withFailureHandler(myFailureFunction)
          .withSuccessHandler(mySuccessFunction)
      }
  </body>
</html>

然后你的 gs 是这样开始的:

function getValuesFromForm(values) {
  const authorname=values.authorName;
}

Here's another example