I would like to add data from folders to a google sheet using google sheets. I would like to have the full folder path in one cell. At the moment I am getting the folder path in one column but not in one row. Like this:
Folder 1
Subfolder 2
SubSubfolder 3
Instead of:
Folder 1 (next cell) Subfolder 1 (next cell) Subsubfolder 1
Folder 1 (next cell) Subfolder 2 (next cell) Subsubfolder 1
Another issue i have been facing is that I would like to limit the depth of the folder path to 3 , how can I integrate that in the loop? I tried using the && operator but that didn't do the trick as the loop goes on and starts counting from a different folder.
Help is highly appreciated :)
Thank you in advance.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var topFolder = DriveApp.getFolderById("FolderID");
var topPath = "/";
sheet.appendRow(["Extracted Folder", "Industry", "Client", "Projectname","Size of Folder", "Link to Folder", "Date Created", "Owner"])
iterateSubFolders(topFolder, topPath)
}
function iterateSubFolders(callingFolder, callingPath, sheet) {
var callingFolderName = callingFolder.getName();
var callingFolderFullPath = callingPath + callingFolderName + "/";
Logger.log(callingFolderFullPath);
addRowToSheetForFolder(callingFolder)
n=0
var childSubFolders = callingFolder.getFolders();
while ((childSubFolders.hasNext()) && (n < 3) ) {
var nextSubFolder = childSubFolders.next();
iterateSubFolders(nextSubFolder, callingFolderFullPath);
n=n+1
}
return callingFolderFullPath;
//This should only return the folder path with the depth = 3
//The full folder path should appear in the google sheets in one row
}
function addRowToSheetForFolder(folder) {
var FolderBanking = DriveApp.getFolderById("13C4cnxnzNP_HMItTiS5PpuyQgWovUuF_");
data = [
//FolderBanking, //Parent folder
folder.getName().toString(),
"move industry here",
"move client to here",
"move project name to here",
folder.getSize(),
folder.getUrl(),
folder.getDateCreated(),
//folder.getOwner().getName(), geht nur für MyDrive
"manuell pflegen",
folder.getDescription(),
];
Logger.log("Writing row to sheet");
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(data);
}
