我正在尝试在Google表格中实施自定义HTML菜单。通过函数 getValuesForRngName() ,可以使用命名范围中的值填充下拉列表。在列表中选择值后,脚本应再次从工作表中检索范围,确定价格并将价格填充到文本框servicePrice中。但是,问题是这行代码“ values = google.script.run.getValuesForRngName('ServiceList'); ”没有检索任何内容。
您能否建议如何使其发挥作用?
HTML代码如下:
<div>
<form>
<table>
<tr>
<td>Select Service:</td>
<td><select name="servicesMD" id="servicesMD" onChange="retrieveServicePrice(this);"></select></td>
</tr>
<tr>
<td><br/><br/>
<script type="text/javascript">
function onSuccess(values) {
var option, dropDown;
for (i = 1; i < values.length; i++) {
dropDown = document.getElementById("servicesMD");
option = document.createElement("option");
dropDown.options.add(option);
option.value = values[i][0]; // service ID
option.text = values[i][1]; // service Name
}
}
function populate() {
google.script.run.withSuccessHandler(onSuccess).getValuesForRngName('ServiceList');
}
</script>
Service Price: <input id="servicePrice" name="servicePrice" type="text" /><br/><br/>
</tr>
</table>
</form>
</div>
<script>
window.addEventListener('load', populate());
function retrieveServicePrice(element) {
var selectionIndex;
var values;
values = google.script.run.getValuesForRngName('ServiceList');
selectionIndex = document.forms[0].action = element.options[element.selectedIndex].getAttribute("value");
document.getElementById("servicePrice").value = values[selectionIndex][3]; // should retrieve price of service
}
</script>
GAS代码如下:
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show custom menu', 'openServiceSalesForm')
.addToUi();
}
function openServiceSalesForm() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setWidth(600)
.setHeight(400);
SpreadsheetApp.getUi().showModalDialog(html, 'Service sales form');
}
// RangeList
function getValuesForRngName(rngName) {
var rngValues = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(rngName).getValues();
return rngValues;
}
答案 0 :(得分:0)
假设ServiceList范围内的列按以下顺序Service ID | Service Name | Price
<script>
window.addEventListener('load', populate());
var serviceID;
function retrieveServicePrice(element) {
serviceID = element;
google.script.run.withSuccessHandler(injectValuesIntoHTML).getValuesForRngName('ServiceList');
}
window.injectValuesIntoHTML = function(values) {
for (var i = 0; i < values.length; i++) {
if (serviceID == values[i][0]) {
document.getElementById("servicePrice").value = values[i][2]; // should retrieve price of service
}
}
}
</script>
HTML:
<select name="servicesMD" id="servicesMD" onChange="retrieveServicePrice(this.value);">