Created
August 23, 2016 12:47
-
-
Save rheajt/2d1b6379a017011c33d90f0d7c9383b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
SpreadsheetApp.getUi().createAddonMenu().addItem('open picker', 'openPicker').addToUi(); | |
} | |
function onInstall() { | |
onOpen(); | |
} | |
function getOAuthToken() { | |
DriveApp.getRootFolder(); | |
return ScriptApp.getOAuthToken(); | |
} | |
function openPicker() { | |
var html = HtmlService.createHtmlOutputFromFile('Picker') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME) | |
.setHeight(400) | |
.setWidth(600); | |
return SpreadsheetApp.getUi().showModalDialog(html, 'open picker'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> | |
<title>Google Picker Example</title> | |
<script type="text/javascript"> | |
// The Browser API key obtained from the Google Developers Console. | |
var developerKey = 'GET YOUR API KEY FROM THE DEVELOPERS CONSOLE'; | |
// Scope to use to access user's files | |
// Find others at: https://developers.google.com/picker/docs/#otherviews | |
var scope = ['https://www.googleapis.com/auth/drive.readonly']; | |
var pickerApiLoaded = false; | |
var oauthToken; | |
// Use the API Loader script to load google.picker and gapi.auth. | |
function onApiLoad() { | |
gapi.load('picker', {'callback': onPickerApiLoad}); | |
} | |
function onPickerApiLoad() { | |
pickerApiLoaded = true; | |
google.script.run | |
.withSuccessHandler(handleAuthResult) | |
.getOAuthToken(); | |
} | |
function handleAuthResult(authResult) { | |
if (authResult && !authResult.error) { | |
oauthToken = authResult; | |
createPicker(); | |
} | |
} | |
// Create and render a Picker object for picking user spreadsheets. | |
function createPicker() { | |
if (pickerApiLoaded && oauthToken) { | |
var picker = new google.picker.PickerBuilder() | |
.addView(google.picker.ViewId.SPREADSHEETS) | |
.enableFeature(google.picker.Feature.NAV_HIDDEN) | |
.hideTitleBar() | |
.setOAuthToken(oauthToken) | |
.setDeveloperKey(developerKey) | |
.setCallback(pickerCallback) | |
.setOrigin('https://docs.google.com') | |
.build(); | |
picker.setVisible(true); | |
} | |
} | |
// A simple callback implementation. | |
function pickerCallback(data) { | |
var url = 'nothing'; | |
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) { | |
var doc = data[google.picker.Response.DOCUMENTS][0]; | |
url = doc[google.picker.Document.URL]; | |
} | |
var message = 'You picked: ' + url; | |
document.getElementById('result').innerHTML = message; | |
} | |
</script> | |
</head> | |
<body> | |
<div id="result"></div> | |
<!-- The Google API Loader script. --> | |
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment