Skip to content

Instantly share code, notes, and snippets.

@erickoledadevrel
Created January 14, 2016 17:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erickoledadevrel/1d2f92d0475f8bf2e332 to your computer and use it in GitHub Desktop.
Save erickoledadevrel/1d2f92d0475f8bf2e332 to your computer and use it in GitHub Desktop.
Polling in a sidebar only when the page is visible
function onOpen() {
SpreadsheetApp.getUi().createMenu('Testing')
.addItem('Show Sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var sidebar = HtmlService.createHtmlOutputFromFile('Sidebar')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Sidebar');
SpreadsheetApp.getUi().showSidebar(sidebar);
}
function getActiveRange() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Active range: <span id="range"></span>
<script src="//cdnjs.cloudflare.com/ajax/libs/visibility.js/1.2.1/visibility.min.js"></script>
<script>
Visibility.every(1000, function () {
getActiveRange();
});
function getActiveRange() {
console.log('Running... ' + new Date());
google.script.run.withSuccessHandler(showRange).getActiveRange();
}
function showRange(range) {
document.getElementById('range').innerHTML = range;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment