e.parameter未定义,但存在于stringify中

时间:2019-08-26 16:01:25

标签: javascript html google-apps-script google-apps google-apps-script-web-application

我正在将HTML表单数据用于Google脚本。当我使用e.parameter.mesic时,它是undefined,但是当使用JSON.stringify时,它存在。

我不知道问题出在哪里。

这是html表单(使用引导程序4):

<!doctype html>
<html lang = "en">
<head>
    <base target = "_top">
    <!-- Required meta tags -->
    <meta charset = "utf-8">
    <meta content = "width=device-width, initial-scale=1, shrink-to-fit=no" name = "viewport">

    <!-- Bootstrap CSS -->
    <link crossorigin = "anonymous" href = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity = "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" rel = "stylesheet">
</head>
<body>
<form onsubmit = 'generate.disabled = true;' class = "form-horizontal">
    <fieldset>
        <!-- Select Basic -->
        <div class = "form-group">
            <label class = "col-md-4 control-label" for = "mesic">Měsíc:</label>
            <div class = "col-md-4">
                <select class = "form-control" id = "mesic" name = "mesic" required>
                    <option value = "1">Leden</option>
                    <option value = "2">Únor</option>
                    <option value = "3">Březen</option>
                    <option value = "4">Duben</option>
                    <option value = "5">Květen</option>
                    <option value = "6">Červen</option>
                    <option value = "7">Červenec</option>
                    <option value = "8">Srpen</option>
                    <option value = "9">Září</option>
                    <option value = "10">Říjen</option>
                    <option value = "11">Listopad</option>
                    <option value = "12">Prosinec</option>
                </select>
            </div>
        </div>

        <!-- Button (Double) -->
        <div class = "form-group">
            <label class = "col-md-4 control-label" for = "generate"></label>
            <div class = "col-md-8">
                <button type= "submit" class = "btn btn-success" id = "generate" name = "generate" onclick="google.script.run.pomoc(this.form)" >Vygenerovat</button>
                <button class = "btn btn-danger" onclick = "google.script.host.close()" id = "cancel" name = "cancel">Zrušit</button>
            </div>
        </div>

    </fieldset>
</form>

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script crossorigin = "anonymous" integrity = "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" src = "https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script crossorigin = "anonymous" integrity = "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script crossorigin = "anonymous" integrity = "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" src = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>

这是Google脚本:

function pomoc(e) {
//  return SpreadsheetApp.getUi().alert(JSON.stringify(e));
  return SpreadsheetApp.getUi().alert(e.parameter.mesic);

1 个答案:

答案 0 :(得分:0)

问题:

  • e是由输入name:value对形式的对象。它没有名为parameter
  • 的密钥

解决方案:

  • 使用正确的name作为键来访问表单对象中的值。

摘要:

SpreadsheetApp.getUi().alert(e.mesic); //OR e["mesic"]

参考文献:

  • Form object
      

    如果您使用表单元素作为参数调用服务器函数,则表单将成为一个单独的对象,其字段名作为键,字段值作为值