Login

Please fill in your details to login.





list google folder contents in a spreadsheet

A little Google Apps script which generates a list of, and links to, all files in a folder on Google Drive in your spreadsheet.
This little Google Apps script function will generate a list of, and links to, all the files you have in a folder on Google Drive into a Google sheet tab. Make sure you add this script as an embedded script in the spreadsheet (accessed from 'Tools > Scripts') rather than a standalone script.

function listFolderContents() {
  var folder_id = 'FOLDER_ID'; // ID of folder containing course lesson lists
    
  var folder = DriveApp.getFolderById(folder_id);
  var contents = folder.getFiles();
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("files"); // Replace with name of spreadsheet tab
  sheet.clear();
  
  var file;
  var name;
  var link;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    id = link.match('/d\/([^\/]+)/')[1];
    sheet.appendRow( [name, link, id] );     
  }  
};


After this, you can use this list for validation in a drop down cell on another sheet. If you want to import the data into the sheet from the file, there are a couple of options depending on the file type you are importing from.

For instance, I have created a folder of TXT (technically PSV - Pipe Separated Value) files from which I'd like to import the data into the sheet. Be warned - this process will NOT work if you've uploaded the txt files to Drive with the option "convert files to Google Docs format" selected in Drive options!

=importdata("https://drive.google.com/uc?export=download&id="&index(list_id,match(DROPDOWN,list_name,0)),"|")


Where...
list_id
is the named range for the third (id) column in the list of files.
DROPDOWN
is the cell reference for the dropdown select cell.
list_name
is the named range for the first (name) column of the list of files.

Here is an example Google Sheet and associated data files so you can see this in operation.
Last modified: November 15th, 2021
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning