This post describes a Google Apps Script function to create a dummy folder structure in Google Drive, including nested folders and files (Documents, Sheets, Slides). It highlights some useful features beginners might find useful such as recursion, randomness, and the use of Google Apps Script services for Drive interactions. The post additionally discusses specific challenges encountered, like using Drive.Files.create for file creation and ensuring compatibility with Shared Drives.
Working with Google Workspace Enterprise customers we recommend using a DEV/Test Google Workspace domain. If you purchase your Workspace licences through a Google Partner you should be able to get a free test domain, which admins can use to test features like Data Loss Prevention, Context Aware-Access without fear of breaking things for your live domain.
Unfortunately there are no convenient settings or data duplication so configuring your test environment can be a challenge. To help create some dummy data with a little help from Gemini I was able to create the following createDummyFoldersWithNesting()
function:
To use the script
- Copy into the Apps Script Editor
- Enable the Google Drive Advanced Service
- Configure the settings inside the function for the root folder and the number of files/folders to generate
Gemini suggested I highlighted the following key points:
- Recursion: The createNestedFolders function calls itself to create folders within folders.
- Randomness: The script randomly determines the number of folders and files to create, and the types of files.
- Google Apps Script Services: It uses DriveApp and Drive.Files to interact with Google Drive.
- File Metadata: When creating files, it uses the mimeType property to specify the Google Apps Script file type.
Whilst Gemini wrote a lot of the code for me (with a little guidance), there were a couple of gotchas I’ll highlight.
Drive.Files.create instead of DriveApp.createFile(name, content, mimeType) – the current documentation would suggest that you can use the .createFile() method and include a MimeType like GOOGLE_SHEETS, but as explained in this Stackoverflow post Google have said “After extensive consideration, we have determined that DriveApp.createFile() should not be used to create MimeType.GOOGLE_* files.”
I could have asked Gemini to rewrite this to use DocumentApp
, SpreadsheetApp
or SlidesApp .create()
methods e.g. SpreadsheetApp.create()
but then I would have to move into a folder, use extra scopes, which all felt a bit messy so instead opted for Drive.Files.create
.
Drive.Files.create supporting Shared Drives without a blob – when using Advanced Services there is a bit of cross referencing required between the auto-complete in the script editor and the associated API documentation. For my script I wanted to support creating files in Shared Drive. To do this requires adding the supportsAllDrives
as optionalArgs
in the Drive.Files.create(resource, mediaData, optionalArgs)
method. As I only wanted blank Docs, Sheets and Slides I was scratching my head as to what to include for the mediaData
blob. Fortunately this issue was discussed in the Google Apps Script Community – Google Group and it was clear I could use null
or undefined
.
Source: Automate Google Drive test data creation with Google Apps Script
Member of Google Developers Experts Program for Google Workspace (Google Apps Script) and interested in supporting Google Workspace Devs.