Skip to content

Instantly share code, notes, and snippets.

@nflint
Last active January 15, 2022 00:44
Show Gist options
  • Save nflint/36f5336c1f7c62d4506a14d9fb87637d to your computer and use it in GitHub Desktop.
Save nflint/36f5336c1f7c62d4506a14d9fb87637d to your computer and use it in GitHub Desktop.
Google Apps Parse Folder
/* /**
* Google Apps Script - List all files & folders in a Google Drive folder, & write into a speadsheet.
* - Main function 1: List all folders
* - Main function 2: List all files & folders
*
* Hint: Set your folder ID first! You may copy the folder ID from the browser's address field.
* The folder ID is everything after the 'folders/' portion of the URL.
*
* @version 1.0
* @see https://github.com/mesgarpour
*/
// TODO: Set folder ID
var folderId = 'XXXXXXXX';
// Main function 1: List all folders, & write into the current sheet.
function listFolders(){
getFolderTree(folderId, false);
};
// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
getFolderTree(folderId, true);
};
// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
try {
// Get folder by id
var parentFolder = DriveApp.getFolderById(folderId);
// Initialise the sheet
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["P#","Document","Category","topic","type","Manual?","Audience","Description","File Path","Date","Last Updated","MIME Type","Size","ID"]);
// Get files and folders
getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
} catch (e) {
Logger.log(e.toString());
}
};
// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
var childFolders = parent.getFolders();
// List folders inside the folder
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
// Logger.log("Folder Name: " + childFolder.getName());
data = [
'', //Blank Column for #P (Priority)
'=HYPERLINK("' + childFolder.getUrl() + '","' + childFolder.getName().replace(/"/gi,'') + '")', //Sets Title as anchor text
'', //Blank Column for Category
'', //Blank Column for Topic
'', //Blank Column for Type
'', //Blank Column for Manual
'', //Blank Column for Audience
childFolder.getDescription(), //Get Folder Desc
parentName + "/" + childFolder.getName(),
childFolder.getDateCreated(), //Get Folder Creation Date
childFolder.getLastUpdated(), //Get Folder Last Updated Date
'', //Leave Blank for since we cannot get mime type.TODO: FIX.
childFolder.getSize(), //Get Size (Will only show for .zip, .pdf, .jpeg, etc.)
childFolder.getId() //Get ID - useful for cross reference or future upload.
];
// Write
sheet.appendRow(data);
// List files inside the folder
var files = childFolder.getFiles();
while (listAll & files.hasNext()) {
var childFile = files.next();
data = [
'', //Blank Column for #P (Priority)
'=HYPERLINK("' + childFile.getUrl() + '","' + childFile.getName().replace(/"/gi,'') + '")', //Sets Title as anchor text
'', //Blank Column for Category
'', //Blank Column for Topic
'', //Blank Column for Type
'', //Blank Column for Manual
'', //Blank Column for Audience
childFile.getDescription(), //Get File Desc
parentName + "/" + childFolder.getName() + "/" + childFile.getName(), //Get File Path
childFile.getDateCreated(), //Get File Creation Date
childFile.getLastUpdated(), //Get File Last Update
childFile.getMimeType().replace('application/vnd.',''), //Get File Type clean it up to be a little more human readable.
childFile.getSize(), //Get Size (Will only show for .zip, .pdf, .jpeg, etc.)
childFile.getId() //Get ID - useful for cross reference or future upload.
];
// Write
sheet.appendRow(data);
}
// Recursive call of the subfolder
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);
}
};
@Nathaliehech
Copy link

@nflint I love this code! Do you know how to use this code and set the folder description according to a column found on the sheet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment