【问题标题】:Google script, html user form, getting data from html user form谷歌脚本,html用户表单,从html用户表单获取数据
【发布时间】:2021-02-14 22:32:30
【问题描述】:

我想从 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);
      }
    }

【问题讨论】:

    标签: html forms google-apps-script


    【解决方案1】:

    尝试将您的 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

    【讨论】:

    • 感谢您的及时回复。我进行了建议的更改,但没有奏效。在此过程中,我发现 不起作用。不管我在函数里面写什么,它都不会被执行。
    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多