我試圖在Google表格中實現自定義HTML菜單。通過函數getValuesForRngName()從下拉列表中填入來自命名範圍的值。在列表中選擇值後,腳本應該再次從表單中檢索範圍,確定價格並將價格填充到文本框servicePrice中。但是,問題是,這行代碼「values = google.script.run.getValuesForRngName('ServiceList');」不檢索任何內容。google.script.run在自定義菜單中的使用
您能否建議如何使它工作?
<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;
}
rngValues是一個二維陣列。如果只有一個,那麼它將被rngValues [0] [0]引用; – Cooper