Google Apps Script

Class DocsListDialog

A "file-open" dialog for Google Drive. Unlike most UiApp objects, DocsListDialog should not be added to the UiInstance. The example below shows how to display a DocsListDialog in the new version of Google Sheets.

Note that HTML service offers a similar but superior feature, Google Picker. In almost all cases, using Google Picker is preferable.

 
function onOpen() {
   SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
       .createMenu('Custom Menu')
       .addItem('Select file', 'showDialog')
       .addToUi();
 }
 
 function showDialog() {
   var app = UiApp.createApplication()
       .setWidth(570)
       .setHeight(352);
 
   var serverHandler = app.createServerHandler('pickerHandler');
 
   app.createDocsListDialog()
      .addCloseHandler(serverHandler)
      .addSelectionHandler(serverHandler)
      .showDocsPicker();
 
   SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(app,' ');
 }
 
 function pickerHandler(e) {
   var action = e.parameter.eventType;
   var app = UiApp.getActiveApplication();
 
   if (action == 'selection') {
     var doc = e.parameter.items[0];
     var id = doc.id;
     var name = doc.name;
     var url = doc.url;
     app.add(app.createLabel('You picked '));
     app.add(app.createAnchor(name, url));
     app.add(app.createLabel('(ID: ' + id + ').'));
   } else if (action == 'close') {
     app.add(app.createLabel('You clicked "Cancel".'));
   }
 
   return app;
 }
 

Methods

MethodReturn typeBrief description
addCloseHandler(handler)DocsListDialogAdd a handler for close events.
addSelectionHandler(handler)DocsListDialogAdd a handler for selection events.
addView(fileType)DocsListDialogAdd a type of file that this DocsListDialog will show.
getId()StringReturns the id that has been assigned to this object.
getType()StringGets the type of this object.
setDialogTitle(title)DocsListDialogSet the title of this DocsListDialog.
setHeight(height)DocsListDialogSet the height of this DocsListDialog.
setInitialView(fileType)DocsListDialogSet the initial type of file that this DocsListDialog will show.
setMultiSelectEnabled(multiSelectEnabled)DocsListDialogSet whether multiple items can be selected.
setWidth(width)DocsListDialogSet the width of this DocsListDialog.
showDocsPicker()DocsListDialogShow this DocsListDialog.

Detailed documentation

addCloseHandler(handler)

Add a handler for close events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var item1 = app.createTreeItem("item1");
   item1.addItem(app.createTreeItem("item2"));
   var tree = app.createTree();
   tree.addItem(item1);
   var handler = app.createServerHandler("handlerFunction");
   tree.addCloseHandler(handler)
   app.add(tree);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "close".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
 }
 
In addition, the values of certain widgets can be sent up with the event as well as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


addSelectionHandler(handler)

Add a handler for selection events.

Note that you can have multiple handlers for the same event on the same widget. They will be called in the order that they were added to the widget, although ServerHandlers may appear to happen simultaneously.

The handler passes back some information to the server about what happened. This information can be accessed as follows:

 
function doGet() {
   var app = UiApp.createApplication();
   var item1 = app.createTreeItem("item1");
   item1.addItem(app.createTreeItem("item2"));
   var tree = app.createTree();
   tree.addItem(item1);
   var handler = app.createServerHandler("handlerFunction");
   tree.addSelectionHandler(handler)
   app.add(tree);
   return app;
 }

 function handlerFunction(eventInfo) {
   var parameter = eventInfo.parameter;
   // the type of event, in this case "selection".
   var eventType = parameter.eventType;
   // the id of the widget that fired this event.
   var source = parameter.source;
 }
 
In addition, the values of certain widgets can be sent up with the event as well as "callback elements." See the documentation of ServerHandler for more information.

Parameters

NameTypeDescription
handlerHandlerthe handler to execute when the event occurs. This can be a ClientHandler or a ServerHandler.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


addView(fileType)

Add a type of file that this DocsListDialog will show.

Parameters

NameTypeDescription
fileTypeFileTypethe type of file to add.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


getId()

Returns the id that has been assigned to this object.

This can be used in conjunction with app.getElementById() to retrieve a reference to this object.

Return

String — the id that has been assigned to this object


getType()

Gets the type of this object.

Return

String — the object type


setDialogTitle(title)

Set the title of this DocsListDialog.

Parameters

NameTypeDescription
titleStringthe new title.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


setHeight(height)

Set the height of this DocsListDialog.

Parameters

NameTypeDescription
heightIntegerthe new height, in pixels.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


setInitialView(fileType)

Set the initial type of file that this DocsListDialog will show.

Parameters

NameTypeDescription
fileTypeFileTypethe type of file to show.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


setMultiSelectEnabled(multiSelectEnabled)

Set whether multiple items can be selected.

Parameters

NameTypeDescription
multiSelectEnabledBooleanwhether multiple items can be selected.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


setWidth(width)

Set the width of this DocsListDialog.

Parameters

NameTypeDescription
widthIntegerthe new width, in pixels.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.


showDocsPicker()

Show this DocsListDialog.

Return

DocsListDialog — the DocsListDialog itself, useful for chaining.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.