如何在 Google 电子表格中捕获更改选项卡事件? [英] How to capture change tab event in Google Spreadsheet?
问题描述
我在 Google 电子表格中发现"了侧边栏功能.很整洁.
I have 'discovered' the Sidebar functionality in Google Spreadsheet. Very neat.
我有不同的选项卡,侧边栏充当帮助功能.如果我在侧边栏中选择下一步,我可以打开包含下一步的选项卡.
I have different tabs and the Sidebar acts as a help function. If I select the next step in the Sidebar, I can open the tab that contains the next step.
现在我有点卡住了.当用户选择不同的标签时,我喜欢用相应的信息填充侧边栏.
Now I'm kind of stuck. When the user selects a different tab, I like to populate the Sidebar with corresponding information.
虽然我知道如何填写信息,但我正在寻找运行该函数的触发器".
Though I know how to fill the information, I'm looking for the 'trigger' to run the function.
所以我的问题归结为:如何捕获更改选项卡事件?
So my question boils down: how do I capture the change tab event?
推荐答案
使用 如何从附加组件轮询 Google 文档.与基于时间的触发器不同,这依赖于侧边栏 html 中的客户端延迟循环来调用您在 Google 服务器上的脚本.
Use the poller technique demonstrated in How to poll a Google Doc from an add-on. Unlike a time-based trigger, this relies on a client-side delay loop in the sidebar html to make calls to your script on Google's servers.
以下是如何修改该问题中的示例.
Here's how you can modify the example from that question.
- 添加获取当前标签名的函数
- 将
DocumentApp
替换为SpreadsheetApp
- 或者,删除所有不相关的光标/选择内容.
/**
* Gets the name of the current tab
*
* @return {string} The selected tab name.
*/
function getActiveTab() {
return SpreadsheetApp.getActiveSheet().getName();
}
在 HTML 中
- 用这个函数替换
poll()
函数. - 或者,删除所有不相关的光标/选择内容.
<div id="tab-name">loading...</div>
...
/**
* Poll server-side function(s) at the given interval.
*
* @param {Number} interval (optional) Time in ms between polls.
* Default is 2s (2000ms)
*/
function poll(interval){
interval = interval || 2000;
setTimeout(function(){
google.script.run
.withSuccessHandler(
function(tabName) {
// DEMO - just display name of selected tab
$('#tab-name').text(tabName);
//Setup the next poll recursively
poll(interval);
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.getActiveTab();
}, interval);
};
这篇关于如何在 Google 电子表格中捕获更改选项卡事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!