Create folder structure in Gdrive

It is possible to create a single folder or hierarchical folders in Gdrive to save images under the app folder (see Workflow to create a new folder in drive)

However, it may not be sufficient for your needs as you need a different file structure for organizing miscellaneous files of different file types.

Here is a bit of google apps script code that may be helpful to create a folder structure.

var GDRIVEFOLDERID = "1234567890abcd"

function doPost(e) {
  
  var json = e.postData.contents;
  var body = JSON.parse(json)
  
  Logger.log( "Entering doPost - received " + JSON.stringify(body) );
 
  try {
    createHierFoldersinGDrive(body.foldername);
    return ContentService    // return json success results
          .createTextOutput(
            JSON.stringify({"result":"success",
                            "data": JSON.stringify(e.parameters) }))
          .setMimeType(ContentService.MimeType.JSON);
  }
  catch(e) {
      return ContentService    // return json failure results
          .createTextOutput(
            JSON.stringify({"result":"failure",
                            "data": "Missing parameter" }))
          .setMimeType(ContentService.MimeType.JSON);
  }	  
}  

function createHierFoldersinGDrive(folder) {

  var folders = ["SubFolder1", "SubFolder2", "SubFolder3", "SubFolder4"]

  //Create the folder for the new property inside the specified folder of G Drive
  var newFolder = DriveApp.getFolderById(GDRIVEFOLDERID).createFolder(property);

  for each (var folder in folders) {
    newFolder.createFolder(folder);
  }
  
  Logger.log("Folders created successfully in G Drive");

}
7 3 1,628
3 REPLIES 3

Hi itโ€™s possible to see and choose the folder from app?
thanks

@adiboo_adib

Hi @adiboo_adib,

Just to make sure to be clear on this:

  1. We are working on a Drive Data Source, although the folders will not be โ€œnested and dynamicโ€ at first. You will point your data source to a single folder, and it will give you a table view of everything in that folder. Subfolders will not be really useful. You would need to, as app creator, add another table with the subfolder if you want to access both levels in your app, and they would show up as two separate tables. We are trying to keep it simple for this first release.

  2. If you want to accomplish what you are trying to do now, I think you will need to construct the folder list in a sheet with an external process like Apps Script, using Drive URLs, and then your App could at least see the list of foldersโ€ฆnot sure what you are trying to download. If you want to just view images, then you can construct image urls and put those in a sheet as well, and AppSheet can view those images with no need to โ€œdownloadโ€ them.

You can see this community post, where @Dave_Willett has kindly shared some Apps Script code that looks through a folder and fills up a sheet with all of the file ids. You would need to do something like this to get your folder details loaded into a sheet, and then build your app based on that sheet. (note: he is currently investigating how to make image loads faster, so please monitor that thread for updates).

R,
Scott

Top Labels in this Space