我已經開始在谷歌表格中使用谷歌應用腳??本,我想創建一個對話框,用戶將在其中撰寫我稍后將使用的輸入。對話框應該有一個下拉串列,可以提出建議或完成輸入。
uj5u.com熱心網友回復:
對于通過 Google 搜索到達這里的任何人:
谷歌表格中的下拉選單可以通過資料驗證來實作。創建一列條目。讓我們這樣說:
A4 = Apples
A5 = Tigers
A6 = Coriander
A7 = Forest
然后選擇一個單元格,例如 B4。現在轉到頂部選單中的資料。選擇資料驗證。將打開一個帶有選項的模塊。選擇“從范圍中列出”。作為標準,然后輸入 A4:A7 作為范圍。您可以選擇拒絕其他輸入。現在將滑鼠懸停在 B4 上并單擊箭頭。您將看到您現在有一個行內下拉選單。知道您可以將串列源添加到另一個選項卡甚至向用戶隱藏該選項卡以保持界面清潔可能很有用。
現在,回答你的實際問題。
您希望下拉選單出現在彈出視窗中。這是可以做到的!它不如使用帶有資料驗證的行內下拉選單快,但它更漂亮。
假設您至少了解 Google Apps 腳本的基礎知識,代碼如下:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Multiple choice', 'dropDownModal')
.addToUi();
}
function dropDownModal() {
var htmlDlg = HtmlService.createHtmlOutputFromFile('dropdown.html')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(350)
.setHeight(175);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'A Title Goes Here');
};
function writeChoice(selection) {
const writeResponseLocation = "B4";
SpreadsheetApp
.getActiveSpreadsheet()
.getSheets()[0]
.getRange(writeResponseLocation)
.setValue(selection);
}
然后創建一個名為的檔案dropdown.html(除了上面的 code.gs 檔案)并輸入以下內容:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
function onSuccess() {
google.script.host.close();
}
function submit() {
const choice = document.getElementById('choice').value;
google.script.run
.withSuccessHandler(onSuccess)
.writeChoice(choice);
}
function setup() {
const button = document.getElementById('submitbutton');
button.addEventListener("click", submit)
}
</script>
<body onload="setup()">
<p>
There will be a slight delay on submission.
</p>
<form>
<select id="choice">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="coriander">Coriander</option>
<option value="monkey">Monkey</option>
</select>
<button id="submitbutton">Submit</button>
</form>
</body>
</html>
Now save everything and reload the sheet. A menu will appear at the end of the menu bar called Custom Menu. Select that and choose Multiple choice. You'll have to give yourself permission to the run the code you entered for this to work (then choose the menu option again). That'll do it. Tweak the code to suit your needs.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/431130.html
上一篇:谷歌表格查詢-將列資料合并到行
