Skip to content

Instantly share code, notes, and snippets.

@rheajt
Last active November 16, 2022 17:04
Show Gist options
  • Save rheajt/e45d75dfa7368de865210ed00bcf7972 to your computer and use it in GitHub Desktop.
Save rheajt/e45d75dfa7368de865210ed00bcf7972 to your computer and use it in GitHub Desktop.
Google Apps Script file-open dialog boilerplate: https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs

File-open dialogs

Google Picker is a "file-open" dialog for information stored in Google servers, including Google Drive, Google Image Search, Google Video Search, and more.

As shown in the example below, Picker's client-side JavaScript API can be used in HTML service to create a custom dialog that lets users select existing files or upload new ones, then pass that selection back to your script for further use.

To enable Picker and obtain an API key, follow these instructions:

  1. In the script editor, select Resources > Cloud Platform project.

  2. In the dialog that appears, click the name of the project your script is associated with.

  3. In the console's left navigation, expand the section APIs & auth and click APIs.

  4. In the filter box, type "picker", then click "Google Picker API" once you see it.

  5. On the next screen, click Enable API.

  6. In the console's left navigation, click Credentials.

  7. In the section for public API access, click Create credentials > API key. This will create the key, but you should restrict it. Click Restrict Key.

  8. In the new tab, select the HTTP referrers (web sites) radio button.

  9. In the referer section that appears, add these URLs as referers, then click Save:

*.google.com

*.googleusercontent.com

  1. In the credentials tab, copy the API key you created for use below, then return to the script editor and click Close to close the dialog.
/**
* Creates a custom menu in Google Sheets when the spreadsheet opens.
*/
function onOpen() {
SlidesApp.getUi().createMenu('Picker')
.addItem('Start', 'showPicker')
.addToUi();
}
/**
* Displays an HTML-service dialog in Google Sheets that contains client-side
* JavaScript code for the Google Picker API.
*/
function showPicker() {
var html = HtmlService.createTemplateFromFile('Picker.html').evaluate()
.setWidth(600)
.setHeight(425)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SlidesApp.getUi().showModalDialog(html, 'Select a file');
}
/**
* Gets the user's OAuth 2.0 access token so that it can be passed to Picker.
* This technique keeps Picker from needing to show its own authorization
* dialog, but is only possible if the OAuth scope that Picker needs is
* available in Apps Script. In this case, the function includes an unused call
* to a DriveApp method to ensure that Apps Script requests access to all files
* in the user's Drive.
*
* @return {string} The user's OAuth 2.0 access token.
*/
function getOAuthToken() {
DriveApp.getRootFolder();
return ScriptApp.getOAuthToken();
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<script>
// IMPORTANT: Replace the value for DEVELOPER_KEY with the API key obtained
// from the Google Developers Console.
var DEVELOPER_KEY = '<?!= PropertiesService.getScriptProperties().getProperty('API_KEY') ?>';
var DIALOG_DIMENSIONS = {width: 600, height: 425};
var pickerApiLoaded = false;
/**
* Loads the Google Picker API.
*/
function onApiLoad() {
gapi.load('picker', {'callback': function() {
pickerApiLoaded = true;
}});
}
/**
* Gets the user's OAuth 2.0 access token from the server-side script so that
* it can be passed to Picker. This technique keeps Picker from needing to
* show its own authorization dialog, but is only possible if the OAuth scope
* that Picker needs is available in Apps Script. Otherwise, your Picker code
* will need to declare its own OAuth scopes.
*/
function getOAuthToken() {
google.script.run.withSuccessHandler(createPicker)
.withFailureHandler(showError).getOAuthToken();
}
/**
* Creates a Picker that can access the user's spreadsheets. This function
* uses advanced options to hide the Picker's left navigation panel and
* default title bar.
*
* @param {string} token An OAuth 2.0 access token that lets Picker access the
* file type specified in the addView call.
*/
function createPicker(token) {
if (pickerApiLoaded && token) {
var picker = new google.picker.PickerBuilder()
// Instruct Picker to display only spreadsheets in Drive. For other
// views, see https://developers.google.com/picker/docs/#otherviews
.addView(google.picker.ViewId.SPREADSHEETS)
// Hide the navigation panel so that Picker fills more of the dialog.
.enableFeature(google.picker.Feature.NAV_HIDDEN)
// Hide the title bar since an Apps Script dialog already has a title.
.hideTitleBar()
.setOAuthToken(token)
.setDeveloperKey(DEVELOPER_KEY)
.setCallback(pickerCallback)
.setOrigin(google.script.host.origin)
// Instruct Picker to fill the dialog, minus 2 pixels for the border.
.setSize(DIALOG_DIMENSIONS.width - 2,
DIALOG_DIMENSIONS.height - 2)
.build();
picker.setVisible(true);
} else {
showError('Unable to load the file picker.');
}
}
/**
* A callback function that extracts the chosen document's metadata from the
* response object. For details on the response object, see
* https://developers.google.com/picker/docs/result
*
* @param {object} data The response object.
*/
function pickerCallback(data) {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var id = doc[google.picker.Document.ID];
var url = doc[google.picker.Document.URL];
var title = doc[google.picker.Document.NAME];
document.getElementById('result').innerHTML =
'<b>You chose:</b><br>Name: <a href="' + url + '">' + title +
'</a><br>ID: ' + id;
} else if (action == google.picker.Action.CANCEL) {
document.getElementById('result').innerHTML = 'Picker canceled.';
}
}
/**
* Displays an error message within the #result element.
*
* @param {string} message The error message to display.
*/
function showError(message) {
document.getElementById('result').innerHTML = 'Error: ' + message;
}
</script>
</head>
<body>
<div>
<button onclick='getOAuthToken()'>Select a file</button>
<p id='result'></p>
</div>
<script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>
@rheajt
Copy link
Author

rheajt commented Nov 15, 2022

The pickerCallback function is where you handle the information sent back from the file you pick. Check the picker api docs for a more recent example using the picker in GAS.

@Myzel394
Copy link

@rheajt Do you know how to send data from the pickerCallback to the sidebar, without reloading it?

@rheajt
Copy link
Author

rheajt commented Nov 16, 2022

can you give an example of what you are trying to accomplish?

you can use javascript to write whatever you want to the sidebar without reloading

@Myzel394
Copy link

Myzel394 commented Nov 16, 2022

I'm basically trying to do the following:

  1. Open the sidebar
  2. User can click on a button, which will open a picker dialog
  3. User picks a file
  4. Show the filename (send the file id) to the sidebar

I made this little illustration maybe it's more clear what I mean this way:

Frame 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment