1
votes

Many thanks for the comments and response. That code was a little too advanced for me and I ended up finding a very inelegant solution that I used because I ran out of time. I was able to get the code to list Folder and first level of subFolders with links, but I have not yet been able to get it to iterate through all levels of folders, mostly because I just need to back up and learn a lot of the basics. I was also able to get all folders to list using some code I found to create a tree, but I couldn't get it to format in a way that you could actually see the structure, or add links. I'm going to continue to try, and will post if I sort it out. Here is what I used, which was fine for our purposes because our shared drive is fairly limited.

For reference, this was the code I used to start with: https://superuser.com/questions/1095578/list-of-subfolder-names-and-file-links-in-google-sheets-script

function listFolders(foldername) {
var ss = SpreadsheetApp.openById(ID);
var sheet = ss.getSheetByName("sheet name");
sheet.appendRow("Parent Folder", "Name", "Link" ]);

//change the folder ID below to reflect your folder's ID (look in the            
URL when you're in your folder)

var folders = DriveApp.getFolderById(ID);
var contents = folders.getFolders();
var cnt = 0;
var folderD;

while (contents.hasNext()) {
var folderD = contents.next();
cnt++;

data = [
        folders.getName(),
        folderD.getName(),
        folderD.getUrl(),
];


sheet.appendRow(data);
};
};

Original Post: I am a beginner using script in google sheets and I am trying to create a list of folders in a google drive with many subfolders. Ideally it would be a tree form but I'd settle for a list at this point. I don't need to list all the files, just the folders. I have been trying to get the code below to work but it keeps hanging up at calling up the spreadsheet. Can anyone help?

I have tried calling up both the folders and the spreadsheet by both name and ID but it always tells me it can't execute the getactivespreadsheet command. I have also tried to modify the code referred to in another another question but I can't get that to work either: https://ctrlq.org/code/19923-google-drive-files-list

function generateFolderIndex(myfoldername) {
var folder = DriveApp.getFolderById('0B8vOJQUb-IIVTHdudlZSVkdtdE0');
var subFolders = folder.getFolders();
var childFolders = subFolders
var ss = SpreadsheetApp.getActiveSpreadsheet('1Trv9OtJFnD4AdSHrZKFfsSu6JMV9f78H6wwZNhF2_M4');
var sheet = ss.getSheetByName('Directory');

sheet.clear(directory);
sheet.appendRow([name, link]);

while (subFolders.hasNext())
{    
var childFolder = childFolders.next(); 

var foldername = childFolder.getname();
var name = childFolder.getName()
var link = childFolder.getUrl()
var date = childFolder.getDateCreated()


data = [name, link]
sheet.appendRow(data);

}
};

I am trying to get a sheet that lists folders and subfolders with URL links. I am currently receiving the following error message: [19-05-31 15:32:20:911 EDT] Execution failed: Cannot find method getActiveSpreadsheet(string). (line 5, file "Code") [0.432 seconds total runtime]

2
Google Drive is not guaranteed to be a tree structure at all. (Team Drive is). Folder A containing Folder B which contains Folder A? Sure thing, go ahead.tehhowch
Also, appendRow is horrendously slow and is only worth the slowness when you need atomic behavior. Otherwise use getRange and setValues.tehhowch

2 Answers

3
votes

Or.. the easy way...

Use DRIVE or FS DRIVE APP for desktop in PC. Usea A CMD (windows)... AND THE FUNCTION

TREE >a.txt

The generated file a.txt will display all the tree.

IT SAVES HOURS OF RESEARCH.

0
votes

SpreadsheetApp.getActiveSpreadsheet() doesn't have any parameters.

However

SpreadsheetApp.openById('ssid') does require and id. I think perhaps you meant to be using openById();

openById

getActiveSpreadsheet

This is a script that I'm currently working on but it generates a list of Spreadsheets and you can exclude folders by id and files by id.

function getAllSpreadsheets() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('FilesAndFolders');
  if(sh.getLastRow()>0) {
    sh.getRange(1,1,sh.getLastRow(),2).clear().clearDataValidations();
  }
  getFnF();
  SpreadsheetApp.getUi().alert('Process Complete')
}

var level=0;
function getFnF(folder) {
  var folder= folder || DriveApp.getRootFolder();
  //var folder=DriveApp.getRootFolder();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('FilesAndFolders');
  var files=folder.getFilesByType(MimeType.GOOGLE_SHEETS)
  while(files.hasNext()) {
    var file=files.next();
    if(isExcluded(file.getId(),'file')){continue;}
    var firg=sh.getRange(sh.getLastRow() + 1,level + 1);
    firg.setValue(Utilities.formatString('=HYPERLINK("%s","%s")',file.getUrl(),'FILE: ' + file.getName()));
    firg.offset(0,1).insertCheckboxes('Exclude','Include');
  }
  var subfolders=folder.getFolders() 
  while(subfolders.hasNext()) {
    var subfolder=subfolders.next();
    if(isExcluded(subfolder.getId(),'folder')){continue;}
    var forg=sh.getRange(sh.getLastRow() + 1,level + 1);
    forg.setValue(Utilities.formatString('=HYPERLINK("%s","%s")',subfolder.getUrl(),'FOLDER: ' + subfolder.getName()));
    //forg.offset(0,1).insertCheckboxes('Exclude','Include');
    //level++;
    getFnF(subfolder);
  }
  //level--;
}

function isExcluded(id,type) {//type: file or folder
  var type=type||'Missing Input';
  var xFldrIdA=['Excluded folder ids'];
  var xFileIdA=['Excluded file ids'];
  var type=type.toLowerCase();
  switch(type) {
    case 'file':
      return xFileIdA.indexOf(id)>-1;
      break;
    case 'folder':
      return xFldrIdA.indexOf(id)>-1;
      break;
    default:
      throw(Utilities.formatString('Error: Invalid Type: %s in isExcluded.',type));
      return true;//assume excluded
      break;
  }
}

Your welcome to use it, perhaps it will help.