e.parameter 未定义,但存在于 stringify 中
e.parameter is undefined, but present in stringify
我正在为 Google 脚本使用 HTML 表单数据。当我使用 e.parameter.mesic
时,它是 undefined
,但是当使用 JSON.stringify
时,它存在。
不知道问题出在哪里
这是 html 表格(使用 bootstrap 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);
问题:
e
是由表单中的输入 name:value
对构成的对象。它没有名为 parameter
的密钥
解决方案:
- 使用正确的
name
作为键访问表单对象中的值。
片段:
SpreadsheetApp.getUi().alert(e.mesic); //OR e["mesic"]
参考文献:
- Form object
如果您使用表单元素作为参数调用服务器函数,表单将成为一个对象,字段名称作为键,字段值作为值
我正在为 Google 脚本使用 HTML 表单数据。当我使用 e.parameter.mesic
时,它是 undefined
,但是当使用 JSON.stringify
时,它存在。
不知道问题出在哪里
这是 html 表格(使用 bootstrap 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);
问题:
e
是由表单中的输入name:value
对构成的对象。它没有名为parameter
的密钥
解决方案:
- 使用正确的
name
作为键访问表单对象中的值。
片段:
SpreadsheetApp.getUi().alert(e.mesic); //OR e["mesic"]
参考文献:
- Form object
如果您使用表单元素作为参数调用服务器函数,表单将成为一个对象,字段名称作为键,字段值作为值