在google-apps-script中,我有一个带有两个日期输入的html表单。我想将默认值设置为当天的前一周。
我无法使用以下脚本。谁能指出我做错了什么?
我收到"Object does not allow properties to be added or changed"
错误。
Code.gs:
function pickDates() {
var d = new Date();
var e = new Date();
d.setDate((d.getDate() - d.getDay() % 7) - 7);
e.setDate(e.getDate() - (e.getDay() + 1) % 7);
d = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
e = e.getFullYear() + '-' + (e.getMonth() + 1) + '-' + e.getDate();
var htmlB = HtmlService.createHtmlOutputFromFile('Pick Dates').setWidth(200).setHeight(200);
htmlB.d = d;
htmlB.e = e;
SpreadsheetApp.getUi().showModalDialog(htmlB, 'Select Dates');
}
选择Dates.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
Start Date<br>
<input id="start" type="date" name="startDay">
<br><br> End Date<br>
<input id="end" type="date" name="endDay">
<br><br>
<input type="button" value="Submit" onclick="google.script.run
.withSuccessHandler(google.script.host.close)
.sendEmails(this.parentNode)" />
</form>
<script>
document.getElementById('start').value = d;
document.getElementById('end').value = e;
</script>
</body>
</html>
答案 0 :(得分:2)
要evaluate()模板中的日期,您需要使用createTemplateFromFile()。该错误是因为createHtmlOutputFromFile()
返回的HtmlOutput对象无法添加htmlB.d = d;
之类的属性。您可以使用带scriptlets的html模板
function pickDates() {
var d = new Date();
var e = new Date();
d.setDate((d.getDate() - d.getDay() % 7) - 7);
e.setDate(e.getDate() - (e.getDay() + 1) % 7);
d = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
e = e.getFullYear() + '-' + (e.getMonth() + 1) + '-' + e.getDate();
// create a template
var htmlB = HtmlService.createTemplateFromFile('Pick Dates');
htmlB.d = d;
htmlB.e = e;
// evaluate() returns HtmlOuput object
var modal_html = htmlB.evaluate().setWidth(200).setHeight(200);
SpreadsheetApp.getUi().showModalDialog(modal_html, 'Select Dates');
}
选择Dates.html - 添加打印scriptlet
...
<script>
document.getElementById('start').value = <?= d ?>;
document.getElementById('end').value = <?= e ?>;
</script>
</body>