Listview with GDrive Folder

Hi!
I need to create an app to have the folders and files contained within a specific Google Drive folder in a listview.
For example:
Folder 1: File1
Folder 2: File2.pdf, File3.pdf
Folder 3: File4.pdf, File5.pdf
Folder 4: File6.pdf, File7.pdf

You will need a google apps script web app with these functions:

// return list of folders in a folder
function getListOfFoldersInFolder(targetID) {
  folderList = [];
  var parentFolder = DriveApp.getFolderById(targetID);
  var folders = parentFolder.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    folderList.push([folder.getName(),folder.getId()]);
  }
  var output = JSON.stringify(folderList);
  return ContentService.createTextOutput(output);
}

// return list of files in a folder
function getListOfFilesInFolder(targetID) {
  filesList = [];
  var parentFolder = DriveApp.getFolderById(targetID);
  var files = parentFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    filesList.push([file.getName(),file.getId()]);
  }
  var output = JSON.stringify(filesList);
  return ContentService.createTextOutput(output);
}
1 Like

How insert e.parameters?

function doGet(e) {
//return list of folder names and IDs from folder
  if ( e.parameter.func == "getListFoldersInFolder" ) {
  return getListOfFoldersInFolder(e.parameter.targetID);
  }
  
  //return list of file names and IDs from folder
  else if ( e.parameter.func == "getListFilesInFolder" ) {
  return getListOfFilesInFolder(e.parameter.targetID);
  }
}

You can create a standalone script if you prefer

1 Like

This is the full code, right?

function doGet(e) {
//return list of folder names and IDs from folder
  if ( e.parameter.func == "getListFoldersInFolder" ) {
  return getListOfFoldersInFolder(e.parameter.targetID);
  }
  
  //return list of file names and IDs from folder
  else if ( e.parameter.func == "getListFilesInFolder" ) {
  return getListOfFilesInFolder(e.parameter.targetID);
  }
}

// return list of folders in a folder
function getListOfFoldersInFolder(targetID) {
  folderList = [];
  var parentFolder = DriveApp.getFolderById(targetID);
  var folders = parentFolder.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    folderList.push([folder.getName(),folder.getId()]);
  }
  var output = JSON.stringify(folderList);
  return ContentService.createTextOutput(output);
}

// return list of files in a folder
function getListOfFilesInFolder(targetID) {
  filesList = [];
  var parentFolder = DriveApp.getFolderById(targetID);
  var files = parentFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    filesList.push([file.getName(),file.getId()]);
  }
  var output = JSON.stringify(filesList);
  return ContentService.createTextOutput(output);
}

The Wen URL is: ?func=getListFilesInFolder&TargetID=xxxxxxx
This is right?

Your full url in your AI2 app would look like this:

<WEBAPPSCRIPTURL>?func=getListFilesInFolder&targetID=<DESIREDFOLDERID>

You use a Get request

1 Like

I created this block:

But I can't figure out how to get the correct function to run based on the contents of the open folder.
I am attaching a shared link to explain me better.
In conclusion I would like to create an alternative app to GDrive to explore folders and files.

https://drive.google.com/drive/folders/1sBJjlv7lbOqKbFyTvm6FkUIFjDPRtb06?usp=sharing

Are you having trouble with the contents of Label1 ? If so try:

image

You also might find it easier to use different global variables and listview for your second iteration....

If you can hold on for @ a week or so, there are at least two of us working on an extension for this.

1 Like

Ok, I can wait for the extension! Thanks as always!

In the meantime, if you have any suggestions for functions on Google Drive not in the list, please let me know:

//create folder in root
//create folder in a folder
//return list of folder names and IDs in root
//return list of file names and IDs in root
//return list of folder names and IDs from folder
//return list of file names and IDs from folder
//return list of file names and IDs by type from folder
//delete a file
//delete a folder, but only if it is empty
//share file by Id to other google account
//share folder by Id to other google account
//share file by Id and access/permission
//share folder by Id and access/permission

... copy an image in GD to device's Gallery

Upload File in root Folder
Upload File in other folder (folder in folder)
Download File

Maybe they are trivial commands but they would be useful in an extension.

download a file, yes :slight_smile:

putting it in the gallery, though, is beyond my pay grade :smiley:

Yes, have these coming, but method and file size is dependent on the framework used (webview/web component)

1 Like

download it to shared storage and use the GalleryRefresh method from the tools extension App Inventor Extensions: Tools | Pura Vida Apps

Taifun

yes, indeed :slight_smile: