Selecting Files in Google Drive using Select Box for Google Apps Script


This is a sample script for selecting files in Google Drive using HTML select box for Google Apps Script.


Feature of this sample.

  • It is a simple and space saving.
  • When the folder is selected, the files in the folder are shown.
  • When the file is selected, the ID of file is retrieved. Users can use this ID at GAS.
  • When a folder is opened, all files in the folder are cached. By this, the second access of the folder is faster.
  • It doesn’t retrieve all files in Google Drive at once, so the read of files from Google Drive becomes the minimum necessary.

I will use this for applications that users need to select files on Google Drive.


From this demonstration, it is found that when a folder is selected for the first time, the time to retrieve files is required. At the second access for the folder, the speed is faster than that for the first time. Because the data of files is cached.

Although this sample uses side bar for Spreadsheet, of course, you can also use this sample to dialog box and Web Apps.


  1. Create Spreadsheet.
  2. Open the Spreadsheet and launch script editor.
  3. Copy and paste following GAS ( to the script editor.
  4. Create new HTML file on the script editor.
  5. Copy and paste following HTML (index.html) to the created HTML file on the script editor. The file name is index.html.
  6. Run main() at GAS (
  7. See Spreadsheet. You can see the side bar like the demonstration.


function main() {
    .setTitle("File selector sample")

function getFiles(e, rootFolderId) {
  var data = {};
  var idn = e;
  e = e == "root" ? DriveApp.getRootFolder().getId() : e;
  data[e] = {};
  data[e].keyname = DriveApp.getFolderById(e).getName();
  data[e].keyparent = idn == rootFolderId
    ? null : DriveApp.getFolderById(e).getParents().hasNext()
    ? DriveApp.getFolderById(e).getParents().next().getId() : null;
  data[e].files = [];
  var da = idn == "root" ? DriveApp.getRootFolder() : DriveApp.getFolderById(e);
  var folders = da.getFolders();
  var files = da.getFiles();
  while (folders.hasNext()) {
    var folder =;
    data[e].files.push({name: folder.getName(), id: folder.getId(), mimeType: "folder"});
  while (files.hasNext()) {
    var file =;
    data[e].files.push({name: file.getName(), id: file.getId(), mimeType: file.getMimeType()});
  return data;

function doSomething(id) {
  // do something
  var res = id;
  return res;

HTML : index.html

  #select_file {
    text-align: left;
    width: 95%;
    font-size: 1em;
    margin: auto;
    height: 3em;
<script src=""></script>

<select id="select_file"></select>
<p id="id"></p>

var alldata = {};

$(function() {
  const rootFolderId = "root";, rootFolderId);

  $('#select_file').change(function() {
    var id = $('#select_file').val();
    var disp = $('#select_file option:selected').text();
    if (~disp.indexOf("Folder") || ~disp.indexOf("../")) {
      $('#select_file > option').remove();
      if (alldata[id]) {
        var dat = {};
        dat[id] = alldata[id];
      } else {, rootFolderId);

function importData(e) {
  var key = Object.keys(e)[0];
  if (!alldata[key]) alldata[key] = e[key];
  if (e[key]["keyparent"]) {
    $('#select_file').append($('<option>').html("./" + e[key]["keyname"]).val(key));
  } else {
    $('#select_file').append($('<option>').html("./" + e[key]["keyname"]).val(key));
  for (var i=0; i < e[key]["files"].length; i++) {
      .html(e[key]["files"][i].mimeType == "folder" ? "[Folder]" + e[key]["files"][i].name : e[key]["files"][i].name)

function output(res){
  • When you want to change the top folder of the list to the specific folder, please modify root of const rootFolderId = "root"; to the folder ID of the specific folder. By this, the top folder of the list becomes the specific folder.