Google Sheet 中是否可以有一个多选列表框?

sha*_*man 1 google-sheets google-apps-script

是否可以使用应用程序脚本在谷歌工作表上有一个多选列表框?

我找到了一种可以通过使用多个下拉菜单来部分实现的方法,但用户需要选择并等待几秒钟,然后再选择其他下拉菜单。还有一个问题是您最终可能会选择多次相同的名称;有一个复选框会更有效。

在谷歌工作表下方,POC1 是我希望有列表框的列,该列表框允许在名称列表中选择多个名称并显示在同一单元格中,并按分隔符分隔。

谷歌表格

如果 POC1 列单元格中有类似下面的内容,那就太好了,但这是使用excel,想知道当我们在工作中使用 Gsuite 时如何在 Google Sheet 上复制。

期望的结果

Log*_*gan 7

使用应用程序脚本:

这是使用应用程序脚本和自定义菜单来完成此操作的方法

1.)转到扩展 -> Apps 脚本将Code.gs重命名为SERVER.gs。现在复制并粘贴下面的代码以替换里面的所有内容

/**
 * Changes the variable validation if needed
 */

var validation = {
    sheet: 'VALIDATION',
    range: 'A2:A'
}

/**
 * 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('Show Sidebar', 'showSidebar')
        .addToUi();
        showSidebar();
}


/**
 * 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() {
    return SpreadsheetApp.getActive().getSheetByName(validation.sheet).getRange(validation.range).getDisplayValues()
        .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')
}
Run Code Online (Sandbox Code Playgroud)

2.)单击加号 (+),然后单击 HTML,在脚本编辑器中创建一个新文件。将此 SIDEBAR 命名为.html。

在此输入图像描述

3.)将内容替换为以下代码:

<!DOCTYPE html>
<html>
<style>
    .container,
    .buttons {
        margin: 5px;
        width: 95%;
        padding: 2px;
        font-size: 13px;
    }
</style>

<head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>

<body>
    <div class="container"></div>
    <div class="buttons">
        <p>
            <button class="action" id="action">Fill active cell</button>
            <button id="btn">Rebuild options</button>
        </p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.min.js"></script>
    <script>
        $(document).ready(function() {
            createList();
            var selected = [];
            $('.ui.checkbox').checkbox();
            $("#action").click(function() {
                $("input:checkbox[name=sel]:checked").each(function() {
                    selected.push($(this).val());
                    $(this).prop( "checked", false ); 
                });
                google.script.run.process(selected)
                selected.length = 0;
            });
            $("#btn").click(function() {
                createList();
            });
        });

        function options(arr) {
            $(".container").empty();
            $(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>')
            });
        }

        function createList() {
            google.script.run.withSuccessHandler(options).getOptions()
        }
    </script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

4.)刷新电子表格,您现在应该拥有自定义菜单Sidebar在此输入图像描述

5.)添加一个名为“验证”的工作表,您将在此处放置可供选择的选项。从第 2 行开始。

在此输入图像描述

6.)单击侧边栏 >显示侧边栏。右侧应打开一个侧栏,其中包含您在验证表中添加的选项列表。

在此输入图像描述

7.) 选择要输入所选选项的单元格。您现在可以从选项中选择多个项目。 在此输入图像描述

参考: