[英]google sheets script - several multiple choice answers
我正在尝试制作一个弹出框,其中包含 select 的多个多项选择答案。
我使用了这个例子: google apps script: a select drop down list in google sheet这是 1 个问题的一个很好的起点,我尝试扩展它以获得多个答案,但未能扩展它。
这是我的测试文件: https://docs.google.com/spreadsheets/d/1BRCqpvfRl64a7ISyuohxUJLWKbqX9Fz6NPCrL2iKEm0/
它包含由简单的按钮按下触发的脚本代码;
function start() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1:B1').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
spreadsheet.getRange('A10').activate();
// START HTML POP-UP
dropDownModal()
};
// -------------------------------------------------------------------------
function dropDownModal() {
var htmlDlg = HtmlService.createHtmlOutputFromFile('dropdown.html')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(350)
.setHeight(175);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'Box title');
};
function writeChoice(selection1) {
const writeResponseLocation1 = "A1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation1)
.setValue(selection1);
}
function writeChoice(selection2) {
const writeResponseLocation2 = "B1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation2)
.setValue(selection2);
}
// -------------------------------------------------------------------------
和这个下拉菜单。html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
function onSuccess1() {
google.script.host.close();
}
function submit1() {
const choice1 = document.getElementById('choice1').value;
const choice2 = document.getElementById('choice2').value;
google.script.run
.withSuccessHandler(onSuccess1)
.writeChoice(choice1)
.writeChoice(choice2);
}
function setup1() {
const button = document.getElementById('submitbutton1');
button.addEventListener("click", submit1)
}
</script>
<body onload="setup1()">
<p>
Text 1.
</p>
<form>
<select id="choice1">
<option value="choice 1-A">choice 1-A</option>
<option value="choice 1-B">choice 1-B</option>
<option value="choice 1-C">choice 1-C</option>
</select>
<br><br>
<select id="choice2">
<option value="choice 2-A">choice 2-A</option>
<option value="choice 2-B">choice 2-B</option>
<option value="choice 2-C">choice 2-C</option>
</select>
<br><br>
<button id="submitbutton1">Hit it 1</button>
</form>
</body>
</html>
并将问题 1 的答案写入问题 2 的位置。
有人可以帮忙找出我哪里出错了吗?
谢谢
writeChoice
仅更改function 参数( selection1
, selection1
)的名称是不够的,实际上这些只是占位符。
相反,您需要创建两个具有两个不同名称的不同功能(示例 1)或将writeResponseLocation
作为第二个参数传递(示例 2)。
此外,使用google.script.run
,您一次只能调用一个 Apps 脚本 function。 您可以在成功处理程序中调用第二个,例如。
样品 1:
代码.gs
...
function writeChoice1(selection1) {
const writeResponseLocation1 = "A1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation1)
.setValue(selection1);
}
function writeChoice2(selection2) {
const writeResponseLocation2 = "B1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation2)
.setValue(selection2);
}
...
html
<script>
function onSuccess1() {
const choice2 = document.getElementById('choice2').value;
google.script.run
.withSuccessHandler(onSuccess2)
.writeChoice2(choice2);
}
function onSuccess2() {
google.script.host.close();
}
function submit1() {
const choice1 = document.getElementById('choice1').value;
google.script.run
.withSuccessHandler(onSuccess1)
.writeChoice1(choice1);
}
function setup1() {
const button = document.getElementById('submitbutton1');
button.addEventListener("click", submit1)
}
</script>
...
样本 2:
代码.gs
...
function writeChoice(selection, writeResponseLocation) {
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation)
.setValue(selection);
}
...
html
<script>
function onSuccess1() {
const choice2 = document.getElementById('choice2').value;
google.script.run
.withSuccessHandler(onSuccess2)
.writeChoice(choice2, "B1");
}
function onSuccess2() {
google.script.host.close();
}
function submit1() {
const choice1 = document.getElementById('choice1').value;
google.script.run
.withSuccessHandler(onSuccess1)
.writeChoice(choice1, "A1");
}
function setup1() {
const button = document.getElementById('submitbutton1');
button.addEventListener("click", submit1)
}
</script>
...
想我在这里和朋友一起摆弄找到了一个解决方案。
// =========================================================================
//
// EMPTY CELLS A1 and A2 before re-run
//
function start() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1:B1').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
spreadsheet.getRange('A10').activate();
// START HTML POP-UP
dropDownModal()
};
// -------------------------------------------------------------------------
function dropDownModal() {
var htmlDlg = HtmlService.createHtmlOutputFromFile('dropdown.html')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(350)
.setHeight(175);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'Box title');
};
function writeChoice1(selection1) {
const writeResponseLocation1 = "A1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation1)
.setValue(selection1);
}
function writeChoice2(selection2) {
const writeResponseLocation2 = "B1";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation2)
.setValue(selection2);
}
// =========================================================================
和
下拉。html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
function onSuccess() {
google.script.host.close();
}
function submit() {
const choice1 = document.getElementById('choice1').value;
const choice2 = document.getElementById('choice2').value;
google.script.run
.writeChoice1(choice1)
google.script.run
.withSuccessHandler(onSuccess)
.writeChoice2(choice2)
}
function setup() {
const button = document.getElementById('submitbutton');
button.addEventListener("click", submit)
}
</script>
<body onload="setup()">
<p>
Text 1.
</p>
<form>
<select id="choice1">
<option value="choice 1-A">choice 1-A</option>
<option value="choice 1-B">choice 1-B</option>
<option value="choice 1-C">choice 1-C</option>
</select>
<br><br>
<select id="choice2">
<option value="choice 2-A">choice 2-A</option>
<option value="choice 2-B">choice 2-B</option>
<option value="choice 2-C">choice 2-C</option>
</select>
<br><br>
<button id="submitbutton">Submit entries</button>
</form>
</body>
</html>
这一切都有效。
我希望理解的一件事,希望这里有人可以解释。
google.script.run
.withSuccessHandler(onSuccess)
.writeChoice1(choice1)
.writeChoice2(choice2)
.withSuccessHandler(onSuccess)
.writeChoice2(choice2)
工作,但是这在编写选项后关闭,而不是这样:
.writeChoice2(choice2)
.withSuccessHandler(onSuccess)
令人困惑的部分是我会假设在写之前关闭意味着没有写,所以我在测试早期的某个地方切换了它们。
很高兴代码有效,但如果有人知道,仍然想学习:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.