如何为scriptDB中的每个项添加新的div标签?
当我到达" while(results.hasNext())&#34时,看起来代码卡住了。部分,可能是因为我在HTML的脚本部分使用了Apps Script语法。当我用一个简单的div.innerHTML等替换整个while循环时,我能够使脚本工作。线
index.html文件如下所示:
<form id="myForm">
<p>Name of Alert: <input name="alertName" type="text" /></p>
<select name="frequency">
<option value="everyDay">Every day</option>
<option value="everyWeek">Every week</option>
</select>
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateAlertList)
.processForm(this.parentNode)" />
</form>
<script>
function updateAlertList(results) {
var div = document.getElementById('output');
while (results.hasNext()) {
var result = results.next();
div.innerHTML = '<a href="http://www.google.com">' + result.alertName + '</a>';
}
}
</script>
Code.gs文件中包含此内容:
function processForm(formObject) {
var formAlertName = formObject.alertName;
var formFrequency = formObject.frequency;
var db = ScriptDb.getMyDb();
var alert = {
alertName: formAlertName,
frequency: formFrequency
};
var record = db.save(alert);
var results = db.query({});
return results;
}
更新:当我将其添加到code.gs
时工作var arrayResults = [];
while (results.hasNext()) {
arrayResults.push(results.next());
}
return arrayResults;
和index.html
for (var i=0;i<results.length;i++)
{
alert(results[i].alertName);
div.innerHTML = '<a href="http://www.google.com">' + results[i].alertName + '</a>';
}
答案 0 :(得分:0)
您需要在gs中处理结果(迭代器)并创建一个结果数组以在javascript中完成。