Всплывающая форма не закроется после отправки в Google Sheets - PullRequest
0 голосов
/ 18 сентября 2018

Я использую свою функцию openDialog для отображения пользовательской формы HTML.Он отправит штраф, но не закроет всплывающее окно после отправки формы.Я хотел бы, чтобы эта форма либо закрылась после отправки, либо показала "thank-you.html" с кнопкой "Закрыть".

.gs

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('index.html');
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
    .showModalDialog(html, 'Pop up Form');
}

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('index').setTitle('Adding Rows');
}

function doPost(e) {
  Logger.log(e);
}

function sendText(data) {
  var sheet = SpreadsheetApp.openById("some id").getActiveSheet();
  sheet.appendRow([data.form_field_1, data.form_field_2, data.form_field_3, data.form_field_4]);
  return 'success!';
}

HTML

<html>

<head>
  <base target="_top">
</head>

<body>
  <form onsubmit="sendText(event)" id="test-form">
    <div>
      <label>Field 1</label>
      <input type="text" name="form_field_1" placeholder="Field 1" />
    </div>
    <div>
      <label>Field 2</label>
      <input type="text" name="form_field_2" placeholder="Field 2" />
    </div>
    <div>
      <label>Field 3</label>
      <input type="text" name="form_field_3" placeholder="Field 3" />
    </div>
    <div>
      <label>Field 4</label>
      <input type="text" name="form_field_4" placeholder="Field 4" />
    </div>
    <div>
      <button type="submit" id="submit-form">Submit</button>
    </div>
  </form>
  <script>
    function sendText(e) {
      e.preventDefault();

      var data = {
        form_field_1: e.target['form_field_1'].value,
        form_field_2: e.target['form_field_2'].value,
        form_field_3: e.target['form_field_3'].value,
        form_field_4: e.target['form_field_4'].value
      }

      google.script.run
        .withSuccessHandler(function(response) {
          console.log(response);
          window.close();
        })
        .sendText(data);
    }
  </script>
</body>

</html>

Не уверен, что я делаю не так

Ответы [ 2 ]

0 голосов
/ 24 сентября 2018

Найдя исправление, сделав это.

<script>
    function sendText(e) {
      e.preventDefault();

      var data = {
        form_field_1: e.target['form_field_1'].value,
        form_field_2: e.target['form_field_2'].value,
        form_field_3: e.target['form_field_3'].value,
        form_field_4: e.target['form_field_4'].value
      }

      google.script.run.withSuccessHandler(function(response) {
        console.log(response);
        google.script.host.close()
      })
      .sendText(data);
    }
</script>
0 голосов
/ 18 сентября 2018

Вам нужно что-то вроде этого:

function closepopup()
{
    document.getElementById('test-form').style.display = 'none';
}
  
function sendtext()
{
    if(jQuery('#test-form').valid())
    {
        var formurl = 'submit url'; 
        jQuery.ajax({
          type: "POST",
          url: formurl,
          data: jQuery('#test-form').serialize(true), 
          success: function() 
          {
              // you can add here redirect the current page or success message
              closepopup();
          }
        });
    }
}
<div>
  <button type="submit" id="submit-form" onclick="sendtext()">Submit</button>
</div>

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...