如何使用脚本隐藏Google电子表格中的补充工具栏?

时间:2014-12-04 12:56:23

标签: javascript google-apps-script google-sheets

我的电子表格有一个侧边栏,如何用脚本隐藏它?

  SpreadsheetApp.getUi().[method?]

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

忽略我在关于显式返回的问题评论中所说的内容... successHandler语法很棘手。你需要一个函数的名称,没有括号。不知道为什么,但你去了。

Code.gs

以下是在验证输入表单后自动关闭的侧边栏的快速示例。您可以通过忽略输入提示来测试错误行为。

/**
 * 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>

答案 1 :(得分:1)

这是直接的方法

function closeSidebar() {
    var html = HtmlService.createHtmlOutput("<script>google.script.host.close();</script>");
    SpreadsheetApp.getUi().showSidebar(html);
}

// anywhere on the server-side script (or code.gs)
closeSidebar()

答案 2 :(得分:0)

来自文档:

  

对话框可以通过调用关闭   HTML服务的客户端中的 google.script.host.close()   接口或UI服务接口中的 UiInstance.close()。对话框   不能由其他接口关闭,只能由用户或自己关闭。

UiInstance.close() 已弃用。此函数已弃用,不应在新脚本中使用

https://developers.google.com/apps-script/guides/dialogs