如何在Google Spreadsheet中捕捉变更标签事件? [英] How to capture change tab event in Google Spreadsheet?
问题描述
我在Google电子表格中发现了侧边栏功能。非常整齐。
我有不同的标签,侧边栏充当帮助功能。如果我选择侧边栏中的下一步,我可以打开包含下一步的选项卡。
现在我有点卡住了。当用户选择不同的选项卡时,我喜欢用相应的信息填充边栏。
尽管我知道如何填充信息,但我正在寻找'触发器'运行该函数。
所以我的问题归结为:我如何捕获变更标签事件?
使用如何从附加组件中轮询Google文档。与基于时间的触发器不同,这依赖于侧边栏html中的客户端延迟循环来调用Google服务器上的脚本。
以下是如何修改这个问题的例子。
在Code.gs中
- 添加一个函数获取当前标签名称
- 用
SpreadsheetApp
DocumentApp > - 或者,删除所有不相关的游标/选择内容。
/ **
*获取当前标签的名称
*
* @return {string}选定的标签名称。
* /
function getActiveTab(){
return SpreadsheetApp.getActiveSheet()。getName();
}
in HTML
- 用这个函数替换
poll()
函数。 - 或者,删除所有不相关的游标/选择的东西。
< div id =tab-名称的载量...< / DIV>
...
/ **
*在给定的时间间隔内轮询服务器端函数。
*
* @param {Number}时间间隔(可选)轮询之间的时间(以毫秒为单位)。
*默认为2s(2000ms)
* /
函数poll(interval){
interval = interval || 2000;
setTimeout(函数(){
google.script.run
.withSuccessHandler(
函数(tabName){
// DEMO - 只显示所选标签的名称
$('#tab-name')。text(tabName);
//设置下一个轮询递归
poll(interval);
})
.withFailureHandler (
function(msg,element){
showError(msg,$('#button-bar'));
element.disabled = false;
})
.getActiveTab();
},interval);
};
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?
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.
in Code.gs
- Add a function to get the current tab name
- Replace
DocumentApp
withSpreadsheetApp
- Optionally, delete all the irrelevant cursor / selection stuff.
/**
* Gets the name of the current tab
*
* @return {string} The selected tab name.
*/
function getActiveTab() {
return SpreadsheetApp.getActiveSheet().getName();
}
in HTML
- replace the
poll()
function with this one. - Optionally, delete all the irrelevant cursor / selection stuff.
<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 Spreadsheet中捕捉变更标签事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!