在Google Script中制作一个Bootstrap表单

时间:2014-02-10 17:25:32

标签: javascript css google-apps-script

我们正在尝试使用Bootstrap 3创建一个表单,并且他们的响应应该转到Google电子表格,我们试图做但不起作用,我们的问题是在google脚本html页面加载bootstrap css,当按钮是将字段信息放入Google电子表格中。

谢谢。

Google脚本

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

    function setData(data) {
      var ss = SpreadsheetApp.openById("1uU2GtTjN8SYKQduar4y4n2u0EGeQFaznRddtrmNSYN8").getActiveSheet();
      var lastRow = ss.getLastRow();
      ss.getRange(lastRow+1, 1).setValue(data[0]);
      Logger.log(data[0]);
      return 0

}

Java脚本和Html

<script>
  window.addEventListener('load', function() {
    google.script.run.withSuccessHandler(getData).setData();
  });

  function getData(data) {
    var data = [];
    data[0] = document.getElementById("InputName").value;
    data[1] = document.getElementById("InputEmail").value;
    data[2] = document.getElementById("Inputtel").value;
    data[3] = document.getElementById("InputCPFCNPJ").value;
    data[4] = document.getElementById("InputEnterprise").value;
    data[5] = document.getElementById("InputMessage").value;

    google.script.run.setdata(data);
    return 0;
  }

    </script>

    <html lang="pt-br">
        <body>
            <form role="form">

                <div class="form-group col-xs-12">
                    <label class="sr-only" for="InputName">Nome</label>
                    <input type="text" class="form-control" id="InputName" placeholder="Nome">
                </div>

                <div class="form-group col-xs-12">
                    <label class="sr-only" for="InputEmail">Email</label>
                    <input type="email" class="form-control" id="InputEmail" placeholder="e-mail">
                </div>

                <div class="form-group">
                    <div class="form-inline">
                        <div class="form-group col-xs-6">
                            <label class="sr-only" for="Inputtel">Telefone</label>
                            <input type="tel" class="form-control" id="Inputtel" placeholder="Telefone">
                        </div>
                        <div class="form-group col-xs-6">
                            <label class="sr-only" for="InputCPFCNPJ">CPF ou CNPJ</label>
                            <input type="number" class="form-control" id="InputCPFCNPJ" placeholder="CPF ou CNPJ">
                        </div>
                    </div>
                </div>

                <div class="form-group col-xs-12">
                    <label class="sr-only" for="InputEnterprise">Empresa</label>
                    <input type="text" class="form-control" id="InputEnterprise" placeholder="Empresa">
                </div>

                <div class="form-group col-xs-12">
                    <label class="sr-only" for="InputMessage">Mensagem</label>
                    <textarea class="form-control" rows="3" id="InputMessage" placeholder="Mensagem"></textarea>
                </div>

                <div class="form-group col-xs-2">
                    <button class="btn btn-default" type="submit" onclick="getData();">
                    Enviar
                    </button>
                </div>

            </form>
            <!-- Bootstrap -->
            <script src="https://code.jquery.com/jquery.js"></script>
            <script src="js/bootstrap.min.js"></script>
        </body>
    </html>

1 个答案:

答案 0 :(得分:2)

首先,您需要正确导入引导程序CSS。 其次,您需要根据服务器端声明使用大写D调用服务器端函数setData。

<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script>
function getData(data) {
var data = [];
    data[0] = document.getElementById("InputName").value;
    data[1] = document.getElementById("InputEmail").value;
    data[2] = document.getElementById("Inputtel").value;
    data[3] = document.getElementById("InputCPFCNPJ").value;
    data[4] = document.getElementById("InputEnterprise").value;
    data[5] = document.getElementById("InputMessage").value;
     google.script.run.setData(data);
  }

</script>
<html lang="pt-br">
    <body>
        <form role="form">

            <div class="form-group col-xs-12">
                <label class="sr-only" for="InputName">Nome</label>
                <input type="text" class="form-control" id="InputName" placeholder="Nome">
            </div>

            <div class="form-group col-xs-12">
                <label class="sr-only" for="InputEmail">Email</label>
                <input type="email" class="form-control" id="InputEmail" placeholder="e-mail">
            </div>

            <div class="form-group">
                <div class="form-inline">
                    <div class="form-group col-xs-6">
                        <label class="sr-only" for="Inputtel">Telefone</label>
                        <input type="tel" class="form-control" id="Inputtel" placeholder="Telefone">
                    </div>
                    <div class="form-group col-xs-6">
                        <label class="sr-only" for="InputCPFCNPJ">CPF ou CNPJ</label>
                        <input type="number" class="form-control" id="InputCPFCNPJ" placeholder="CPF ou CNPJ">
                    </div>
                </div>
            </div>

            <div class="form-group col-xs-12">
                <label class="sr-only" for="InputEnterprise">Empresa</label>
                <input type="text" class="form-control" id="InputEnterprise" placeholder="Empresa">
            </div>

            <div class="form-group col-xs-12">
                <label class="sr-only" for="InputMessage">Mensagem</label>
                <textarea class="form-control" rows="3" id="InputMessage" placeholder="Mensagem"></textarea>
            </div>

            <div class="form-group col-xs-2">
                <button class="btn btn-default" type="submit" onclick="getData();">
                Enviar
                </button>
            </div>

        </form>

    </body>
</html>