title: List every file and folder of a shared drive in a spreadsheet with Apps Script
body: <p>I found a script that works flawlesly for My Drive that I want to adapt to also support Shared Drives.</p>
  <pre><code>    function onOpen() {
    var SS = SpreadsheetApp.getActiveSpreadsheet();
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('List Files/Folders')
      .addItem('List All Files and Folders', 'listFilesAndFolders')
      .addToUi();
  };
  
  function listFilesAndFolders(){
    var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
    if (folderId === &quot;&quot;) {
      Browser.msgBox('Folder ID is invalid');
      return;
    }
    getFolderTree(folderId, true); 
  };
  
  // Get Folder Tree
  function getFolderTree(folderId, listAll) {
    try {
      // Get folder by id
      var parentFolder = DriveApp.getFolderById(folderId);
      
      // Initialise the sheet
      var file, data, sheet = SpreadsheetApp.getActiveSheet();
      sheet.clear();
      sheet.appendRow([&quot;Full Path&quot;, &quot;Name&quot;,&quot;Type&quot; ,&quot;Date&quot;, &quot;URL&quot;, &quot;Last Updated&quot;, &quot;Description&quot;, &quot;Size&quot;,&quot;Owner Email&quot;]);
      
      // Get files and folders
      getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
    } catch (e) {
      Logger.log(e.toString());
    }
  };
  
  // Get the list of files and folders and their metadata in recursive mode
  function getChildFolders(parentName, parent, data, sheet, listAll) {
    var childFolders = parent.getFolders();
   
    // List folders inside the folder
    while (childFolders.hasNext()) {
      var childFolder = childFolders.next();
      var folderId = childFolder.getId();
      data = [ 
        parentName + &quot;/&quot; + childFolder.getName(),
        childFolder.getName(),
        &quot;Folder&quot;,
        childFolder.getDateCreated(),
        childFolder.getUrl(),
        childFolder.getLastUpdated(),
        childFolder.getDescription(),
        childFolder.getSize()/1024,
        childFolder.getOwner().getEmail()
      ];
      // Write
      sheet.appendRow(data);
      
      // List files inside the folder
      var files = childFolder.getFiles();
      while (listAll &amp; files.hasNext()) {
        var childFile = files.next();
        data = [ 
          parentName + &quot;/&quot; + childFolder.getName() + &quot;/&quot; + childFile.getName(),
          childFile.getName(),
          &quot;Files&quot;,
          childFile.getDateCreated(),
          childFile.getUrl(),
          childFile.getLastUpdated(),
          childFile.getDescription(),
          childFile.getSize()/1024,
          childFile.getOwner().getEmail(),
        ];
        // Write
        sheet.appendRow(data);
      }
      // Recursive call of the subfolder
      getChildFolders(parentName + &quot;/&quot; + childFolder.getName(), childFolder, data, sheet, listAll);  
    }
  };
  </code></pre>
  <p>I honestly still don't fully understand how</p>
  <pre><code>{supportsAllDrives: true}
  </code></pre>
  <p>works but I'll assume that it is exactly what's missing.</p>
  <p>I've been trying to get some more information about it, but what I've found is't exactly useful for me and it's not a complete explanation on how to use it.</p>
  <p>I'm very new to Apps Script but I'm doing my best to make fully operational code or at least understand it, so I would really appreciate if anyone could link me to any good pages  about it.</p>