[英]Google apps how to send data from html file to code.gs variable
我的谷歌應用腳本有問題。 我創建了一個下拉 select 元素,該元素從 json 獲取值,我試圖將所選值傳遞回變量中的 code.gs。 我怎樣才能做到這一點? json數據來自function“加入”。 我試圖獲取要在 api 調用中使用的“id”變量。
這是我的 html 文件:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<select id="droplist">
</select>
<button type="button" onclick="values()" >Hae</button>
<script>
(function () {
google.script.run.withSuccessHandler(
function(people) {
var select = document.getElementById("droplist");
for( var a of people) {
var option = document.createElement("option");
option.text = a[0] + " " + a[1];
select.add(option);
}
}
).getPeople();
}());
function values() {
var x = []
x.push.document.getElementById("droplist").selectedIndex;
alert(document.getElementsByTagName("option")[x].value);
Logger.log(x)
}
</script>
</body>
</html>
這是code.gs function 我試圖獲取數據的地方:
function join(x) {
var id = [];
id.push(x)
Logger.log(x)
Logger.log(id);
我假設您在命名 function values
。 然后為此,您可以通過使用selectElement.selectedOptions
並從中提取數據來獲取選定的值。 然后,您可以使用google.script.run
調用 join function ,就像使用getPeople
一樣。
這是一個最小的代碼示例: Code.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('page')
}
function join(values) {
Logger.log(values)
}
Page.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function values() {
// Get the values
const selected = document.getElementById("droplist").selectedOptions
const values = Array.from(selected).map(el => el.value)
// Call the Apps Script function
google.script.run
.withSuccessHandler(_ => console.log('success'))
.withFailureHandler(error => console.error(error))
.join(values)
}
</script>
</head>
<body>
<select id="droplist" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br>
<button onclick="values()">Send values!</button>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.