如何使用脚本在Google Spreadsheet中隐藏侧边栏? [英] How to hide the Sidebar in a Google Spreadsheet using a script?
本文介绍了如何使用脚本在Google Spreadsheet中隐藏侧边栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SpreadsheetApp.getUi()。[方法]我的电子表格有一个侧边栏,如何用脚本隐藏它? ?]
感谢您的任何帮助!
解决方案
忽略我在问题中有关显式返回的评论... successHandler语法很棘手。你需要一个函数的名字,没有括号。不知道为什么,但你去了那里。
Code.gs
下面是一个快速示例,验证输入表单后自动关闭。您可以通过忽略输入提示来测试错误行为。
$ b
/ **
*打开文档时在Google表格用户界面中创建一个菜单条目。
* /
函数onOpen(e){
SpreadsheetApp.getUi()。createAddonMenu()
.addItem('Start','showSidebar')
.addToUi ();
}
/ **
*安装加载项时运行。
* /
onInstall(e){
onOpen(e);
}
/ **
*在包含附件用户界面的文档中打开边栏。
* /
函数showSidebar(){
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Simple');
SpreadsheetApp.getUi()。showSidebar(ui);
}
/ **
*验证新文本,并在可接受的情况下将其插入到电子表格中。
*
* @param {string} newText用于替换当前选择的文本。
* /
函数insertText(newText){
if(newText.indexOf('Pizza')== -1){
SpreadsheetApp.getActiveSheet()。 。getActiveCell()的setValue(newText);
}
else {
抛出新错误('No Pizza allowed!');
}
return;
}
Sidebar.html
< pre class =lang-js prettyprint-override>
< link rel =stylesheethref =https://ssl.gstatic.com/docs/script/css/add-ons.css >
< div>
< form>
< div>
< label for =new-text>输入新文字:< / label>
< input type =textid =new-textplaceholder =不要输入'Pizza'!>
< / div>
< br>
< div class =blockid =button-bar>
< button id =insert-text>插入< / button>
< / div>
< / form>
< / div>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\">
< / script>
< script>
/ **
*在文档加载时,指定点击处理程序
* /
$(function(){
$('#insert-text')。click (insertText);
});
/ **
*运行一个服务器端函数来验证并输入输入的文本。
* /
函数insertText(){
this.disabled = true;
$('#error')。remove();
google.script.run
.withSuccessHandler(google.script.host.close)//< - 函数名称后没有括号
.withFailureHandler(
function(msg,元素){
showError(msg,$('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.insertText($('#new-text').val());
}
/ **
*在给定元素后面插入包含错误消息的div。
*
* @param msg要显示的错误消息。
* @param元素显示错误的元素。
* /
function showError(msg,element){
var div = $('< div id =errorclass =error>'+ msg +'< / DIV>');
$(element).after(div);
}
< / script>
My spreadsheet has a sidebar, how can I hide it with a script?
SpreadsheetApp.getUi().[method?]
Thanks for any help!
解决方案
Ignore what I said in the question comments about an explicit return... the successHandler syntax is tricky. You need the NAME of a function, without the brackets. No idea why, but there you go.
Code.gs
Here's a quick example of a sidebar that automatically closes after validating the input form. You can test the error behavior by ignoring the input hint.
/**
* Creates a menu entry in the Google Sheets UI when the document is opened.
*/
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
/**
* Runs when the add-on is installed.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Simple');
SpreadsheetApp.getUi().showSidebar(ui);
}
/**
* Validates the new text, and inserts it into spreadsheet if it is acceptable.
*
* @param {string} newText The text with which to replace the current selection.
*/
function insertText(newText) {
if (newText.indexOf('Pizza') == -1) {
SpreadsheetApp.getActiveSheet().getActiveCell().setValue(newText);
}
else {
throw new Error( 'No Pizza allowed!' );
}
return;
}
Sidebar.html
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<div>
<form>
<div>
<label for="new-text">Enter new text:</label>
<input type="text" id="new-text" placeholder="Don't enter 'Pizza'!">
</div>
<br>
<div class="block" id="button-bar">
<button id="insert-text">Insert</button>
</div>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
/**
* On document load, assign click handler
*/
$(function() {
$('#insert-text').click(insertText);
});
/**
* Runs a server-side function to validate and enter the entered text.
*/
function insertText() {
this.disabled = true;
$('#error').remove();
google.script.run
.withSuccessHandler(google.script.host.close) // <-- No brackets after function name
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.insertText($('#new-text').val());
}
/**
* Inserts a div that contains an error message after a given element.
*
* @param msg The error message to display.
* @param element The element after which to display the error.
*/
function showError(msg, element) {
var div = $('<div id="error" class="error">' + msg + '</div>');
$(element).after(div);
}
</script>
这篇关于如何使用脚本在Google Spreadsheet中隐藏侧边栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文