Skip to content

Instantly share code, notes, and snippets.

@rheajt
Last active July 5, 2016 15:33
Show Gist options
  • Select an option

  • Save rheajt/a9e8c3f770a5a1e2a764585370fa94d4 to your computer and use it in GitHub Desktop.

Select an option

Save rheajt/a9e8c3f770a5a1e2a764585370fa94d4 to your computer and use it in GitHub Desktop.
kern viewer

kern viewer

This is still a very basic prototype. Please let me know what you can't get to work, and suggestions for improvement. This will also not be able to open webpages that are not served from HTTPS due to security rules put in place with add-ons. There may be a way to work around this, but why?

This add-on assumes that the first row is a header row. Please help me identify which kind of URLs work with the iframe, and possibly ways to make the add-on accept more URLs. Add-on unlisted on the web store: https://goo.gl/yvS8r1

how to test this script with the google sheets script editor:

  1. create a google sheet
  2. fill the first column with the shared URLs from a google doc, sheet, or form.
  3. open the script editor
  4. copy the code from this 'Code.gs' into the 'Code.gs' in your script editor
  5. click on 'File > New > Html File' and name the file 'Viewer' (this should open a Viewer.html window)
  6. copy my code into this window
  7. save everything and click on 'Publish > Test as add-on'
  8. follow the provided steps and test the add-on
function onOpen() {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('KERNView', 'openKernView')
.addToUi();
}
function onInstall() {
onOpen();
}
function openKernView() {
var html = HtmlService.createHtmlOutputFromFile('Viewer')
.setTitle('KERNViewer')
.setHeight(500)
.setWidth(700)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(html, 'KERNViewer');
}
function getUrls(col) {
var sheet = SpreadsheetApp.getActiveSheet();
return sheet.getRange(2, col, sheet.getLastRow()).getValues()
.reduce(function(a, b) {
return a.concat(b);
});
}
function getUrlColumn() {
var sheet = SpreadsheetApp.getActiveSheet();
return sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues().reduce(function(a, b) {return a.concat(b);});
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<style>
.back {
float: left;
}
.forward {
float: right;
}
.iframe-view {
display: none;
}
</style>
</head>
<body>
<div class="block" style="text-align:center">
<button class="back">back</button>
Select the column with your urls: <select class="url-column"></select>
<button class="forward">forward</button>
</div>
<div class="iframe-view">
<iframe height="450" width="690"></iframe>
</div>
<div class="error-view">
<h1>You need to select a column with URLs to Google Documents (Docs, Sheets, Forms)</h1>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
var srcCount = 0,
srcArray = [];
google.script.run
.withSuccessHandler(function(response) {
response.forEach(function(each, ind) {
$('.url-column').append('<option value="' + ind + '">' + each + '</option>');
});
})
.getUrlColumn();
//set events
$('.forward').click(goForward);
$('.back').click(goBack);
$('.url-column').change(setUrlColumn);
//move functions
function goForward() {
if(srcCount < srcArray.length) {
srcCount++;
$('iframe').attr('src', srcArray[srcCount]);
}
}
function goBack() {
if(srcCount < srcArray.length) {
srcCount--;
$('iframe').attr('src', srcArray[srcCount]);
}
}
//set the column to pull URLs from
function setUrlColumn() {
google.script.run
.withSuccessHandler(function(response) {
srcArray = response;
//make sure that the column has google docs links in it
if(testGoogleUrl(srcArray[0])) {
toggleViews();
$('iframe').attr('src', srcArray[0]);
} else {
toggleViews();
}
})
.getUrls($('.url-column').val());
}
function testGoogleUrl(url) {
return (url.match(/(https:\/\/docs\.google\.com\/)/)) ? true : false;
}
function toggleViews() {
$('.error-view').toggle();
$('.iframe-view').toggle();
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment