不知道這是否會令你滿意,但使用側邊欄面板上顯示「確定/取消」按鈕可以完成這項工作。您可以通過Toast消息顯示任務/問題(這樣您就不會失去瀏覽器對側欄按鈕的注意力,因爲它會在模態框中發生)。然後,由於側欄始終顯示,您只需按下輸入或標籤並進入以選擇以前的焦點「確定」或「取消」。
我不完全知道如何連接主函數,烤麪包的消息和確定/取消面板,但我想這存儲在片本身例如一些數據可以做的工作。
例如:將「D5」單元座標存儲在工作表中的某處並運行函數。 Toast問你是否要修改D5(使用存儲的座標)。如果選擇是,則運行一個函數,獲取座標並繼續。
這是相當複雜的,可能是無情無義uneffective,但如果你的工作是真的如此重複,它可能節省一些時間。
Here的一個例子片(HTML代碼從我的一些舊腳本的截取)和代碼GS和HTML還提供下面。
Code.gs
function onOpen() {
var html = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('a test');
SpreadsheetApp.getUi()
.showSidebar(html)
.createMenu('——MENU')
.addItem('Test toast', 'openDialog')
.addToUi();
}
// ok/cancel functions
function func_ok(){
SpreadsheetApp.getActiveSpreadsheet().toast('ok');
// do something
}
function func_cancel(){
SpreadsheetApp.getActiveSpreadsheet().toast('cancel');
// do something
}
// toast function
function openDialog() {
SpreadsheetApp.getActiveSpreadsheet().toast('you can display some information here and confirm in via the panel');
}
sidebar.html
<div style='font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;background: linear-gradient(to bottom, #323232 0%,#ffffff 100%);padding-bottom:100%'>
<section style='background:#FFE599'>
<button onclick="google.script.run.func_ok();">OK</button>
<button onclick="google.script.run.func_cancel();">Cancel</button>
</section>
</div>
<!-- stylesheet -->
<style>
h4 {
margin:auto auto 10px auto;
border-bottom: 1px solid rgba(44, 44, 44, 0.3);
padding:3px 10px;
background: rgba(44, 44, 44, 0.1);
text-align:center
}
button {
background: #E5E5E5;
border:none;
border-bottom:2px solid #ccc;
border-radius:2px;
padding:3px 4px;
margin:4px 10%;
width:80%
}
button:hover {
background:#efefef;
cursor:pointer
}
section {
padding-bottom:5%;
margin: 0 2% 5% 2%;
border-radius: 2px;
box-shadow:0 1px 2px 1px #111111;
}
</style>
感謝您對這個建議!我認爲這樣做很有挑戰性,因爲焦點一直在電子表格上,並且提示將作爲電子表格中的值的結果出現,因此無論哪種方式焦點都不在側邊欄上。 – GimelG
我最終找到了一個奇怪的解決方案,但似乎工作: 我使用HtmlService打開一個對話框,並帶有一個'html'確定按鈕,自動對焦。由於HtmlService Ui不會阻止腳本繼續運行,因此我設置了一個單獨的'whenClicked(doThis)'函數,其中'doThis'是一個在特定條件下評估爲true的變量。 – GimelG
這解決了大約75%的情況,但剩下的25%現在需要額外的點擊,因爲焦點不在ESC按鈕上......所以我做了一個「Utilities.sleep」,允許我點擊OK只需3秒鐘,之後會打開另一個對話框'',這會導致關閉原始對話框。在某種程度上,它甚至更好,我甚至不需要按ESC。不知何故,但不訣竅:) – GimelG