【发布时间】:2021-09-12 16:34:52
【问题描述】:
我一直在拼凑其他来源的脚本,感觉自己已经很接近了,但我无法独自完成最后的飞跃。只是给你一些背景
我正在 Google 表格中整理一个文档,我可以让学生输入来自现场足球比赛的特定值,这些值将在 OBS 上显示为我们视频广播的实时统计数据。我为篮球制作了一个非常简单的基于按钮的表格,效果非常好。 Picture of Google Basketball Sheet
足球是另一种野兽。我的概念是有一个侧边栏,其中我在下拉菜单中预选了一组名称,以及第二个下拉菜单,其中预选了统计类型的下拉菜单(匆忙,通过......)这两个下拉菜单会找到与名称行关联的单元格)和统计类型(列)以及为选定单元格添加值的输入字段。
我无法弄清楚如何让下拉菜单与特定单元格相关联,更不用说两个不同的下拉菜单了。如果这两个下拉菜单不可用,我很乐意制作一个与特定游戏相关联的按钮,然后下拉名称和输入字段就可以了。
到目前为止,我有一个下拉菜单,可以从 A 列中提取名称,但是当我选择一个时,什么也没有发生。我还有一个按钮,我可以将值发送到特定单元格,但它不会添加到原始单元格,它只是将单元格更改为“输入值”1
function ftball() {
try {
var output = HtmlService.createHtmlOutputFromFile('HTML_Sidebar');
SpreadsheetApp.getUi().showSidebar(output);
}
catch(err) {
Logger.log(err);
}
}
function getNames() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Football");
return sheet.getRange(1, 1, sheet.getLastRow(), 1).getValues(); // Retrieve values and send to Javascript
}
// Sets the value of A1 cell to value entered in the input field in the side bar!
function enterValue(number){
var ss = SpreadsheetApp.getActive()
var sheet = ss.getActiveSheet()
sheet.getRange("B11").setValue(number + 1)
}
以及侧边栏的 HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<select id="mySelect" onchange="selectChange(this)">
</select>
<script>
function selectChange(select) {
google.script.run.changeSheet(select.value);
}
function playerNames(names) {
var select = document.getElementById("mySelect");
for( var i=0; i<names.length; i++ ) {
var option = document.createElement("option");
option.text = names[i];
select.add(option);
}
}
(function () { google.script.run.withSuccessHandler(playerNames).getNames(); }());
</script>
<button onclick='f1()'>Select Athlete Name</button>
</br>
<!-- Create a input field to except a value form user, in this case there name -->
Enter Value:
<input type="number" id="number"><br>
<!-- Create a button to send the value to the spreadsheet -->
<button onclick='sendValue()'>Send Value</button>
<script>
function f1() {
google.script.run.getAddress();
}
function sendValue(){
//Get the value of the input field
var number = document.getElementById("number").value
//Log the value of input field in the web browser console (usually used for debugging)
console.log(number)
//Send the value of the text field as a arugment to the server side function.
google.script.run.enterValue(number)
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript google-apps-script google-sheets