将焦点返回到电子表格 [英] Return focus to spreadsheet

查看:69
本文介绍了将焦点返回到电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些链接到我的Google Spreadsheet上的按钮的Google脚本.单击按钮以运行脚本后,即使仍在查看电子表格并且所选单元格仍然可见,在电子表格窗口上的焦点也会丢失.我必须单击窗口以返回焦点.

I'm running some Google Scripts linked to buttons on my Google Spreadsheet. After clicking the button to run the script, even though the spreadsheet is still being viewed and the selected cell is still visible, focus on the spreadsheet window is lost. I have to click the window to return focus.

这听起来与

This sounds like a very similar question to the one asked by robodog21. Ed Nelson's solution involving a sidebar with buttons executing the HTML command google.script.host.editor.focus() works well, but I'd really rather not involve a sidebar. I'd prefer to stick with my existing buttons if possible. These are inserted on the spreadsheet as a Google Drawing and then linked to the appropriate Javascript function.

正如robodog21指出的那样,我对Javascript和HTML还是很陌生.我在Google Apps脚本中找到了以下帮助:

As robodog21 indicated, I am also quite new to Javascript and HTML. I found the following help in Google Apps Script:

在G Suite中移动浏览器焦点

要将用户浏览器中的焦点从对话框或侧边栏切换回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).

但是,由于我对HTML的熟悉程度有限,所以我还没有弄清楚如何实现这一点.

However I have not figured out how to implement this due to my limited familiarity with HTML.

我无法在Stack Overflow中找到专门与Google Apps脚本相关的解决方案,并且还不了解(尚未!)来修改其他解决方案(例如

I have not been able to find a solution in Stack Overflow related specifically to Google Apps Scripts and don't know enough (yet!) to modify other solutions (such as this) to suit my needs. I would appreciate someone clarifying how to apply a solution to my situation.

推荐答案

在阅读Rubén的答案并辞职使用侧边栏后,我一直在玩弄我的脚本,并意识到将焦点返回到电子表格所需的一切仅仅是打开和关闭侧边栏.最小的HTML似乎是:

After reading Rubén's answer and resigning myself to using a sidebar, I kept playing around with my scripts and realized that all it takes to return focus to the spreadsheet is simply opening and closing a sidebar. The minimum HTML seems to be:

<html>
  <body onload="google.script.host.close()">
  </body>
</html>

这在JS函数中调用:

function sidebarCreator() {
  var html = HtmlService.createHtmlOutputFromFile('Page');
  SpreadsheetApp.getUi() 
      .showSidebar(html)
}

我意识到此实现有点混乱,因为它拉起了我实际上不想要的侧边栏,然后立即将其关闭.但是,它可以实现我想要的功能,而无需对现有按钮和功能进行其他任何更改,因此,我感到很高兴.如果有人知道实现此结果的更好方法,请告诉我.感谢鲁本为我指明了正确的方向!

I realize this implementation is a little messy in that it pulls up a sidebar which I don't actually want and then immediately closes it. However, it accomplishes what I want with no other changes needed to my existing buttons and functions, so I'm fairly happy. If anyone knows a better method to achieve this result please let me know. Thanks to Rubén for pointing me in the right direction!

这篇关于将焦点返回到电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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