1

I want to get a text from a html form but I didn't find out how to recover the data. This worked fine in an older version of the form, and also when I reprogrammmed it due to deprecation of UI methods). But now it does not function anymore. I reprogrammed it and the resulting code shows the html form but I can't recover the data. When I click OK form does not react (when I click Exit, form closes).

Mi code is

Code 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 Answer 1

0

Try changing your html to something like this:

<!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>

Then your gs starts like this:

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

Here's another example

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your prompt replying. I made suggested changes but it didn´t work. During the process I found that <input onclick="formsubmit(this.parentNode)" type="button" value="OK" /> is not working. It doesn't matter what I write inside the function, it is not executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.