我有一个HTML服务应用程序,它要求上课的日期。选择日期时,它会为您提供复选框、类标题和类说明。您假设检查要参加的课程,然后单击参加按钮以记录数据。我接到日志的调用,因为它记录了一些东西,但它始终是"null"而不是选择的事件。
这是我 code.gs:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index.html').setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function listEvents(dateSelected) {
var calendar = CalendarApp.getCalendarById('CalendarID').getEventsForDay(new Date(dateSelected));
if (calendar[0] == null) {
var text = 'No classes for this date';
return text;
}
else {
var text = '<form id="eventsForm">';
for (var i=0; i < calendar.length; i++) {
text += "<input type='checkbox' name=";
text += i+1;
text += " value=";
text += calendar[i].getTitle();
text += ">";
text += calendar[i].getTitle();
text += ' - ';
text += calendar[i].getDescription();
text += '<br>';
}
text += "<input type='button' value='Attend' onclick='google.script.run.withSuccessHandler(test).processForm(this.parentnode)' />";
text += "</form>";
return text;
}
}
function processForm(x) {
Logger.log(x);
}
这是 HTML:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/redmond/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<div class="container" >
<div>
<h1>Welcome to the sign up page</h1>
<p>Please select a date for the class you'd like to attend below.</p>
<p>Click Here: <input type="text" name="date" id="datepicker" /><p>
</div>
</div>
<div id='test'></div>
<script>
$("#datepicker").datepicker();
$("#datepicker").on("change", function () {
var dateSelected = $(this).val()
google.script.run.withSuccessHandler(onSuccess).listEvents(dateSelected);
});
function onSuccess(eventText) {
document.getElementById('test').innerHTML=eventText;
}
</script>
当您单击参加按钮时,它会转到 processFrom in code.gs 并记录,但正如我所说,它只记录日期/时间和"null"而不是类。我只是想检查我是否得到了一些东西,以便我可以将人们检查的课程发送到电子表格以显示谁来了。关于如何让表单发回正确数据的任何想法?
当你看到它时,你会惊呆的!
将"this.parentnode"重命名为"this.parentNode"
在这里,您的脚本正在工作:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index.html').setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function listEvents(dateSelected) {
Logger.log("dateSeleted: "+dateSelected);
var selDate = new Date(dateSelected);
Logger.log(selDate);
var calendar = CalendarApp.getCalendarById('CalendarID').getEventsForDay(new Date(dateSelected));
if (calendar[0] == null) {
var text = 'No classes for this date';
return text;
}
else {
var text = '<form id="eventsForm">';
for (var i=0; i < calendar.length; i++) {
text += "<input type='checkbox' name=";
text += i+1;
text += " value=";
text += calendar[i].getTitle();
text += ">";
text += calendar[i].getTitle();
text += ' - ';
text += calendar[i].getDescription();
text += '<br>';
}
text += "<input type='button' value='Attend' onclick='google.script.run.withSuccessHandler(test).processForm(this.parentNode)' />";
text += "</form>";
return text;
}
}
function processForm(x) {
Logger.log("x: "+JSON.stringify(x));
}
这也发生在我身上,我讨厌当我犯这种错误并且我搜索几个小时的解决方案时。
祝你剩下的代码好运!