Last active
July 18, 2024 09:43
-
-
Save mhawksey/8f7c295ae60429501a44edf31cd0f63a to your computer and use it in GitHub Desktop.
See https://hawksey.info/blog/2024/07/automate-google-drive-test-data-creation-with-google-apps-script/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createDummyFoldersWithNesting() { | |
// Get the starting point for the dummy folder structure | |
// Replace with your desired folder ID or use 'root' to start in the root of your My Drive | |
const rootFolder = DriveApp.getFolderById("YOUR_FOLDER_ID"); | |
// Configuration for the dummy structure | |
const maxNestingLevel = 3; // How many levels deep the structure will go | |
const maxFoldersPerLevel = 5; // Maximum number of folders to create in each level | |
const maxFilesPerFolder = 5; // Maximum number of files to create in each folder | |
const fileFolderPrefix = 'Dummy' // Text added to the start of all folders/files | |
// ---------------------------- // | |
// Array of file types we want to create (Documents, Sheets, Slides) | |
const fileTypes = ["document", "spreadsheet", "presentation"]; | |
// Function to randomly pick a file type from the array | |
const randomFileType = () => fileTypes[Math.floor(Math.random() * fileTypes.length)]; | |
// Recursive function to create nested folders and files | |
const createNestedFolders = (parentFolder, currentLevel) => { | |
// Decide how many folders to create in this level | |
const numFolders = Math.floor(Math.random() * maxFoldersPerLevel) + 1; // Randomly create 1-5 folders | |
console.log(`Creating ${numFolders} folders in ${parentFolder.getName()} (level ${currentLevel})`); | |
// Create folders and files within them | |
for (let i = 0; i < numFolders; i++) { | |
const levelName = `${currentLevel}.${i + 1}`; // Name for the folder (e.g., "1.2" for 2nd folder in level 1) | |
const folder = parentFolder.createFolder(`${fileFolderPrefix} Folder ${levelName}`); // Create the new folder | |
// Create files within the new folder | |
Array.from({ length: maxFilesPerFolder }, (_, index) => { // Create up to 5 files | |
const fileType = randomFileType(); // Pick a random file type | |
const fileMetadata = { // Prepare information about the new file | |
name: `${fileFolderPrefix} ${fileType[0].toUpperCase() + fileType.slice(1)} ${levelName}.${index + 1}`, // File name (e.g., "Dummy Document 1.2.3") | |
mimeType: `application/vnd.google-apps.${fileType}`, // Google Apps Script file type (document, spreadsheet, or presentation) | |
parents: [folder.getId()] // Place the file in the newly created folder | |
}; | |
Drive.Files.create(fileMetadata, undefined, { supportsAllDrives: true }); // Create the file in Google Drive | |
console.log(` Created ${fileType} in folder ID: ${folder.getId()}`); | |
}); | |
// Recursive call: If haven't reached max nesting level, create more folders in this one | |
if (currentLevel < maxNestingLevel) { | |
createNestedFolders(folder, currentLevel + 1); | |
} | |
} | |
}; | |
// Start the process | |
console.log("Starting dummy folder creation..."); | |
createNestedFolders(rootFolder, 1); | |
console.log("Finished creating dummy folders!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment