我目前正在使用以下教程在侧栏中显示谷歌表数据:https://yagisanatode.com/2018/02/12/how-to-get-something-from-google-sheets-and-display-it-in-the-sidebar-in-google-apps-script/#more-219
但是,我想在我的html文件中添加一个“for”循环来显示一些复选框(参见下面的代码)。最后,我想从我的电子表格中将数字“16”更改为“rangeResult”值。你能提供一些关于如何做的建议吗?
Code.gs - 功能
function numEarlyRelease() {
var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var numValues = ss.getRange('R19').getValue();
return numValues;
}
Page.html文件
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h3><script> document.write(new Date().toLocaleDateString()); </script></h3>
<div id="rangeResult"></div>
<script>
function addRange(rangeStartEnd){
$('#rangeResult').text(rangeStartEnd);
};
for (var i = 1; i <=16; i++) {
document.write('<br><input type="checkbox" name="scores" id="i" value="i">'+ i);
}
google.script.run.withSuccessHandler(addRange).numEarlyRelease();
</script>
<br><input type="button" value="Submit Check-In" onclick="google.script.host.close()" />
<input type="button" value="Close" onclick="google.script.host.close()" />
</body>
</html>
谢谢您的帮助!
函数调用 -
google.script.run.withSuccessHandler(addRange).numEarlyRelease();
意味着如果函数numEarlyRelease()
返回一个值,在本例中为numValues
,它可用作函数addRange()
的参数。 addRange()
采用参数rangeStartEnd
,因此它将具有numValues
的值。您可以随意使用它。举个例子,如果是一个数字 -
for (var i = 1; i <=rangeStartEnd; i++) {
document.append('<br><input type="checkbox" name="scores" id="i" value="i">'+ i);
}
注意:除非您想要覆盖页面中的所有内容,否则请勿使用document.write()
。
谢谢你的建议!以下是我的解决方案。 (我已经改变了一些变量的名称以适应我的项目。)
code.公司
function earlyRelease(e) {
var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var numValues = ss.getRange('R19').getValue();
var studentName = ss.getSheetValues(20, 18, numValues, 1);
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('Early Release')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html); }
function earlyReleaseList() {
var doc = SpreadsheetApp.openById("1OF6Y1CTU9dkIgd1P-nw-5f2lqHSS5cGZytndwzJhw-o");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var numValues = ss.getRange('R19').getValue();
var studentNames = ss.getSheetValues(20, 18, numValues, 1);
return studentNames; }
page.html中
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<script>
function addStudents(studentList){
$('#rangeResult').text(studentList);
document.write(new Date().toLocaleDateString());
var students = [];
//The following loop was added to the function
for (var i = 0; i < studentList.length; i++) {
document.write('<br><input type="checkbox" name="students" id="i" value="i">'+ studentList[i]);
}
//Also added the buttons to the function with document.write rather than html tags outside of the function
document.write('<br><input type="button" value="Submit Early Release" onclick="google.script.host.close()" />');
document.write('<input type="button" value="Close" onclick="google.script.host.close()" />');
};
google.script.run.withSuccessHandler(addStudents).earlyReleaseList();
</script>
</body>
</html>