我理解为什么我的代码不起作用,但我找不到在线搜索的解决方案。可能是一个简单的语法修正。这是代码:
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getData() {
return SpreadsheetApp
.openById('My Sheet ID')
.getActiveSheet()
.getDataRange()
.getValues();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
h1 {
width: 100%;
overflow: hidden;
background-color: #ABC;
}
#wrapper {
margin: auto;
width: 60%;
border: 3px solid #000;
padding: 10px;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>Attendace Discrepancy Tracker v1.0</h1>
<h3>Discrepancy date:</h3><input type="date" name="bday" min="2000-01-02"><br>
<? var data = getData(); ?> <------ Gets data from my sheet
<h3>Select a name:</h3><select id='empName' onchange="findEmpID();">
<? for (var i = 1; i < data.length; i++) { ?>
<option value="<?= i ?>"><?= data[i][2] + ", " + data[i][1] + " " + data[i][3]?></option>
<? } ?>
</select>
<div id="empProfile">
<p>Employee Number: <span id="empNumber"></span></p>
<p>Supervisor: <span id="empSup"></span></p>
</div>
</div>
<script>
function findEmpID () {
var e = document.getElementById("empName");
var strUser = e.options[e.selectedIndex].value;
document.getElementById("empNumber")
.innerHTML = <?= data[strUser][0] ?>; <----- This does not work
}
</script>
</body>
</html>
整个程序都有效,除非我尝试引用回data
数组。我需要一种方法将<script>
代码strUser
值传递给data
数组google脚本变量,以便获取主管ID号。显然,当我切换到<?
标记空间时,它无法从<script>
标记中读取变量。我确信这是可能的,只是还没弄清楚如何。
答案 0 :(得分:0)
您正在尝试在加载HTML后使用scriptlet。看起来您正试图触发scriptlet从下拉列表的onchange
属性运行。除非首次加载页面,否则我认为scriptlet不会运行。
我改变了这样的事情:
使用this
关键字将员工姓名传递给客户端findEmpID()
功能:
<h3>Select a name:</h3><select id='empName' onchange="findEmpID(this.value);">
请注意使用
this.value
的。在函数括号中输入变量名称以接收员工姓名:
function findEmpID(theName) {
将名称传递给服务器功能:
<script>
function findEmpID(theName) {
google.script.run
.withSuccessHandler(enterName)
.getThisEmployeeID(theName);
};
function enterName(theReturnedName) {
//Put the name into the HTML Element
document.getElementById("empNumber").textContent = theReturnedName;
};
</script>
您需要从电子表格中获取数据,并使用服务器功能查找名称:
GS代码
function getThisEmployeeID(theName) {
//Get range of data
var arrayOfData = sheet.getRange(parameters).getValues();
//I don't know how the data is configured, but you'll need to match
//the name to the index number in the array, then get the ID
var employeeID = 'xyz';
return employeeID;
};