我需要使用数据验证从Google电子表格的下拉菜单中选择多个值,但我还需要能够在不同的验证列表之间切换,其中包含不同的值。 我尝试了一个由Jean-Pierre Verhulst制作的脚本,它通过创建一个带有3个列表选项的侧栏来进行选择,每个列表包含一组不同的值,但它不会将侧边栏验证列表中的选中值输入到活动中细胞
此google工作表包含脚本。 https://docs.google.com/spreadsheets/d/1zX4ArXsVjcYDqVpn6Olb5FGctdpmsTvIbbykNmxQXLI/edit?usp=sharing 验证值在VALIDATION选项卡中设置。选项卡dataSheet1和dataSheet2是最终结果的示例。
SERVER.gs代码:
/**
* Change the variable validation if needed
*/
var validation = {
sheet: 'VALIDATION',
userProperties: PropertiesService.getUserProperties()
}
function validOne() {
validation.userProperties.setProperty("range", "A2:A")
showSidebar();
}
function validTwo() {
validation.userProperties.setProperty("range", "B2:B")
showSidebar();
}
function validThree() {
validation.userProperties.setProperty("range", "C2:C")
showSidebar();
}
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Sidebar')
.addItem('List 1', 'validOne')
.addItem('List 2', 'validTwo')
.addItem('List 3', 'validThree')
.addToUi();
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSidebar() {
SpreadsheetApp.getUi()
.showSidebar(HtmlService.createTemplateFromFile('SIDEBAR')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Multiple selector'));
}
function getOptions() {
Logger.log(validation.range)
return SpreadsheetApp.getActive()
.getSheetByName(validation.sheet)
.getRange(validation.userProperties.getProperty("range"))
.getValues()
.filter(String)
.reduce(function(a, b) {
return a.concat(b)
})
}
function process(arr) {
arr.length > 0 ? SpreadsheetApp.getActiveRange()
.clearContent()
.setValue(arr.join(", ")) :
SpreadsheetApp.getUi()
.alert('No options selected')
}
function include(File) {
return HtmlService.createHtmlOutputFromFile(File).getContent();
};
SIDEBAR.html代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<?!=include( 'CSS'); ?>
<?!=include( 'javaScript'); ?>
</head>
<body>
<div class="container"></div>
<div class="buttons">
<p>
<button class="action" id="action">Fill active cell</button>
<button class="secondary" id="btn">Rebuild options</button>
</p>
</div>
</body>
</html>
javaScript.html代码:
<!-- SCRIPTGEDEELTE -->
<!-- Importeer jQuery (extern), Moment en semJs(intern) libraries -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
createList();
$("#action").click(function() {
google.script.run.withSuccessHandler(removeSelectedOptions).removeSelected()
selected.length = 0;
})
$("#btn").click(function() {
createList();
});
});
function options(arr) {
$(".container").empty();
if (arr && arr.length > 0) {
$(arr).each(function(i, el) {
$(".container").append('<div class="field"><div class="ui checkbox"><input type="checkbox" name="sel" value="' + el + '"><label>' + el + '</label></div></div>')
});
} else {
$(".container").append('<b>No options left.</b><br>To continue, rebuild the options list.')
$('#action').hide();
}
}
function createList(arr) {
google.script.run.withSuccessHandler(options).getOptions(arr);
}
function removeSelectedOptions(answer) {
var checked, unchecked, arr;
checked = [];
unchecked = [];
$('input[type=checkbox]').each(function () {
this.checked ? checked.push($(this).val()) : unchecked.push($(this).val());
$(this).prop("checked", false);
})
arr = [checked, unchecked]
google.script.run.process(arr[0])
if (answer) {
createList(arr)
}
}
</script>
我没有任何脚本或编程知识,所以我要求你的帮助,如果你能看一下脚本并找出问题所在,那对我来说将是一个救生员。
编辑: 需要帮助:侧边栏验证列表中的选定值未填充到活动单元格中。
谢谢!
丹