使用Ui Service API在Google Apps中移动浏览器焦点 [英] Moving browser focus in Google Apps using Ui Service API

查看:40
本文介绍了使用Ui Service API在Google Apps中移动浏览器焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google文档构建一个小的脚本 bound .该脚本创建一个菜单项和一个侧栏.边栏包含一个按钮,单击该按钮可将今天的日期插入文档中的光标.菜单项执行相同的操作.

I'm building a small script bound to a Google Document. The script creates a menu entry and a sidebar. The sidebar contains a single button that, when clicked, inserts today's date at the cursor in the document. The menu entry does the same.

使用菜单项插入日期时,将插入日期,并且光标将保持聚焦在文档编辑区域中.但是,当从侧栏中单击按钮时,焦点会丢失.

When using the menu entry to insert the date, the date is inserted, and the cursor stays focused into the document editing area. But, when the button is clicked from the side-bar, the focus is lost.

我在将浏览器聚焦于Google Apps 此摘要:

要将用户浏览器中的焦点从对话框或侧边栏切换回Google文档,表格或表单编辑器,只需调用方法google.script.host.editor.focus().与Document服务方法Document.setCursor(position)和Document.setSelection(range)结合使用时,此方法特别有用.

To switch focus in the user's browser from a dialog or sidebar back to the Google Docs, Sheets, or Forms editor, simply call the method google.script.host.editor.focus(). This method is particularly useful in combination with the Document service methods Document.setCursor(position) and Document.setSelection(range).

但是在使用

But the call to google.script.host.editor.focus() fails from within a click handler when using the Ui Service in a Google Document. What is the equivalent UI Service method(s) to force focus back to the document area?

这是我的脚本,它绑定到Google文档.请参见在 myClickHandler 函数内对 google.script.host.editor.focus()的注释掉的调用.

Here is my script which is bound to the Google Document. See the commented out call to google.script.host.editor.focus() inside the myClickHandler function.

function onOpen() {
  // Add a menu with some items, some separators, and a sub-menu.
  DocumentApp.getUi().createMenu('Sidebars')
      .addItem('Insert Date at cursor', 'insertDateAtCursor')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

/**
 * Insert a Date at the current cursor location.
 */
function insertDateAtCursor() {
  var cursor = DocumentApp.getActiveDocument().getCursor();

  if (cursor) {
    // Attempt to insert text at the cursor position. If insertion returns null,
    // then the cursor's containing element doesn't allow text insertions.
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
    var today = new Date;
    var element = cursor.insertText( today.getFullYear() + "-" + today.getDate() + "-" + (today.getMonth()+1) );
    if (!element) {
      DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
    }
  } else {
    DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }
}

/**
 * Show the side bar:
 */
function showSidebar() {
  // https://developers.google.com/apps-script/guides/ui-service
  var app = UiApp.createApplication().setTitle('Utility Sidebar');
  var panel = app.createVerticalPanel();
  var button = app.createButton("Insert Date At Cursor");
  panel.add(button);
  app.add(panel);
  
  // https://developers.google.com/apps-script/guides/ui-service#ServerHandlers
  var handler = app.createServerHandler('myClickHandler');
  button.addClickHandler(handler);

  DocumentApp.getUi().showSidebar(app);
}

/**
 * Handle the button click:
 */
function myClickHandler(e) {
  var app = UiApp.getActiveApplication();

  insertDateAtCursor();
  
  // What is the UiApp equivalent of this?:
  // google.script.host.editor.focus();
  
  // Calling app.close() closes the side bar so comment this out:
  // app.close();
  return app;
}

推荐答案

UiApp完全在远程服务器上运行,它无法直接与您的浏览器进行交互并更改焦点.正弦,您的用户界面非常简单,建议您将脚本转换为使用HTMLService而不是UiApp,这是一个尝试的机会,不要太麻烦:-)

UiApp runs entirely on a distant server, it can't interact directly with your browser and change the focus. Sine your UI is very simple I'd suggest you convert your script to use HTMLService instead of UiApp,it will be a good occasion to give it a try without too much headache :-)

这篇关于使用Ui Service API在Google Apps中移动浏览器焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆