简体   繁体   English

Google表格自定义边栏重新加载表单提交后

[英]Google Sheets custom sidebar reload form on submit

I'm relatively new to Google Scripts and jQuery. 我是Google脚本和jQuery的新手。 Let's just say I probably know just enough to get myself into trouble! 假设我大概知道足以使自己陷入困境! I am trying to create a custom UI sidebar for entering data to a Google Sheet. 我正在尝试创建一个自定义UI边栏,以将数据输入到Google表格中。 I used the code from this post as a starting point (Thank you to Cooper for sharing!!): Cooper's Code 我以这篇文章中的代码为起点(感谢Cooper分享!): Cooper的代码

Everything works beautifully, but I need to automatically reset the form after each submission so it's ready to accept the next entry. 一切正常,但我需要在每次提交后自动重置表单,以便准备接受下一个条目。 Ideally, I would like a confirmation dialog with a button to make a new entry. 理想情况下,我想要一个带有按钮的确认对话框以进行新的输入。 I modified the code as follows, but my "New Entry" button does nothing: 我修改了代码,如下所示,但是“新建条目”按钮没有任何作用:

    <!DOCTYPE html>
        <html>
          <head>
            <base target="_top">
          </head>
          <body>
          <div id="data">
            <br />Text 1<input type="text" size="15" id="txt1" />
    <br />Text 2<input type="text" size="15" id="txt2" />
    <br />Text 3<input type="text" size="15" id="txt3" />
    <br />Text 4<input type="text" size="15" id="txt4" />
    <br /><input type="button" value="Log Entry" id="btn1" />
  </div>
  <div id="resp" style="display:none;">
    <h1>Your data has been received.</h1>
    <input type="button" value="New Entry" id="btn2" />
  </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $(function() {
        $('#btn1').click(validate);
        $('#btn2').click(openDialog);
        $('#txt4').val('');
        $('#txt3').val('');
        $('#txt2').val('');
        $('#txt1').val('')
      });

      function setResponse(a)
      {
        if(a)
        {
          $('#data').css('display','none');
          $('#resp').css('display','block');
        }
      }

      function validate()
      {
        var txt1 = document.getElementById('txt1').value || ' ';
        var txt2 = document.getElementById('txt2').value || ' ';
        var txt3 = document.getElementById('txt3').value || ' ';
        var txt4 = document.getElementById('txt4').value || ' ';
        var a = [txt1,txt2,txt3,txt4];
        if(txt1 && txt2 && txt3 && txt4)
        {
          google.script.run
            .withSuccessHandler(setResponse)
            .getData(a);
            return true;
        }
        else
        {
          alert('All fields must be completed.');
        }
      }

      function loadTxt(from,to)
      {
          document.getElementById(to).value = document.getElementById(from).value;
      }

     console.log('My Code');

     function openDialog() {
     var html = HtmlService.createHtmlOutputFromFile("index");
     html.setTitle('Gmf Sent Msg Form'); 
     SpreadsheetApp.getUi().showSidebar(html);
    }
   </script>
  </body>
</html>

I'm sure this is something simple that my newbie brain just isn't catching, but any help is appreciated! 我敢肯定,这很简单,我的新手大脑还没被抓住,但是任何帮助,我们都感激不尽!

If you want your dialog to work you need to do something like this: 如果您希望对话框正常工作,则需要执行以下操作:

code.gs 代码

function openMyDialog()
{
 var html = HtmlService.createHtmlOutputFromFile("index");
 html.setTitle('Gmf Sent Msg Form'); 
 SpreadsheetApp.getUi().showSidebar(html);
}

And in your javascript something like this: 在您的JavaScript中,如下所示:

function openDialog()
{
  google.script.run.openMyDialog();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM