Close Htmlservice popup

别说谁变了你拦得住时间么 提交于 2019-12-11 17:03:58

问题


I am wondering if is possible to close the htmlservice popup when the submit button is hit on the Google form

function launchForm() {
  var form = FormApp.openById('15Qw9jmolybvMbx2d3UhddEWHrKe0zBiV5_asKXolsM0');
  var formUrl = form.getPublishedUrl(); 

//  var response = UrlFetchApp.fetch(formUrl);
//  var formHtml = response.getContentText();

  var htmlApp = HtmlService
      .createHtmlOutput('<h1>Your Organization</h1>')
      .append('<iframe src ="' + formUrl + '?embedded=true" width="1000" height="900" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>')
      .setTitle('Form')
      .setWidth(1000) 
      .setHeight(1000);

  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

I'm hoping that when I hit the submit button on the form, the html app will close


回答1:


Google forms load each page on form navigation or submit. You can listen to those events, count the number of page loads and close the pop up.

Sample:

code.gs:

function showGoogleForm() {
  var form = FormApp.openById(/*FORM EDIT ID*/);
  var formUrl = form.getPublishedUrl();
  var temp = HtmlService.createTemplateFromFile('googleForm');
  temp.pubUrl = formUrl;
  var htmlApp = temp
    .evaluate()
    .setTitle('Form')
    .setWidth(1000)
    .setHeight(1000);
  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

googleForm.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <script type="text/javascript">
      var i = 0;
      var totalSections = 1; //TODO Set Total number of pages in your form
      check = () => {
        if (++i > totalSections) {
          alert('Thank You for filling up the form');
          setTimeout(google.script.host.close, 7);
        }
      };
    </script>
  </head>
  <body>
    <h1>
      MASTER FORM
    </h1>
    <iframe
      onload="check()"
      referrerpolicy="no-referrer"
      sandbox="allow-scripts allow-forms"
      src="<?=pubUrl?>?embedded=true"
      width="700"
      height="520"
      frameborder="0"
      marginheight="0"
      marginwidth="0"
    ></iframe>
  </body>
</html>


来源:https://stackoverflow.com/questions/57727771/close-htmlservice-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!